Java : ByteArrayInputStream with Examples

ByteArrayInputStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most ByteArrayInputStream methods.


Summary

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

Class diagram

Note : Closing a ByteArrayInputStream has no effect.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

System.out.println(is.read()); // 10
System.out.println(is.read()); // 20

System.out.println(Arrays.toString(is.readAllBytes())); // [30, 40, 50]

Fields

protected byte[] buf

An array of bytes that was provided by the creator of the stream.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected int count

The index one greater than the last valid byte in the input stream buffer.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected int mark

The currently marked position in the stream.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected int pos

The index of the next byte to read from the input stream buffer.

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

Constructors

ByteArrayInputStream (byte[] buf)

Creates a ByteArrayInputStream so that it uses buf as its buffer array.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

System.out.println(is.read()); // 10
System.out.println(is.read()); // 20

System.out.println(Arrays.toString(is.readAllBytes())); // [30, 40, 50]

ByteArrayInputStream (byte[] buf, int offset, int length)

Creates ByteArrayInputStream that uses buf as its buffer array.

final byte[] buf = {10, 20, 30, 40};

final var is1 = new ByteArrayInputStream(buf, 0, 0);
System.out.println(Arrays.toString(is1.readAllBytes())); // []

final var is2 = new ByteArrayInputStream(buf, 0, 1);
System.out.println(Arrays.toString(is2.readAllBytes())); // [10]

final var is3 = new ByteArrayInputStream(buf, 0, 2);
System.out.println(Arrays.toString(is3.readAllBytes())); // [10, 20]

final var is4 = new ByteArrayInputStream(buf, 0, 3);
System.out.println(Arrays.toString(is4.readAllBytes())); // [10, 20, 30]

final var is5 = new ByteArrayInputStream(buf, 0, 4);
System.out.println(Arrays.toString(is5.readAllBytes())); // [10, 20, 30, 40]

final var is6 = new ByteArrayInputStream(buf, 0, 5);
System.out.println(Arrays.toString(is6.readAllBytes())); // [10, 20, 30, 40]
final byte[] buf = {10, 20, 30, 40};

final var is1 = new ByteArrayInputStream(buf, 0, 4);
System.out.println(Arrays.toString(is1.readAllBytes())); // [10, 20, 30, 40]

final var is2 = new ByteArrayInputStream(buf, 1, 3);
System.out.println(Arrays.toString(is2.readAllBytes())); // [20, 30, 40]

final var is3 = new ByteArrayInputStream(buf, 2, 2);
System.out.println(Arrays.toString(is3.readAllBytes())); // [30, 40]

final var is4 = new ByteArrayInputStream(buf, 3, 1);
System.out.println(Arrays.toString(is4.readAllBytes())); // [40]

final var is5 = new ByteArrayInputStream(buf, 4, 0);
System.out.println(Arrays.toString(is5.readAllBytes())); // []

Methods

int available ()

Returns the number of remaining bytes that can be read (or skipped over) from this input stream.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

System.out.println(is.available()); // 5
System.out.println(is.read()); // 10

System.out.println(is.available()); // 4
System.out.println(is.read()); // 20

System.out.println(is.available()); // 3
System.out.println(is.read()); // 30

System.out.println(is.available()); // 2
System.out.println(is.read()); // 40

System.out.println(is.available()); // 1
System.out.println(is.read()); // 50

System.out.println(is.available()); // 0
System.out.println(is.read()); // -1

void close ()

Closing a ByteArrayInputStream has no effect.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

// Closing a stream has no effect.
is.close();

// The methods in this class can be called after the stream has been closed.
System.out.println(Arrays.toString(is.readAllBytes())); // [10, 20, 30, 40, 50]

void mark (int readAheadLimit)

Set the current marked position in the stream.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30

// Note: The readAheadLimit for this class has no meaning.
is.mark(0);

System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1

is.reset();

System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1

boolean markSupported ()

Tests if this InputStream supports mark/reset.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);
System.out.println(is.markSupported()); // true

int read ()

Reads the next byte of data from this input stream.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30
System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1

int read (byte[] b, int off, int len)

Reads up to len bytes of data into an array of bytes from this input stream.

final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];

final var is = new ByteArrayInputStream(buf);

System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

System.out.println(is.read(b, 0, 10)); // 5
System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
final byte[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];

final var is = new ByteArrayInputStream(buf);

System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]

