o7planning

Java ByteArrayOutputStream Tutorial

View more Tutorials:

1- ByteArrayOutputStream

ByteArrayOutputStream is a subclass of OutputStream. True to its name, ByteArrayOutputStream is used to write bytes to a byte array that manages in the way of an OutputStream.
bytes written to ByteArrayOutputStream will be assigned to elements of the byte array that ByteArrayOutputStream manages.
When the number of bytes written to ByteArrayOutputStream is greater than the length of array, ByteArrayOutputStream will create a new array with greater length and copy the bytes from the old array.
Constructors
public ByteArrayOutputStream()  

public ByteArrayOutputStream(int initialSize)
  • ByteArrayOutputStream(int) constructor creates a ByteArrayOutputStream object with a byte array that has a specified initial size.
  • ByteArrayOutputStream() constructor creates a ByteArrayOutputStream object with a byte array of that has default size (32).
Methods of ByteArrayOutputStream
void close()  
void reset()  
int size()  

byte[] toByteArray()  
String toString()  
String toString​(String charsetName)  
String toString​(Charset charset)  

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

void writeTo​(OutputStream out)

2- Examples

ByteArrayOutputStreamEx1.java
package org.o7planning.bytearrayoutputstream.ex;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamEx1 {

    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        
        byte[] bytes1 = new byte[] {'H', 'e'};
        
        baos.write(bytes1);
        
        baos.write(108); // 'l'
        baos.write('l'); // code: 108
        baos.write('o'); // code: 111

        byte[] buffer = baos.toByteArray();
        
        for(byte b: buffer)  {
            System.out.println(b + " --> " + (char)b);
        }
    }
}
Output:
72 --> H
101 --> e
108 --> l
108 --> l
111 --> o
For example, add 2 byte arrays to create a new array.
ByteArrayOutputStreamEx3.java
package org.o7planning.bytearrayoutputstream.ex;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamEx3 {

    public static void main(String[] args) throws IOException {
        byte[] arr1 = "Hello ".getBytes();
        byte[] arr2 = new byte[] {'W', 'o', 'r', 'l', 'd', '!'};
        
        byte[] result = add(arr1, arr2);
        
        for(byte b: result)  {
            System.out.println(b + " " + (char)b);
        }
    }

    public static byte[] add(byte[] arr1, byte[] arr2) {
        if (arr1 == null) {
            return arr2;
        }
        if (arr2 == null) {
            return arr1;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            baos.write(arr1);
            baos.write(arr2);
        } catch (Exception e) {
            // Never happened!
        }
        return baos.toByteArray();
    }
}
Output:
72 H
101 e
108 l
108 l
111 o
32  
87 W
111 o
114 r
108 l
100 d
33 !
Basically, most of ByteArrayOutputStream methods inherit from OutputStream, so you can find more examples of how to use these methods in the article below:

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.