Table of Contents
ByteArrayOutputStream class in Java
The ByteArrayOutputStream class in Java writes data into an array of bytes so that we can write this byte array data into any Output stream class. It stores the byte array in a buffer for later use. This buffer is dynamic in nature and grows automatically based on the data storage.
In this tutorial, we will see in detail the various methods of the ByteArrayOutputStream class along with examples.
Constructors of Java ByteArrayOutputStream
Below are the constructors of the ByteAraryOutputStream class in Java.
Constructor | Description |
---|---|
ByteArrayOutputStream() | Creates a new ByteArrayOutputStream with default capacity as 32 |
ByteArrayOutputStream(int size) | Creates a new ByteArrayOutputStream with the specified capacity |
Methods of Java ByteArrayOutputStream
The ByteArrayOutputStream class has the below methods:
Method | Description |
---|---|
void close() | Closes the ByteArrayOutputStream |
void flush() | Flushes the data from the buffer |
void reset() | Resets the current position in the ByteArrayOutputStream |
int size() | Returns the current size of the ByteArrayOutputStream |
byte[] toByteArray() | Creates a new byte array |
String toString() | Returns the string representation of the content in the output stream |
void write(byte[] b) | Writes the specified byte array to the output stream |
void write(int b) | Writes the specified byte to the output stream |
void write(byte[] b, int off, int len) | Writes the specified byte array from the offset position until the specified length |
void writeBytes(byte[] b) | Writes the entire contents of the byte array to the output stream |
void writeTo(OutputStream out) | Writes all the contents to the specified output stream |
Java ByteArrayOutputStream Examples
Now let us see various examples using the above methods of the ByteArrayOutputStream class in Java.
Example: Write data to buffer and output stream
Below is an example of writing data to buffer using the write()
method and writing the same content to the output stream using the writeTo()
method. Hence both the buffer and the output file will contain the character ‘P’ which is equivalent to the byte integer value 80.
import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class WriteData { public static void main(String[] args) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); FileOutputStream fos = new FileOutputStream("output.txt"); bos.write(80); bos.writeTo(fos); fos.close(); System.out.println("Content written to output stream"); } }
Content written to output stream
Output.txt
Example: Write byte array using the ByteArrayOutputStream
Below is an example of directly writing an array of bytes to the ByteArrayOutputStream using the write()
method and reading its contents back by using the toString()
method.
import java.io.ByteArrayOutputStream; import java.io.IOException; public class WriteByteArray { public static void main(String[] args) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); String value = "Welcome to java tutorial"; byte[] arrBytes = value.getBytes(); bos.write(arrBytes); String text = bos.toString(); System.out.println("Content in the output stream: " + text); bos.close(); } }
Content in the output stream: Welcome to java tutorial
Example: Write byte array and read using ByteArrayInputStream
We can also convert the input into a byte array using the toByteArray()
method of the ByteArrayOutputStream class in Java. In this example, first, we write the bytes and then convert them into a byte array after which we can print each character using the for
loop.
We can read this byte array using the ByteArrayInputStream class and convert each character into an upper case.
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class ByteArray { public static void main(String[] args) { ByteArrayOutputStream bos = new ByteArrayOutputStream(16); try { bos.write("Java tutorial".getBytes()); //Convert to byte array byte[] b = bos.toByteArray(); for(int i = 0;i<b.length;i++) System.out.print((char)b[i] + " "); System.out.println(); //Reading the same input and converting to uppercase ByteArrayInputStream bis = new ByteArrayInputStream(b); int j; while((j=bis.read()) != -1) System.out.print(Character.toUpperCase((char)j) + " "); } catch (IOException e) { e.printStackTrace(); } } }
J a v a t u t o r i a l J A V A T U T O R I A L
Example: writeBytes() method of ByteArrayOutputStream
This example illustrates the use of the writeBytes()
method of the ByteArrayOutputStream class in Java that directly writes the entire byte content to the output stream.
import java.io.ByteArrayOutputStream; public class WriteBytes { public static void main(String[] args) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); String value = "Example of ByteArrayOutputStream"; bos.writeBytes(value.getBytes()); String text = bos.toString(); System.out.println(text); } }
Example of ByteArrayOutputStream
Example: Write the specific length of bytes to ByteArrayOutputStream
This example shows how to write only a specific length of bytes to ByteArrayOutputStream using the write()
method. Since the offset is 4 and the length is 15, it prints the characters within this range.
import java.io.ByteArrayOutputStream; public class WriteBytes { public static void main(String[] args) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); String value = "Example of ByteArrayOutputStream"; byte[] b = value.getBytes(); bos.write(b, 4, 15); String text = bos.toString(); System.out.println(text); } }
ple of ByteArra