System.out.println(is.read(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [10, 0, 0, 0, 0]

System.out.println(is.read(b, 0, 2)); // 2
System.out.println(Arrays.toString(b)); // [20, 30, 0, 0, 0]

System.out.println(is.read(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [40, 50, 60, 0, 0]

System.out.println(is.read(b, 0, 4)); // 1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]

System.out.println(is.read(b, 0, 5)); // -1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];

final var is = new ByteArrayInputStream(buf);

System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]

System.out.println(is.read(b, 4, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 10]

System.out.println(is.read(b, 3, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 20, 10]

System.out.println(is.read(b, 2, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 30, 20, 10]

System.out.println(is.read(b, 1, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 40, 30, 20, 10]

System.out.println(is.read(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [50, 40, 30, 20, 10]
final byte[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];

final var is = new ByteArrayInputStream(buf);

System.out.println(Arrays.toString(b)); // [0, 0, 0]

System.out.println(is.read(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [10, 20, 30]

//is.read(b, 0, 4); // IndexOutOfBoundsException
//is.read(b, 3, 1); // IndexOutOfBoundsException

byte[] readAllBytes ()

Reads all remaining bytes from the input stream.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

final var ret = is.readAllBytes();
System.out.println(Arrays.toString(ret)); // [10, 20, 30, 40, 50]

int readNBytes (byte[] b, int off, int len)

Reads the requested number of bytes from the input stream into the given byte array.

final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[10];

final var is = new ByteArrayInputStream(buf);

System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

System.out.println(is.readNBytes(b, 0, 10)); // 5
System.out.println(Arrays.toString(b)); // [10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
final byte[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[5];

final var is = new ByteArrayInputStream(buf);

System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]

System.out.println(is.readNBytes(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [10, 0, 0, 0, 0]

System.out.println(is.readNBytes(b, 0, 2)); // 2
System.out.println(Arrays.toString(b)); // [20, 30, 0, 0, 0]

System.out.println(is.readNBytes(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [40, 50, 60, 0, 0]

System.out.println(is.readNBytes(b, 0, 4)); // 1
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]

System.out.println(is.readNBytes(b, 0, 5)); // 0
System.out.println(Arrays.toString(b)); // [70, 50, 60, 0, 0]
final byte[] buf = {10, 20, 30, 40, 50};
final byte[] b = new byte[5];

final var is = new ByteArrayInputStream(buf);

System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 0]

System.out.println(is.readNBytes(b, 4, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 0, 10]

System.out.println(is.readNBytes(b, 3, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 0, 20, 10]

System.out.println(is.readNBytes(b, 2, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 0, 30, 20, 10]

System.out.println(is.readNBytes(b, 1, 1)); // 1
System.out.println(Arrays.toString(b)); // [0, 40, 30, 20, 10]

System.out.println(is.readNBytes(b, 0, 1)); // 1
System.out.println(Arrays.toString(b)); // [50, 40, 30, 20, 10]
final byte[] buf = {10, 20, 30, 40, 50, 60, 70};
final byte[] b = new byte[3];

final var is = new ByteArrayInputStream(buf);

System.out.println(Arrays.toString(b)); // [0, 0, 0]

System.out.println(is.readNBytes(b, 0, 3)); // 3
System.out.println(Arrays.toString(b)); // [10, 20, 30]

//is.readNBytes(b, 0, 4); // IndexOutOfBoundsException
//is.readNBytes(b, 3, 1); // IndexOutOfBoundsException

void reset ()

Resets the buffer to the marked position.

final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

System.out.println(is.read()); // 10
System.out.println(is.read()); // 20
System.out.println(is.read()); // 30

// Note: The readAheadLimit for this class has no meaning.
is.mark(0);

System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1

is.reset();

System.out.println(is.read()); // 40
System.out.println(is.read()); // 50
System.out.println(is.read()); // -1

long skip (long n)

Skips n bytes of input from this input stream.

final byte[] buf = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};

final var is = new ByteArrayInputStream(buf);

System.out.println(is.read()); // 10

System.out.println(is.skip(1)); // 1
System.out.println(is.read()); // 30

System.out.println(is.skip(2)); // 2
System.out.println(is.read()); // 60

System.out.println(is.skip(3)); // 3
System.out.println(is.read()); // 100

System.out.println(is.skip(1)); // 0
System.out.println(is.read()); // -1
final byte[] buf = {10, 20, 30, 40, 50};

final var is = new ByteArrayInputStream(buf);

System.out.println(is.read()); // 10

System.out.println(is.skip(10)); // 4
System.out.println(is.read()); // -1

System.out.println(is.skip(1)); // 0
System.out.println(is.read()); // -1

Methods declared in InputStream

nullInputStream, read, readNBytes, skipNBytes, transferTo

Please see the link below.


Related posts

To top of page