Topics Index

Input/Output: Exploring > Byte Streams >
Siva Nookala - 20 Feb 2017
ByteArrayInputStream is used to get bytes in a streamed way from a byte array. ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method.
Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

public class ByteArrayInputStream
extends InputStream
Constructors :
Constructors Description
ByteArrayInputStream(byte[] buf) Creates a ByteArrayInputStream so that it uses buf as its buffer array.
ByteArrayInputStream(byte[] buf, int offset, int length) Creates ByteArrayInputStream that uses buf as its buffer array.
Important Methods :
Method Description
int available() Returns the number of remaining bytes that can be read (or skipped over) from this input stream.
int read() Reads the next byte of data from this input stream.
boolean markSupported() Tests if this InputStream supports mark/reset.
void mark(int readlimit) Set the current marked position in the stream.
void reset() Resets the buffer to the marked position.
void close() Closing a ByteArrayInputStream has no effect.

class ByteArrayInputStreamDemo
    public static void main(String arg[])
        String s = "I-LOVE-JAVA";
        byte b[] = s.getBytes();
        ByteArrayInputStream bais = new ByteArrayInputStream(b); // LINE A
        System.out.println("Available Bytes : " + bais.available()); // LINE B
        System.out.println("MarksSupported : " + bais.markSupported()); // LINE C
        //reading bytes from bais
        //marking the position
        bais.mark(1); // LINE D
        //closing ByteArrayInputStream
        bais.close(); // LINE E
        //reset invoked
        System.out.println("Reset method invoked.");
        bais.reset(); // LINE F
        int c = 0;
        while((c = != -1)
        System.out.print((char) c);

Available Bytes : 11
MarksSupported : true
Reset method invoked.


In the above program we have created a string I LOVE JAVA and change to byte array using getBytes() method.
At LINE Awe created a ByteArrayInputStream object and passed byte array b. At LINE B we are checking the available bytes in b. At LINE C we are checking whether bais is markSupported. At LINE D we marked a position in bais and set number of characters to be remembered. At LINE E we are closing the stream which doesn't show any effect. At LINE F we are invoking the reset method which will reset bytes to the mark position.

  • Comment LINE D and see the output. The reset method resets bytes to the starting position of the byte array.
  • Remember the close method will not show any effect on ByteArrayInputStrem.Comment LINE E and you will find no difference in output.
Rest Of Methods :
Method Description
int read(byte[] b, int off, int len) Reads up to len bytes of data into an array of bytes from this input stream.
long skip(long n) Skips n bytes of input from this input stream.

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App