dsa

Java ByteArrayInputStream Class

In this tutorial, we will learn about Java ByteArrayInputStream and its methods with the help of examples.

The ByteArrayInputStream is composed of two words: ByteArray and InputStream. As the name suggests, it can be used to read byte array as input stream.
Java ByteArrayInputStream class contains an internal buffer which is used to read byte array as stream. In this stream, the data is read from a byte array.
The buffer of ByteArrayInputStream automatically grows according to data.

The ByteArrayOutputStream is a subclass of the Java OutputStream.

Java ByteArrayInputStream class declaration

public class ByteArrayInputStream extends InputStream

Fields

Java ByteArrayInputStream class methods

Sr.No. Method & Description
1

public int read()

This method reads the next byte of data from the InputStream. Returns an int as the next byte of data. If it is the end of the file, then it returns -1.

2

public int read(byte[] r, int off, int len)

This method reads upto len number of bytes starting from off from the input stream into an array. Returns the total number of bytes read. If it is the end of the file, -1 will be returned.

3

public int available()

Gives the number of bytes that can be read from this file input stream. Returns an int that gives the number of bytes to be read.

4

public void mark(int read)

This sets the current marked position in the stream. The parameter gives the maximum limit of bytes that can be read before the marked position becomes invalid.

5

public long skip(long n)

Skips ā€˜nā€™ number of bytes from the stream. This returns the actual number of bytes skipped.