Site icon TechVidvan

Java ByteArrayOutputStream Class with Examples

java bytearray outputstream

Java is the most widely and useful language.

An array of output data (in bytes) can be written using the Java.io package’s ByteArrayOutputStream class.
The abstract class OutputStream is extended by it.

Explanation

Declaration

public class ByteArrayOutputStream extends OutputStream

How to create:

Constructors:

Constructor Description
ByteArrayOutputStream() Generates a new output stream for the byte array, initially with a capacity of 32 bytes, but it can be expanded if needed.
ByteArrayOutputStream(int size) Generates a new output stream for a byte array with a buffer capacity of the given size, expressed in bytes.

Methods Table:

Methods Description
int size() It is employed to provide the buffer’s current size.
byte[] toByteArray() It’s employed to generate a freshly assigned byte array.
String toString() It is employed to transform the content into a string by decoding the character set by default on the platform.
String toString(String charsetName) It is employed to transform the data into a string by decoding bytes with a given charsetName.
void write(int b) It is employed to write the given byte to the output stream of the byte array.
void write(byte[] b, int off, int len It is used to write len bytes to the byte array output stream from a given byte array, beginning at the offset.
void writeTo(OutputStream out) It is used to write all of a byte array output stream’s content to the designated output stream.
void reset() It is employed to set a byte array output stream’s count field to zero.
void close() It’s employed to terminate the ByteArrayOutputStream.

Fields Tables:

Modifier Description
protected byte[] Buf

The data is stored in the buffer 

protected int Count

The buffer consists of number of valid bytes.

Example

package com.java;  
import java.io.*;  
public class TechVidvan{  
public static void main(String args[])throws Exception{    
      FileOutputStream fout1=new FileOutputStream("f1.txt");    
      FileOutputStream fout2=new FileOutputStream("f2.txt");    
        
      ByteArrayOutputStream bout=new ByteArrayOutputStream();    
      bout.write(65);    
      bout.writeTo(fout1);    
      bout.writeTo(fout2);    
        
      bout.flush();    
      bout.close();//has no effect    
      System.out.println("TechVidvan Tutorials");    
     }    
    }

Output:
TechVidvan Tutorials

f1.txt:
A
f2.txt:
A

Example:

import java.io.*;

public class TechVidvan {

    public static void main(String args[]) {
        ByteArrayOutputStream aOutput = new ByteArrayOutputStream(10);

        int targetSize = 10;

        while (aOutput.size() < targetSize) {
            aOutput.write("TechVidvan".getBytes(), 0, 10);
        }

        byte a[] = aOutput.toByteArray();
        System.out.println("Printing the content");

        for (int x = 0; x < a.length; x++) {
            System.out.print((char) a[x] + "   ");
        }
        System.out.println("  ");

        int c;
        ByteArrayInputStream aInput = new ByteArrayInputStream(a);
        System.out.println("Converting into Upper case ");

        for (int y = 0; y < 1; y++) {
            while ((c = aInput.read()) != -1) {
                System.out.println(Character.toUpperCase((char) c));
            }
            aInput.reset();
        }
    }
}

Output:
Printing the content
T e c h V i d v a n
Converting into Upper case
T
E
C
H
V
I
D
V
A
N

Fields:

Access Data from Java ByteArrayOutputStream

Methods of Java ByteArrayOutputStream:

Difference between ByteArrayOutputStream and FioeOutputStream:

Uses:

Benefits of Java ByteArrayOutputStream:

Conclusion

The ByteArrayOutputStream used in various output data stream in Java. We have undergone many purposes and examples of it.

Exit mobile version