ByteArrayOutputStream class


java.lang.Object
 |
 +java.io.OutputStream
   |
   +-java.io.ByteArrayOutputStream

ByteArrayOutputStream is an implementation of OutputStream that uses a byte array as the destination. ByteArrayOutputStream has two constructors, shown here:

ByteArrayOutputStream( )
a buffer of 32 bytes is created.
ByteArrayOutputStream(int numBytes)
a numBytes-sized buffer

The buffer size will be increased automatically, if needed.

Methods from ByteArrayOutputStream

void close()
Closing a ByteArrayOutputStream has no effect.
void reset()
Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.
int size()
Returns the current size of the buffer.
byte[] toByteArray()
Creates a newly allocated byte array.
String toString()
Converts the buffer's contents into a string decoding bytes using the platform's default character set.
String toString(String charsetName)
Converts the buffer's contents into a string by decoding the bytes using the specified charsetName.
void write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
void write(int b)
Writes the specified byte to this byte array output stream.
void writeTo(OutputStream out)
Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

Revised from Open JDK source code

 
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {
  public static void main(String args[]) throws IOException {
    ByteArrayOutputStream f = new ByteArrayOutputStream();
    
    byte buf[] = "this is a test".getBytes();
    f.write(buf);
    System.out.println(f.toString());
    byte b[] = f.toByteArray();
    for (int i = 0; i < b.length; i++) {
      System.out.println((char) b[i]);
    }
    OutputStream f2 = new FileOutputStream("test.txt");
    f.writeTo(f2);
    f2.close();
    f.reset();
    for (int i = 0; i < 3; i++){
      f.write('X');
    }
    System.out.println(f.toString());
  }
}
  

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

public class Main {
  public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    outStream.write('a');
    outStream.write(("java2s.com").getBytes());
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    outStream.close();
  }
}

The output:


outstream: ajava2s.com
size: 11

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class Main {
  public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    outStream.write('a');
    outStream.write(("java2s.com").getBytes());
   
    System.out.println(Arrays.toString(outStream.toByteArray()));
    System.out.println(new String(outStream.toByteArray()));
    outStream.close();
  }
}

The output:


[97, 106, 97, 118, 97, 50, 115, 46, 99, 111, 109]
ajava2s.com
Home 
  Java Book 
    File Stream