readNBytes(byte[], int, int): This method is available in the java.io.BufferedInputStream class of Java.
Syntax:
int java.io.InputStream.readNBytes(byte[] b, int off, int len) throws IOException
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.
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.
Throws:
1. IOException - if an I/O error occurs.
2. NullPointerException - if b is null.
3. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off.
Approach 1: When no exception
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamreadNBytes2 {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);byte b[] = { 1, 2, 2, 3, 5 };int off = 0, len = 2;System.out.println(bufferedInputStream.readNBytes(b, off, len));}}
Output:
0
Approach 2: IOException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamreadNBytes2 {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);byte b[] = { 1, 2, 2, 3, 5 };int off = 0, len = 2;bufferedInputStream.close();System.out.println(bufferedInputStream.readNBytes(b, off, len));}}
Output:
Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:168) at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:334) at java.base/java.io.InputStream.readNBytes(InputStream.java:503)
Approach 3: NullPointerException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamreadNBytes2 {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);byte b[] = null;int off = 0, len = 2;System.out.println(bufferedInputStream.readNBytes(b, off, len));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "b" is null at java.base/java.io.InputStream.readNBytes(InputStream.java:499)
Approach 4: IndexOutOfBoundsException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamreadNBytes2 {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);byte b[] = { 1, 2, 2, 3, 5 };int off = -1, len = 2;System.out.println(bufferedInputStream.readNBytes(b, off, len));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [-1, -1 + 2) out of bounds for length 5 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) at java.base/java.io.InputStream.readNBytes(InputStream.java:499)
No comments:
Post a Comment