ByteArrayInputStream readAllBytes() in Java

readAllBytes(): This method is available in the java.io.ByteArrayInputStream class of Java.

Syntax:

byte[] java.io.ByteArrayInputStream.readAllBytes()

This method reads all remaining bytes from the input stream. This method blocks until all remaining bytes have been read and the end of the stream is detected, or an exception is thrown.

Note: This method does not close the input stream.

Parameters: NA

Returns: a byte array containing the bytes read from this input stream.

Exceptions: NA

Approach

Java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;

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

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

        System.out.println(Arrays.toString(
byteArrayInputStream.readAllBytes()));
        byteArrayInputStream.close();
    }

}

Output:

[1, 2, 3, 4]


No comments:

Post a Comment