ByteArrayInputStream read(byte[], int, int) in Java

read(byte[], int, int): This method is available in the java.io.ByteArrayInputStream class of Java.

Syntax:

int java.io.ByteArrayInputStream.read(byte[] b, int off, int len)

This method takes three arguments. This method reads up to len bytes of data into an array of bytes from this input stream.

If pos equals count, then -1 is returned to indicate the end of the file. Otherwise, the number k of bytes read equals the smaller len and count-pos.

Parameters: Three parameters are required for this method.

b: the buffer into which the data is read.

off: the start offset in the destination array b.

len: the maximum number of bytes read.

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Throws:

1. NullPointerException - If b is null.

2. 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.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamread2 {
    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.read(b,
off, len));
        byteArrayInputStream.close();
    }

}

Output:

2


Approach 2: NullPointerException

Java

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamread2 {
    public static void main(String[] args) throws IOException {

        byte buf[] = { 1, 2, 3, 4 };
        ByteArrayInputStream byteArrayInputStream =
new ByteArrayInputStream(buf);

        byte b[] = null;
        int off = 0, len = 2;

        System.out.println(byteArrayInputStream.read(b,
off, len));
        byteArrayInputStream.close();
    }

}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "b" is null at java.base/java.io.ByteArrayInputStream.read(ByteArrayInputStream.java:173)



Approach 3: IndexOutOfBoundsException 

Java

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamread2 {
    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 = -1, len = 2;

        System.out.println(byteArrayInputStream.read(b,
off, len));
        byteArrayInputStream.close();
    }

}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [-1, -1 + 2) out of bounds for length 4 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.ByteArrayInputStream.read(ByteArrayInputStream.java:173)


No comments:

Post a Comment