ByteArrayOutputStream Class in Java

ByteArrayOutputStream is an implementation of an output stream that uses a byte array as the destination.

ByteArrayOutputStream Constructors

  • ByteArrayOutputStream() - Creates a new byte array output stream.
  • ByteArrayOutputStream(int size) - Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
In the first form of constructor, a buffer of 32 bytes is created. In the second, a buffer is created with a size equal to that specified by numBytes.
The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().
Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

ByteArrayOutputStream Methods

  • void close() - Closing a ByteArrayOutputStream has no effect.
  • void reset() - Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.
  • int size() - Returns the current size of the buffer.
  • byte[] toByteArray() - Creates a newly allocated byte array.
  • String toString() - Converts the buffer's contents into a string decoding bytes using the platform's default character set.
  • String toString(String charsetName) - Converts the buffer's contents into a string by decoding the bytes using the named charset.
  • void write(byte[] b, int off, int len) - Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
  • void write(int b) - Writes the specified byte to this byte array output stream.
  • void writeTo(OutputStream out) - Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

ByteArrayOutputStream Example

Let's write an example to demonstrate usage of ByteArrayOutputStream class. This program uses try-with-resources. It requires JDK 7 or later.
class ByteArrayOutputStreamDemo {
    public static void main(String args[]) {
        ByteArrayOutputStream f = new ByteArrayOutputStream();
        String s = "This should end up in the array";
        byte buf[] = s.getBytes();
        try {
        } catch (IOException e) {
            System.out.println("Error Writing to Buffer");
        System.out.println("Buffer as a string");
        System.out.println("Into array");
        byte b[] = f.toByteArray();
        for (int i = 0; i < b.length; i++) System.out.print((char) b[i]);
        System.out.println("\nTo an OutputStream()");
        // Use try-with-resources to manage the file stream.
        try (FileOutputStream f2 = new FileOutputStream("test.txt")) {
        } catch (IOException e) {
            System.out.println("I/O Error: " + e);

        System.out.println("Doing a reset");
        for (int i = 0; i\ < 3; i++) f.write('X');
When you run the program, you will see the following output. Notice how after the call to reset( ), the three X’s end up at the beginning.
Buffer as a string
This should end up in the array
Into array
This should end up in the array
To an OutputStream()
Doing a reset


Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course