java.io.ByteArrayOutputStream . Accumulates output in a byte array that automatically grows as needed.
Data is retrieved using toByteArray(),
toByteArrayInputStream(), toString() and
toString(encoding).
Closing a ClosableByteArrayOutputStream prevents
further write operations, but all other operations may succeed until after
the first invocation of free().
Freeing a ClosableByteArrayOutputStream closes the stream and
releases the internal buffer, preventing successful invocation of all
operations, with the exception of size(), close(),
isClosed(), free() and isFreed().
This class is especially useful when an accumulating output stream must be
handed off to an external client under contract that the stream should
exhibit true Closable behaviour in response both to internally tracked
events and to client invocation of the OutputStream.close() method.
| Modifier and Type | Field and Description |
|---|---|
| protected byte[] | buf
Data buffer. |
| protected boolean | closed
Whether this stream is closed. |
| protected int | count
# of valid bytes in buffer. |
| protected boolean | freed
Whether this stream is freed. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ClosableByteArrayOutputStream(int
the initial size. size)Creates a new output stream with a buffer capacity of the specified
|
| Modifier and Type | Method and Description |
|---|---|
| public synchronized int | Returns: the length of the internal data arrayReturns the current capacity of this stream's data buffer. |
| protected synchronized void | |
| protected synchronized void | |
| public synchronized void | close()
Overrides java. Implements java. Closes this object for further writing. |
| protected byte[] | Returns: copy oforiginal with the given newLengththe object to copy original, int the length of the copy newLength)Retrieves a copy of |
| public void | flush()
Overrides java. Implements java. By default, does nothing. |
| public synchronized void | |
| public synchronized boolean | Returns: true if this stream is closed, else falseRetrieves whether this stream is closed. |
| public synchronized boolean | |
| public synchronized void | reset()
Resets the |
| public synchronized void | |
| public synchronized int | Returns: the value of thecount field, which is the number
of valid bytes in this output stream.Returns the current size of this stream's accumulated data. |
| public synchronized byte[] | Returns: a copy of this stream's accumulated data, as a byte array.Retrieves a copy of this stream's accumulated data, as a byte array. |
| public synchronized ByteArrayInputStream | Returns: an input stream representing this output stream's accumulated dataPerforms an efficient (zero-copy) conversion of the data accumulated in this output stream to an input stream. |
| public synchronized String | Returns: String translated from this stream's accumulated data.Overrides java. Converts this stream's accumulated data into a string, translating bytes into characters according to the platform's default character encoding. |
| public synchronized String | |
| public synchronized void | |
| public synchronized void | write(int
the single byte to be written. b)Implements abstract java. Writes the specified single byte. |
| public synchronized void | write(byte[]
the data. b, int the start offset in the data. off, int the number of bytes to write. len)Overrides java. Writes the specified portion of the designated octet sequence. |
| public synchronized void | writeTo(OutputStream
the output stream to which to write the data. out)Writes the complete contents of this stream's accumulated data to the specified output stream. |
| buf | back to summary |
|---|---|
| protected byte[] buf Data buffer. | |
| closed | back to summary |
|---|---|
| protected boolean closed Whether this stream is closed. | |
| count | back to summary |
|---|---|
| protected int count # of valid bytes in buffer. | |
| freed | back to summary |
|---|---|
| protected boolean freed Whether this stream is freed. | |
| ClosableByteArrayOutputStream | back to summary |
|---|---|
| public ClosableByteArrayOutputStream() Creates a new output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary. | |
| ClosableByteArrayOutputStream | back to summary |
|---|---|
| public ClosableByteArrayOutputStream(int size) throws IllegalArgumentException Creates a new output stream with a buffer capacity of the specified
| |
| capacity | back to summary |
|---|---|
| public synchronized int capacity() throws IOException Returns the current capacity of this stream's data buffer.
| |
| checkClosed | back to summary |
|---|---|
| protected synchronized void checkClosed() throws IOException Tests whether this stream is closed.
| |
| checkFreed | back to summary |
|---|---|
| protected synchronized void checkFreed() throws IOException Tests whether this stream is freed.
| |
| close | back to summary |
|---|---|
| public synchronized void close() Overrides java. Implements java. Closes this object for further writing.
Other operations may continue to succeed until after the first invocation
of | |
| copyOf | back to summary |
|---|---|
| protected byte[] copyOf(byte[] original, int newLength) Retrieves a copy of
| |
| flush | back to summary |
|---|---|
| public void flush() throws IOException Overrides java. Implements java. By default, does nothing.
| |
| free | back to summary |
|---|---|
| public synchronized void free() Closes this object and releases the underlying buffer for garbage collection. | |
| isClosed | back to summary |
|---|---|
| public synchronized boolean isClosed() Retrieves whether this stream is closed.
| |
| isFreed | back to summary |
|---|---|
| public synchronized boolean isFreed() Retrieves whether this stream is freed.
| |
| reset | back to summary |
|---|---|
| public synchronized void reset() throws IOException Resets the Further write operations will reuse the allocated buffer space.
| |
| setSize | back to summary |
|---|---|
| public synchronized void setSize(int newSize) Sets the size of this stream's accumulated data.
| |
| size | back to summary |
|---|---|
| public synchronized int size() Returns the current size of this stream's accumulated data.
| |
| toByteArray | back to summary |
|---|---|
| public synchronized byte[] toByteArray() throws IOException Retrieves a copy of this stream's accumulated data, as a byte array.
| |
| toByteArrayInputStream | back to summary |
|---|---|
| public synchronized ByteArrayInputStream toByteArrayInputStream() throws IOException Performs an efficient (zero-copy) conversion of the data accumulated in this output stream to an input stream.
To ensure the future integrity of the resulting input stream,
| |
| toString | back to summary |
|---|---|
| public synchronized String toString() Overrides java. Converts this stream's accumulated data into a string, translating bytes into characters according to the platform's default character encoding.
| |
| toString | back to summary |
|---|---|
| public synchronized String toString(String enc) throws IOException, UnsupportedEncodingException Converts this stream's accumulated data into a string, translating bytes into characters according to the specified character encoding.
| |
| trimToSize | back to summary |
|---|---|
| public synchronized void trimToSize() throws IOException Attempts to reduce this stream's capacity to its current size.
If the data buffer is larger than necessary to hold its current sequence
of bytes, then it may be resized to become more space efficient.
Calling this method may, but is not required to, affect the value
returned by a subsequent call to the
| |
| write | back to summary |
|---|---|
| public synchronized void write(int b) throws IOException Implements abstract java. Writes the specified single byte.
| |
| write | back to summary |
|---|---|
| public synchronized void write(byte[] b, int off, int len) throws IOException Overrides java. Writes the specified portion of the designated octet sequence.
| |
| writeTo | back to summary |
|---|---|
| public synchronized void writeTo(OutputStream out) throws IOException Writes the complete contents of this stream's accumulated data to the specified output stream.
The operation occurs as if by calling
| |