Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท I/O and Files

ByteArrayInputStream and ByteArrayOutputStream

Reference for ByteArrayInputStream and ByteArrayOutputStream: in-memory byte streams for testing, capturing output and building binary data.

ByteArrayInputStream reads from a byte array you already have, and ByteArrayOutputStream collects everything written to it in a growing internal array. They turn memory into a stream, which is handy when an API demands an InputStream or OutputStream but your data lives in a byte[].

Typical uses are unit tests (feed input, capture output), assembling a binary message before sending it, and redirecting System.out temporarily. Neither class holds an operating-system resource, so closing them has no effect, but it is harmless.

Java
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
buffer.write("data".getBytes(StandardCharsets.UTF_8));
byte[] bytes = buffer.toByteArray();

Try it

Java Writing to memory and reading it back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Capturing System.out in a test

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class ByteArrayInputStream extends InputStream
ByteArrayInputStream(byte[] buf)
ByteArrayInputStream(byte[] buf, int offset, int length)

public class ByteArrayOutputStream extends OutputStream
ByteArrayOutputStream()
ByteArrayOutputStream(int size)
Member Description
ByteArrayOutputStream.toByteArray() A fresh copy of the bytes written so far.
toString() / toString(Charset) Decodes the content. The no-argument form uses the default charset; pass a charset (Java 10+).
size() Number of bytes written.
reset() Sets the count back to zero.
writeBytes(byte[]) Like write(byte[]) without IOException (Java 11+).
writeTo(OutputStream) Copies the content to another stream.
ByteArrayInputStream.available() Exact number of unread bytes.
mark(int) / reset() Supported; the read limit is ignored.

Throws: these classes do not throw IOException from read or write(int); read returns -1 at the end.

Usage notes

  • The input stream does not copy the array. If you change the array later, the stream sees the change.
  • toByteArray() copies every time, so calling it in a loop on a large buffer is expensive. writeTo avoids the copy.
  • Memory grows without bound: everything stays on the heap, so do not use these for gigabytes. The maximum size is about 2 GB (OutOfMemoryError).
  • Methods are synchronized, so a single stream is safe to share across threads, although at some cost.
  • Closing has no effect: you can keep using the stream after close().
  • Decode with a charset. toString() without an argument uses the default charset.
  • Redirecting System.out changes it for the whole JVM; restore it in finally, as shown.
  • For text in memory, StringReader and StringWriter are the character equivalents.