ByteArrayInputStream Class in Java

ByteArrayInputStream is an implementation of an input stream that uses a byte array as the source.

ByteArrayInputStream Constructors

  • 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.

ByteArrayInputStream Methods

  • int available() - Returns the number of remaining bytes that can be read (or skipped over) from this input stream.
  • void close() - Closing a ByteArrayInputStream has no effect.
  • void mark(int readAheadLimit) - Set the current marked position in the stream.
  • boolean markSupported() - Tests if this InputStream supports mark/reset.
  • int read() - Reads the next byte of data from this input stream.
  • int read(byte[] b, int off, int len) - Reads up to len bytes of data into an array of bytes from this input stream.
  • void reset() - Resets the buffer to the marked position.
  • long skip(long n) - Skips n bytes of input from this input stream.
The close( ) method has no effect on a ByteArrayInputStream. Therefore, it is not necessary to call close( ) on a ByteArrayInputStream, but doing so is not an error.

ByteArrayInputStream Example 1

The following example creates a pair of ByteArrayInputStreams, initializing them with the byte representation of the alphabet:
// Demonstrate ByteArrayInputStream.
import java.io.*;
class ByteArrayInputStreamDemo {
    public static void main(String args[]) {
        String tmp = "abcdefghijklmnopqrstuvwxyz";
        byte b[] = tmp.getBytes();
        ByteArrayInputStream input1 = new ByteArrayInputStream(b);
        ByteArrayInputStream input2 = new ByteArrayInputStream(b, 0, 3);
    }
}
The input1 object contains the entire lowercase alphabet, whereas input2 contains only the first three letters. A ByteArrayInputStream implements both mark( ) and reset( ).
However, if mark( ) has not been called, then reset( ) sets the stream pointer to the start of the stream—which, in this case, is the start of the byte array passed to the constructor.

ByteArrayInputStream Example 2

This example shows how to use the reset( ) method to read the same input twice. 
In this case, the program reads and prints the letters "abc" once in lowercase and then again in uppercase.
This example uses the try-with-resources statement to close the resources automatically.
import java.io.*;
class ByteArrayInputStreamReset {
    public static void main(String args[]) {
        String tmp = "abc";
        byte b[] = tmp.getBytes();
        ByteArrayInputStream in = new ByteArrayInputStream(b);
        for (int i = 0; i < 2; i++) {
            int c;
            while ((c = in .read()) != -1) {
                if (i == 0) {
                    System.out.print((char) c);
                } else {
                    System.out.print(Character.toUpperCase((char) c));
                }
            }
            System.out.println(); in .reset();
        }
    }
}
Output:
abc
ABC
This example first reads each character from the stream and prints it as-is in lowercase. It then resets the stream and begins reading again, this time converting each character to uppercase before printing.

Reference

Comments