Java ByteArrayOutputStream Example
In this example we will discuss about ByteArrayOutputStream
class and its usage. 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()
.
ByteArrayOutputStream
extends OutputStream
, the abstract class which is the superclass of all classes representing an output stream of bytes.
The ByteArrayOutputStream
exists since JDK1.0.
The structure of ByteArrayOutputStream
Constructor:
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.
The ByteArrayOutputStream in Java
To see a basic usage of ByteArrayOutputStream
, create a class called SimpleByteArrayOutputStreamExample
with the following source code:
SimpleByteArrayOutputStreamExample.java
package com.javacodegeeks.examples; import java.io.ByteArrayOutputStream; public class SimpleByteArrayOutputStreamExample { public static void main(String[] args) { ByteArrayOutputStream bout = new ByteArrayOutputStream(); for (int i=0;i<10;i++) { bout.write((byte) (Math.random() * 100)); } byte[] byteArray = bout.toByteArray(); for (byte b : byteArray) System.out.print(b+" "); } }
In this example I created an instance of ByteArrayOutputStream
and wrote 10 random bytes into it.
After that, I turned the ByteArrayOutputStream
instance into a byte array, using the toByteArray()
method, and then printed every byte using a foreach
loop.
The output is this:
98 64 23 49 55 87 53 59 37 59
Another usage of ByteArrayOutputStream
There is another implmentation of the write()
method used above, the write(byte[] b, int off,int len)
method. This method writes len
bytes from the specified byte array starting at offset off
to this byte array output stream.
To see this, create a class called AnotherByteArrayOutputStreamExample
with this source code:
SimpleByteArrayOutputStreamExample.java
package com.javacodegeeks.examples; import java.io.ByteArrayOutputStream; public class AnotherByteArrayOutputStreamExample { public static void main(String[] args) { ByteArrayOutputStream bout = new ByteArrayOutputStream(); for (int i=0;i<10;i++) { bout.write((byte) (Math.random() * 100)); } byte[] byteArray = bout.toByteArray(); System.out.println("The original array:"); for (byte b : byteArray) System.out.print(b+" "); bout.reset(); bout.write(byteArray,4,4); System.out.println("\nThe new byte array:"); for (byte b : bout.toByteArray()) System.out.print(b+" "); } }
This example is actually the first one with some changes. After printing the byte array (as in the first example), I use the reset()
method to reset the ByteArrayOutputStream
instance. Then, I use the write()
method to write four of the elements of byteArray
, starting from the index 4 (which is the offset).
The output is this:
The original array: 57 3 45 91 91 84 70 1 41 69 The new byte array: 91 84 70 1
More about ByteArrayOutputStream in Java
The ByteArrayOutputStream
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()
.
Closing a ByteArrayOutputStream
has no effect. The methods in this class can be called after the stream has been closed without generating an IOException
.
Download Code
You can download the full source code of this example here : ByteArrayOutputStreamExample