Java : ByteArrayOutputStream with Examples

ByteArrayOutputStream (Java SE 18 & JDK 18) API Examples.
You will find code examples on most ByteArrayOutputStream methods.


Summary

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().

Class diagram

final var os = new ByteArrayOutputStream();

os.write(10);
os.write(20);

final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);

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

Note : Closing a ByteArrayOutputStream has no effect.


Fields

protected byte[] buf

The buffer where data is stored.

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

protected int count

The number of valid bytes in the buffer.

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

Constructors

ByteArrayOutputStream ()

Creates a new ByteArrayOutputStream.

final var os = new ByteArrayOutputStream();

final var ret1 = os.toByteArray();
System.out.println(Arrays.toString(ret1)); // []

os.write(10);
os.write(20);

final var ret2 = os.toByteArray();
System.out.println(Arrays.toString(ret2)); // [10, 20]

final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);

final var ret3 = os.toByteArray();
System.out.println(Arrays.toString(ret3)); // [10, 20, 30, 40, 50]

ByteArrayOutputStream (int size)

Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.

class MyOutputStream extends ByteArrayOutputStream {
    MyOutputStream() {
        super();
    }

    MyOutputStream(int size) {
        super(size);
    }

    int getCapacity() {
        return buf.length;
    }
}

final var os1 = new MyOutputStream();
System.out.println(os1.getCapacity()); // 32

final var os2 = new MyOutputStream(128);
System.out.println(os2.getCapacity()); // 128

Methods

void close ()

Closing a ByteArrayOutputStream has no effect.

final var os = new ByteArrayOutputStream();

// No effect.
os.close();

os.write(10);
os.write(20);

System.out.println(Arrays.toString(os.toByteArray())); // [10, 20]

void reset ()

Resets the count field of this ByteArrayOutputStream to zero, so that all currently accumulated output in the output stream is discarded.

final var os = new ByteArrayOutputStream();

os.write(10);
os.write(20);

final var ret1 = os.toByteArray();
System.out.println(Arrays.toString(ret1)); // [10, 20]

os.reset();

final var ret2 = os.toByteArray();
System.out.println(Arrays.toString(ret2)); // []

os.write(30);
os.write(40);

final var ret3 = os.toByteArray();
System.out.println(Arrays.toString(ret3)); // [30, 40]

int size ()

Returns the current size of the buffer.

final var os = new ByteArrayOutputStream();

System.out.println(os.size()); // 0
System.out.println(Arrays.toString(os.toByteArray())); // []

os.write(10);

System.out.println(os.size()); // 1
System.out.println(Arrays.toString(os.toByteArray())); // [10]

os.write(20);

System.out.println(os.size()); // 2
System.out.println(Arrays.toString(os.toByteArray())); // [10, 20]

final byte[] bytes = {30, 40, 50};
os.writeBytes(bytes);

System.out.println(os.size()); // 5
System.out.println(Arrays.toString(os.toByteArray())); // [10, 20, 30, 40, 50]

byte[] toByteArray ()

Creates a newly allocated byte array.

Please see ByteArrayOutputStream().

String toString ()

Converts the buffer's contents into a string decoding bytes using the default charset.

final var os = new ByteArrayOutputStream();

final var bytes1 = "abcd".getBytes();
System.out.println(Arrays.toString(bytes1)); // [97, 98, 99, 100]

os.writeBytes(bytes1);

final var ret1 = os.toString();
System.out.println(ret1); // abcd

final var bytes2 = "XYZ".getBytes();
System.out.println(Arrays.toString(bytes2)); // [88, 89, 90]

os.writeBytes(bytes2);

final var ret2 = os.toString();
System.out.println(ret2); // abcdXYZ

String toString (int hibyte)

Deprecated. This method does not properly convert bytes into characters.

Deprecated.

String toString (String charsetName)

Converts the buffer's contents into a string by decoding the bytes using the named charset.

final var os = new ByteArrayOutputStream();

final var bytes = "○△□".getBytes("Shift_JIS");
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, -96]

os.writeBytes(bytes);

final var ret1 = os.toString();
System.out.println(ret1); // "������" <- Garbled characters

final var ret2 = os.toString("Shift_JIS");
System.out.println(ret2); // "○△□"

String toString (Charset charset)

Converts the buffer's contents into a string by decoding the bytes using the specified charset.

final var os = new ByteArrayOutputStream();
final var sjis = Charset.forName("Shift_JIS");

final var bytes = "○△□".getBytes(sjis);
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, -96]

os.writeBytes(bytes);

final var ret1 = os.toString();
System.out.println(ret1); // "������" <- Garbled characters

final var ret2 = os.toString(sjis);
System.out.println(ret2); // "○△□"

void write (byte[] b, int off, int len)

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream.

final var os = new ByteArrayOutputStream();
final byte[] bytes = {1, 2, 3, 4};

os.write(bytes, 0, 0);
System.out.println(Arrays.toString(os.toByteArray())); // []

os.reset();
os.write(bytes, 0, 1);
System.out.println(Arrays.toString(os.toByteArray())); // [1]

os.reset();
os.write(bytes, 0, 2);
System.out.println(Arrays.toString(os.toByteArray())); // [1, 2]

os.reset();
os.write(bytes, 0, 3);
System.out.println(Arrays.toString(os.toByteArray())); // [1, 2, 3]

os.reset();
os.write(bytes, 0, 4);
System.out.println(Arrays.toString(os.toByteArray())); // [1, 2, 3, 4]
final var os = new ByteArrayOutputStream();
final byte[] bytes = {1, 2, 3, 4};

os.reset();
os.write(bytes, 1, 3);
System.out.println(Arrays.toString(os.toByteArray())); // [2, 3, 4]

os.reset();
os.write(bytes, 2, 2);
System.out.println(Arrays.toString(os.toByteArray())); // [3, 4]

os.reset();
os.write(bytes, 3, 1);
System.out.println(Arrays.toString(os.toByteArray())); // [4]

os.reset();
os.write(bytes, 4, 0);
System.out.println(Arrays.toString(os.toByteArray())); // []

void write (int b)

Writes the specified byte to this ByteArrayOutputStream.

Please see ByteArrayOutputStream().

void writeBytes (byte[] b)

Writes the complete contents of the specified byte array to this ByteArrayOutputStream.

Please see ByteArrayOutputStream().

void writeTo (OutputStream out)

Writes the complete contents of this ByteArrayOutputStream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

final var src = new ByteArrayOutputStream();
final var dst = new ByteArrayOutputStream();

src.write(10);
src.write(20);
src.write(30);

System.out.println(Arrays.toString(src.toByteArray())); // [10, 20, 30]
System.out.println(Arrays.toString(dst.toByteArray())); // []

src.writeTo(dst);

System.out.println(Arrays.toString(src.toByteArray())); // [10, 20, 30]
System.out.println(Arrays.toString(dst.toByteArray())); // [10, 20, 30]

Methods declared in OutputStream

flush, nullOutputStream, write

Please see the link below.


Related posts

To top of page