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 newReusableByteBuffer
with the default initial capacity of 1024 bytes.ReusableByteBuffer
(int initialCapacity) Create a newReusableByteBuffer
with the specified initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
reset()
Reset the contents of thisReusableByteBuffer
.int
size()
Return the current size of the buffer.byte[]
Creates a newly allocated byte array.void
write
(byte[] data, int offset, int length) void
write
(int datum) void
writeTo
(OutputStream out) Write the buffers content to the given OutputStream.Methods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
ReusableByteBuffer
public ReusableByteBuffer()Create a newReusableByteBuffer
with the default initial capacity of 1024 bytes. -
ReusableByteBuffer
public ReusableByteBuffer(int initialCapacity) Create a newReusableByteBuffer
with the specified initial capacity.- Parameters:
initialCapacity
- the initial buffer size in bytes
-
-
Method Details
-
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in 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:
-