java.io
Class ByteArrayOutputStream

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

public class ByteArrayOutputStream
extends OutputStream

This class implements a buffer that can be used as an OutputStream. The buffer automatically grows when data is written to the stream. The data can be retrieved using toByteArray() and toString().


Field Summary
protected  byte[] buf
          The buffer where data is stored.
protected  int count
          The number of bytes in the buffer.
 
Constructor Summary
ByteArrayOutputStream()
          Creates a new ByteArrayOutputStream.
ByteArrayOutputStream(int size)
          Creates a new ByteArrayOutputStream with the specified initial size.
 
Method Summary
 void reset()
          Resets the buffer so that you can use it again without throwing away the already allocated buffer.
 int size()
          Returns the current size of the buffer.
 byte[] toByteArray()
          Returns a copy of the input data.
 String toString()
          Converts input data to a string.
 String toString(int hibyte)
          Converts input data to a string.
 void write(byte[] b, int off, int len)
          Writes bytes to the buffer.
 void write(int b)
          Writes a byte to the buffer.
 void writeTo(OutputStream out)
          Writes the contents of the buffer to another stream.
 
Methods inherited from class java.io.OutputStream
close, flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

protected byte[] buf
The buffer where data is stored.


count

protected int count
The number of bytes in the buffer.

Constructor Detail

ByteArrayOutputStream

public ByteArrayOutputStream()
Creates a new ByteArrayOutputStream.


ByteArrayOutputStream

public ByteArrayOutputStream(int size)
Creates a new ByteArrayOutputStream with the specified initial size.

Parameters:
size - the initial size
Method Detail

write

public void write(int b)
Writes a byte to the buffer.

Specified by:
write in class OutputStream
Parameters:
b - the byte

write

public void write(byte[] b,
                  int off,
                  int len)
Writes bytes to the buffer.

Overrides:
write in class OutputStream
Parameters:
b - the data to be written
off - the start offset in the data
len - the number of bytes that are written

writeTo

public void writeTo(OutputStream out)
             throws IOException
Writes the contents of the buffer to another stream.

Parameters:
out - the output stream to write to
IOException

reset

public void reset()
Resets the buffer so that you can use it again without throwing away the already allocated buffer.


toByteArray

public byte[] toByteArray()
Returns a copy of the input data.


size

public int size()
Returns the current size of the buffer.


toString

public String toString()
Converts input data to a string.

Overrides:
toString in class Object
Returns:
the string.

toString

public String toString(int hibyte)
Converts input data to a string. The top 8 bits of each 16 bit Unicode character are set to hibyte.

Parameters:
hibyte - the bits set