Home » Core Java » io » ByteArrayOutputStream » Java ByteArrayOutputStream Example

About Aldo Ziflaj

Aldo Ziflaj
Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.

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

Download
You can download the full source code of this example here : ByteArrayOutputStreamExample
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of