The data can be retrieved using toByteArray() and
toString().
Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
This is an alternative implementation of the java.io.ByteArrayOutputStream class. The original implementation only allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to the original it doesn't reallocate the whole memory block but allocates additional buffers. This way no buffers need to be garbage collected and the contents don't have to be copied to the new buffer. This class is designed to behave exactly like the original. The only exception is the deprecated toString(int) method that has been ignored.
| Modifier and Type | Field and Description |
|---|---|
| private List | buffers
The list of buffers, which grows and never reduces. |
| private int | count
The total count of bytes written. |
| private byte[] | currentBuffer
The current buffer. |
| private int | currentBufferIndex
The index of the current buffer. |
| private static final byte[] | EMPTY_BYTE_ARRAY
A singleton empty byte array. |
| private int | filledBufferSum
The total count of bytes in all the filled buffers. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ByteArrayOutputStream(int
the initial size size)Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes. |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides java. Implements java. |
| private byte[] | Returns: the bufferthe index of the buffer required index)Return the appropriate |
| private void | needNewBuffer(int
the size of the buffer if one is created newcount)Makes a new buffer available either by allocating a new one or re-cycling an existing one. |
| public synchronized void | |
| public synchronized int | |
| public synchronized byte[] | Returns: the current contents of this output stream, as a byte arrayGets the curent contents of this byte stream as a byte array. |
| public String | toString()
Overrides java. |
| public String | |
| public void | write(byte[]
the data. b, int the start offset in the data. off, int the number of bytes to write. len)Overrides java. len bytes from the specified byte array
starting at offset off to this output stream.
|
| public synchronized void | write(int
the b)byte.Implements abstract java. |
| public synchronized void | writeTo(OutputStream
the output stream to write to out)Writes the entire contents of this byte stream to the specified output stream. |
| buffers | back to summary |
|---|---|
| private List<E> buffers The list of buffers, which grows and never reduces. | |
| count | back to summary |
|---|---|
| private int count The total count of bytes written. | |
| currentBuffer | back to summary |
|---|---|
| private byte[] currentBuffer The current buffer. | |
| currentBufferIndex | back to summary |
|---|---|
| private int currentBufferIndex The index of the current buffer. | |
| EMPTY_BYTE_ARRAY | back to summary |
|---|---|
| private static final byte[] EMPTY_BYTE_ARRAY A singleton empty byte array. | |
| filledBufferSum | back to summary |
|---|---|
| private int filledBufferSum The total count of bytes in all the filled buffers. | |
| ByteArrayOutputStream | back to summary |
|---|---|
| public ByteArrayOutputStream() Creates a new byte array output stream. The buffer capacity is initially 1024 bytes, though its size increases if necessary. | |
| ByteArrayOutputStream | back to summary |
|---|---|
| public ByteArrayOutputStream(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
| |
| getBuffer | back to summary |
|---|---|
| private byte[] getBuffer(int index) Return the appropriate
| |
| needNewBuffer | back to summary |
|---|---|
| private void needNewBuffer(int newcount) Makes a new buffer available either by allocating a new one or re-cycling an existing one.
| |
| reset | back to summary |
|---|---|
| public synchronized void reset() | |
| size | back to summary |
|---|---|
public synchronized int size()
| |
| toByteArray | back to summary |
|---|---|
| public synchronized byte[] toByteArray() Gets the curent contents of this byte stream as a byte array. The result is independent of this stream.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Gets the curent contents of this byte stream as a string.
| |
| toString | back to summary |
|---|---|
| public String toString(String enc) throws UnsupportedEncodingException Gets the curent contents of this byte stream as a string using the specified encoding.
| |
| write | back to summary |
|---|---|
| public void write(byte[] b, int off, int len) Overrides java. Doc from java. Writes
If
If
| |
| write | back to summary |
|---|---|
| public synchronized void write(int b) Implements abstract java. Doc from java. Writes the specified byte to this output stream. The general
contract for
| |
| writeTo | back to summary |
|---|---|
| public synchronized void writeTo(OutputStream out) throws IOException Writes the entire contents of this byte stream to the specified output stream.
| |