Class FastByteArrayOutputStream
- All Implemented Interfaces:
- Closeable,- Flushable,- AutoCloseable
ByteArrayOutputStream. Note that
 this variant does not extend ByteArrayOutputStream, unlike
 its sibling ResizableByteArrayOutputStream.
 Unlike ByteArrayOutputStream, this implementation is backed
 by a ArrayDeque of byte[] instead of 1 constantly
 resizing byte[]. It does not copy buffers when it gets expanded.
 
The initial buffer is only created when the stream is first written.
 There is also no copying of the internal buffer if its content is extracted
 with the writeTo(OutputStream) method.
- Since:
- 4.2
- Author:
- Craig Andrews, Juergen Hoeller
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newFastByteArrayOutputStreamwith the default initial capacity of 256 bytes.FastByteArrayOutputStream(int initialBlockSize) Create a newFastByteArrayOutputStreamwith the specified initial capacity.
- 
Method SummaryModifier and TypeMethodDescriptionvoidclose()Get anInputStreamto retrieve the data in this OutputStream.voidreset()Reset the contents of thisFastByteArrayOutputStream.voidresize(int targetCapacity) Resize the internal buffer size to a specified capacity.intsize()Return the number of bytes stored in thisFastByteArrayOutputStream.byte[]Create a newly allocated byte array.byte[]Convert the stream's data to a byte array and return the byte array.toString()Convert the buffer's contents into a string decoding bytes using the platform's default character set.voidwrite(byte[] data, int offset, int length) voidwrite(int datum) voidwriteTo(OutputStream out) Write the buffers content to the given OutputStream.Methods inherited from class java.io.OutputStreamflush, nullOutputStream, write
- 
Constructor Details- 
FastByteArrayOutputStreampublic FastByteArrayOutputStream()Create a newFastByteArrayOutputStreamwith the default initial capacity of 256 bytes.
- 
FastByteArrayOutputStreampublic FastByteArrayOutputStream(int initialBlockSize) Create a newFastByteArrayOutputStreamwith the specified initial capacity.- Parameters:
- initialBlockSize- the initial buffer size in bytes
 
 
- 
- 
Method Details- 
write- Specified by:
- writein class- OutputStream
- Throws:
- IOException
 
- 
write- Overrides:
- writein class- OutputStream
- Throws:
- IOException
 
- 
closepublic void close()- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
- Overrides:
- closein class- OutputStream
 
- 
toStringConvert the buffer's contents into a string decoding bytes using the platform's default character set. The length of the newStringis a function of the character set, and hence may not be equal to the size of the buffer.This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The CharsetDecoder class should be used when more control over the decoding process is required. 
- 
sizepublic int size()Return the number of bytes stored in thisFastByteArrayOutputStream.
- 
toByteArrayUnsafepublic byte[] toByteArrayUnsafe()Convert the stream's data to a byte array and return the byte array.Also replaces the internal structures with the byte array to conserve memory: if the byte array is being created anyway, we might as well as use it. This approach also means that if this method is called twice without any writes in the interim, the second call is a no-op. This method is "unsafe" as it returns the internal buffer. Callers should not modify the returned buffer. - Returns:
- the current contents of this output stream, as a byte array.
- See Also:
 
- 
toByteArraypublic byte[] toByteArray()Create a newly allocated byte array.Its size is the current size of this output stream, and it will contain the valid contents of the internal buffer. - Returns:
- the current contents of this output stream, as a byte array
- See Also:
 
- 
resetpublic void reset()Reset the contents of thisFastByteArrayOutputStream.All currently accumulated output in the output stream is discarded. The output stream can be used again. 
- 
getInputStreamGet anInputStreamto retrieve the data in this OutputStream.Note that if any methods are called on the OutputStream (including, but not limited to, any of the write methods, reset(),toByteArray(), andtoByteArrayUnsafe()) then theInputStream's behavior is undefined.- Returns:
- InputStreamof the contents of this OutputStream
 
- 
writeToWrite the buffers content to the given OutputStream.- Parameters:
- out- the OutputStream to write to
- Throws:
- IOException
 
- 
resizepublic void resize(int targetCapacity) Resize the internal buffer size to a specified capacity.- Parameters:
- targetCapacity- the desired size of the buffer
- Throws:
- IllegalArgumentException- if the given capacity is smaller than the actual size of the content stored in the buffer already
- See Also:
 
 
-