Java ByteArrayOutputStream Example

In this section we will discuss about the Java IO Byte Streams ByteArrayOutputStream.

Java ByteArrayOutputStream Example

In this section we will discuss about the Java IO Byte Streams ByteArrayOutputStream.

Java ByteArrayOutputStream Example

Java ByteArrayOutputStream Example

In this section we will discuss about the Java IO Byte Streams ByteArrayOutputStream.

ByteArrayOutputStream is the subclass of OutputStream which is created for writing the data into a byte array. This class has internal buffer which increases automatically as the data written into it. toByteArray() method is used to create a new byte array, and the toString() method is used to get the buffer's content as string. The close() method does not put any effect on the ByteArrayOutputStream, methods can be called after closing of the ByteArrayOutputStream.

Constructors

Constructor Name Description
ByteArrayOutputStream() This constructor is used to create a new byte array output stream with the initial buffer size of 32 bytes, size of this byte array output stream can be increases if the size of data written into it is increases.
public ByteArrayOutputStream()
ByteArrayOutputStream(int size) This constructor is used to create a new byte array output stream with the specified length of buffer size in bytes.
public ByteArrayOutputStream(int size)

Commonly used methods of ByteArrayOutputStream

Method Name Description
close() This method is used to close the ByteArrayOutputStream.
Syntax : public void close() throws IOException
reset() This method is used to reset the counter i.e the count field of current ByteArrayOutputStream to zero.
Syntax : public void reset()
size() This method is used to get the current size of buffer.
Syntax : public int size()
toByteArray() This method is used create a new byte array.
Syntax : public byte[] toByteArray()
toString() This method is used to get the buffer's content as String.
Syntax : public String toString()
write(byte[] b, int off, int len) This method is used to write the specified length of byte from the specified byte array started from the specified offset.
Syntax : public void write(byte[] b, int off, int len)
write(int b) This method is used to write a single byte the current byte array output stream.
Syntax : public void write(int b)
writeTo(OutputStream out) This method is used to write the all contents of byte array to the output stream given as argument.
Syntax : public void writeTo(OutputStream out) throws IOException

Example :

Here an example is being given which will demonstrate you about how to write a byte to the byte array output stream at a time. In this example I have created a class named WriteByteStreams which contains the main method. Using this class I have tried to take an input of string as a command line argument. Then created an object of ByteArrayOutputStream and called some commonly used methods of this class. Source Code of this example is given below :

WriteByteStream.java

/* This example demonstrate that how a byte 
stream can be written to the byte array 
output stream one at a time*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class WriteByteStream
{
public static void main(String args[])
{
ByteArrayOutputStream baos = null;
int bufsize;
String bufferStr;
try
{
baos = new ByteArrayOutputStream();
byte[] b;
String str = args[0];
b = str.getBytes();
baos.write(b);
System.out.println("Input String is : " + baos.toString()); 
System.out.println("Number of characters in '"+ str + "' is : " + baos.size()); 
byte[] bufArray = baos.toByteArray();
System.out.println("Converted String to a byte array : ");
for(int i=0; i<bufArray.length; i++)
{
System.out.print((char)bufArray[i]);
System.out.print(",");
}
System.out.println();
baos.reset();
bufsize = baos.size();
System.out.println("Size after reset: " + bufsize);
}
catch(IOException e)
{
System.out.println("IOException caught..!!");
e.printStackTrace();
}
}
}

When you will execute the above example you will have to provide an input through the command line as follows :

After providing the value when you will press the enter key you will get the output as follows :

Download Source Code