See OutputStream class for descriptions of methods ByteArrayOutputStream uses. They are:
write( int b ) Writes a single byte, supplied as the low-order byte in an int.
write( byte[ ] ) Writes an array of bytes.
write( byte[ ], offset, len ) Writes
a portion len of a byte
array, beginning at offset
close( ) Closes the stream, flushing it first.
flush( ) Flushes all associated buffers in a stream without closing it.
plus it adds:
.toByteArray( ) all
below
.toString( )
.size( )
.writeTo(OutputStream)
.reset( )
■ ByteArrayOutputStream allows you to use write(...) methods to add data to a dynamic byte stream. You can retrieve the stream's contents in any of three ways:
(1) To retrieve
the stream's contents as a byte
array use the method toByteArray( )
(2) To retrieve them as a String use toString( )
(3) To write them out at any time to another Writer stream use the writeTo(...) method.
■ If the contents of a ByteArrayOutputStream stream are sent to a byte array using the toByteArray( ) method, a ByteArrayInputStream can then read the resulting array. See CharArrayWriter class for a complete example of this situation using the similar (but character-oriented) classes CharArrayWriter and CharArrayReader,
■ There is no void write( String s ) method under OutputStream, as there is on the character side under Writer. To write a String to an OutputStream, convert the String to a byte array and write it with the method write( byte[ ] ). i.e.
import
java.io.*;
try
{
ByteArrayOutputStream baos = new
ByteArrayOutputStream( );
String s = "Hello
World";
baos.write( s.getBytes( ) ); // convert it and write it in
the same statement
}
catch (IOException e) { }
■ ByteArrayOutputStream adds five methods to those provided by OutputStream. They are described below:
char[
] .toByteArray(
) method
■ This code returns a new byte array containing the bytes written to this stream thus far. It allocates a new array.
import
java.io.*;
import
java.util.*;
ByteArrayOutputStream
bos = new ByteArrayOutputStream( );
bos.write(
65 );
bos.write(
66 );
bos.write(
67 );
byte[
] ba = new byte [ bos.size( ) ];
ba
= bos.toByteArray( );
System.out.println(
"bos is " + bos.size( ) +
" long and contains: " + bos );
System.out.print(
"Array ba contains: ");
for
( int x = 0; x < ba.length; x++ )
System.out.print( (char) ba[ x ] );
String .toString(
) method
■ The toString( ) method, overridden from Object, returns a String of the whole ByteArrayOutputStream stream written thus far. i.e.
import
java.io.*;
try
{
ByteArrayOutputStream
baos = new ByteArrayOutputStream( );
String
s = "Hello World";
byte[
] b = s.getBytes( );
baos.write(
b );
System.out.println(
baos.toString( ) );
}
catch (IOException e) { }
■ This code returns an int containing the number of chars actually written to the ByteArrayOutputStream stream buffer thus far. If the reset( ) method is called, it resets this particular value to zero.
import
java.io.*;
try
{
ByteArrayOutputStream baos= new ByteArrayOutputStream( );
String s1 = "Hello ";
String s2 = "World";
baos.write( s1.getBytes( ) );
baos.write( s2.getBytes( ) );
int x;
x = baos.size( );
System.out.println(x + " characters
written."); //
prints 11
baos.reset( ); // reset
x = baos.size( );
System.out.println(x + " characters
written."); //
now prints 0 after reset
}
catch (IOException e) { }
void
.writeTo(OutputStream) method
■ This method writes out to any OutputStream all the chars which were written to the ByteArrayOutputStream thus far.
The snippet below demonstrates. It writes half of the alphabet, A through M, to a ByteArrayOutputStream. It then uses ByteArrayOutputStream's writeTo(...) method to write the digits 1234567890 to the PipedWriter stream after the M. It then continues writing the rest of the alphabet, N to Z, to the PipedWriter stream. The result is that 1234567890 appears in the middle of the alphabet in the PipedWriter stream. The associated PipedReader stream shows this result when it reads the PipedWriter stream for printing.
import
Java.io.*;
ByteArrayOutputStream baos = new
ByteArrayOutputStream( );
PipedInputStream pis = new PipedInputStream( );
String S = "ABCEFGHIJKLMNOPQRSTUVWXYZ";
try {
PipedOutputStream pos = new PipedOutputStream( );
pos.connect( pis );
for ( int x = 0; x < S.length( ); x++ )
{
if ( x == 13 ) {
String s2 =
"1234567890";
baos.write( s2.getBytes( ) );
}
baos.write( S.getBytes( ), x, 1 );
}
baos.writeTo( pos );
for ( int x = 0; x < 36;
x++ ) {
System.out.print( (char) pis.read( )
);
}
} catch (IOException e) { }
■ This method discards everything in the ByteArrayOutputStream stream and resets the size( ) count to zero. Using reset( ) on an existing stream, so that it can be reused, is faster and more efficient than creating a new one.