readNBytes(byte[], int, int): This method is available in the java.io.ByteArrayInputStream class of Java.
Syntax:
int java.io.ByteArrayInputStream.readNBytes(byte[] b, int off, int len)
This method takes three arguments. This method reads the requested number of bytes from the input stream into the given byte array. This method blocks until len bytes of input data have been read, the end of the stream is detected, or an exception is thrown.
Note:
1. The number of bytes actually read, possibly zero, is returned. This method does not close the input stream.
2. If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read up to len bytes.
Parameters: Three parameters are required for this method.
b: the byte array into which the data is read.
off: the start offset in b at which the data is written.
len: the maximum number of bytes to read
Returns: the actual number of bytes read into the buffer.
Exceptions: NA
Approach
Java
import java.io.ByteArrayInputStream;import java.io.IOException;public class ByteArrayInputStreamreadNBytes {public static void main(String[] args) throws IOException {byte buf[] = { 1, 2, 3, 4 };ByteArrayInputStream byteArrayInputStream =new ByteArrayInputStream(buf);byte b[] = { 1, 2, 5, 6 };int off = 0, len = 2;System.out.println(byteArrayInputStream.readNBytes(b, off, len));byteArrayInputStream.close();}}
Output:
2
No comments:
Post a Comment