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.
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 final 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. |
| pack-priv static final int | |
| 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(final 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. Closing a |
| private void | needNewBuffer(final 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 byte[] | Returns: the current contents of this output stream, as a byte arrayGets the current contents of this byte stream as a byte array. |
| public void | write(final byte[]
the bytes to write b, final int The start offset off, final int The number of bytes to write len)Overrides java. Write the bytes to byte array. |
| public synchronized void | write(final int
the byte to write b)Implements abstract java. Write a byte to byte array. |
| public synchronized void | writeTo(final 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 final List<byte[]> 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. | |
| DEFAULT_SIZE | back to summary |
|---|---|
| pack-priv static final int DEFAULT_SIZE | |
| 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(final 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
| |
| needNewBuffer | back to summary |
|---|---|
| private void needNewBuffer(final int newcount) Makes a new buffer available either by allocating a new one or re-cycling an existing one.
| |
| toByteArray | back to summary |
|---|---|
| public synchronized byte[] toByteArray() Gets the current contents of this byte stream as a byte array. The result is independent of this stream.
| |
| write | back to summary |
|---|---|
| public void write(final byte[] b, final int off, final int len) Overrides java. Write the bytes to byte array.
| |
| write | back to summary |
|---|---|
| public synchronized void write(final int b) Implements abstract java. Write a byte to byte array.
| |
| writeTo | back to summary |
|---|---|
| public synchronized void writeTo(final OutputStream out) throws IOException Writes the entire contents of this byte stream to the specified output stream.
| |