Skip to main content

ByteArrayInputStream class in java is a class in the Java programming language that provides an implementation of an input stream that reads data from an array of bytes. This means that it allows you to treat a byte array as a stream of bytes that can be read from sequentially.

The constructor of ByteArrayInputStream takes a byte array as an argument and creates a new input stream that reads from this array. Once the input stream is created, you can use the methods provided by the InputStream class to read data from the byte array. For example, you can use the read() method to read a single byte from the array, or the read(byte[] b, int off, int len) method to read a block of bytes from the array into a buffer.

ByteArrayInputStream is often used in Java when you need to read data from an in-memory buffer, such as when working with network protocols or when reading data from a file that has been loaded into memory. Because it implements the InputStream interface, it can be used in combination with other Java I/O classes and methods to perform various input operations.

Inside of this class exists 8 public methods. Declaration of this class as follows.

public class 

ByteArrayInputStream public method

There are following useful methods which is define the inside of class.

int available() It returns the number of remaining bytes that can be read (or skipped over) from this input stream.
void close() It closing a ByteArrayInputStream has no effect.
void mark(int readAheadLimit) It set the current marked position in the stream.
boolean markSupported() It tests if this supports mark/reset.
int read() It reads the next byte of data from this input stream.
int read(byte[] b, int off, int len) It reads up to bytes of data into an array of bytes from this input stream.
void reset() It resets the buffer to the marked position.
long skip(long n) It skips bytes of input from this input stream.

This reference is belong to javadoc

Public Constructors

There is 2 types of public constructor is defined inside the class, Whose syntax as follows.


Example of those constructors.

public class Example
    public static void main(String[] argv)
        byte[] data = {
            97 , 98 , 99 , 32 , 65 , 66 , 67
        // ByteArrayInputStream(byte[] buf)
        ByteArrayInputStream record1 = new ByteArrayInputStream(data);
        // ByteArrayInputStream(byte[] buf, int offset, int length)
        ByteArrayInputStream record2 = new ByteArrayInputStream(data, 2, 3);
        // print byte to char
        int info =;
        while (info != -1)
            System.out.print((char) info);
            info =;
        // print byte to char
        info =;
        while (info != -1)
            System.out.print((char) info);
            info =;
Instance of class in java
abc ABC
c A


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment