Class ReusableByteBuffer
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
ByteArrayOutputStream.
Unlike ByteArrayOutputStream, this implementation is backed by an ArrayList
of byte[] instead of 1 constantly resizing byte[] array. 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 contents is extracted with the
writeTo(OutputStream) method.
The reset() method clears the content and resets the buffer to its initial state.
Buffers are disposed except the initial buffer which is reused by subsequent usage.
This class is *not* thread-safe!
Note: This class is for internal use only and subject to backward incompatible change at any time.
- Author:
- brenuart
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a newReusableByteBufferwith the default initial capacity of 1024 bytes.ReusableByteBuffer(int initialCapacity) Create a newReusableByteBufferwith the specified initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidreset()Reset the contents of thisReusableByteBuffer.intsize()Return the current size of the buffer.byte[]Creates a newly allocated byte array.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 OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
ReusableByteBuffer
public ReusableByteBuffer()Create a newReusableByteBufferwith the default initial capacity of 1024 bytes. -
ReusableByteBuffer
public ReusableByteBuffer(int initialCapacity) Create a newReusableByteBufferwith the specified initial capacity.- Parameters:
initialCapacity- the initial buffer size in bytes
-
-
Method Details
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream
-
size
public int size()Return the current size of the buffer.- Returns:
- the current size of the buffer.
-
reset
public void reset()Reset the contents of thisReusableByteBuffer.All currently accumulated output in the output stream is discarded. The output stream can be used again.
-
writeTo
Write the buffers content to the given OutputStream.- Parameters:
out- the OutputStream to write to- Throws:
IOException- in case of problems writing into the output stream
-
toByteArray
public 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.
- See Also:
-