This class implements an output stream in which the data is
written into a byte array. The buffer automatically grows as data
is written to it.
The data can be retrieved using toByteArray() and
toString().
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).
buf
protected byte buf[]
The buffer where data is stored.
count
protected int count
The number of valid bytes in the buffer.
ByteArrayOutputStream
public ByteArrayOutputStream()
Creates a new byte array output stream. The buffer capacity is
initially 32 bytes, though its size increases if necessary.
ByteArrayOutputStream
public ByteArrayOutputStream(int size)
Creates a new byte array output stream, with a buffer capacity of
the specified size, in bytes.
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).
Parameters:
out - the output stream to which to write the data.
Resets the count field of this byte array output
stream to zero, so that all currently accumulated output in the
ouput stream is discarded. The output stream can be used again,
reusing the already allocated buffer space.
toByteArray
public synchronized byte[] toByteArray()
Creates a newly allocated byte array. Its size is the current
size of this output stream and the valid contents of the buffer
have been copied into it.
Returns:
the current contents of this output stream, as a byte array.
Note: toString() is deprecated.This method does not properly convert bytes into characters.
As of JDK 1.1, the preferred way to do this is via the
toString(String enc) method, which takes an encoding-name
argument, or the toString() method, which uses the
platform's default character encoding.
Creates a newly allocated string. Its size is the current size of
the output stream and the valid contents of the buffer have been
copied into it. Each character c in the resulting string is
constructed from the corresponding element b in the byte
array such that:
c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
Parameters:
hibyte - the high byte of each resulting Unicode character.
Returns:
the current contents of the output stream, as a string.