ByteArrayOutputStream write(byte[], int, int) in Java

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

Syntax:

void java.io.ByteArrayOutputStream.write(byte[] b, int off, int len)

This method takes three arguments. This method writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream.

Parameters: Three parameters are required for this method.

b: the data.

off: the start offset in the data.

len: the number of bytes to write.

Returns: NA

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

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

        int size = 5;
        ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(size);

        byte b[] = { '1', '2', '3', '4' };
        int off = 0, len = 2;
        byteArrayOutputStream.write(b, off, len);
        System.out.println(byteArrayOutputStream.toString());
        byteArrayOutputStream.close();
    }
}

Output:

12


Approach 2: NullPointerException 

Java

import java.io.ByteArrayOutputStream;
import java.io.IOException;

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

        int size = 5;
        ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(size);

        byte b[] = null;
        int off = 0, len = 2;
        byteArrayOutputStream.write(b, off, len);
        System.out.println(byteArrayOutputStream.toString());
        byteArrayOutputStream.close();
    }
}

Output:

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



Approach 3: IndexOutOfBoundsException

Java

import java.io.ByteArrayOutputStream;
import java.io.IOException;

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

        int size = 5;
        ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(size);

        byte b[] = { '1', '2', '3', '4' };
        int off = -1, len = 2;
        byteArrayOutputStream.write(b, off, len);
        System.out.println(byteArrayOutputStream.toString());
        byteArrayOutputStream.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.ByteArrayOutputStream.write(ByteArrayOutputStream.java:129)


No comments:

Post a Comment