public class ReusableByteBuffer extends OutputStream
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.
Constructor and Description |
---|
ReusableByteBuffer()
Create a new
ReusableByteBuffer with the default initial capacity of 1024 bytes. |
ReusableByteBuffer(int initialCapacity)
Create a new
ReusableByteBuffer with the specified initial capacity. |
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
reset()
Reset the contents of this
ReusableByteBuffer . |
int |
size()
Return the current size of the buffer.
|
byte[] |
toByteArray()
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.
|
flush, write
public ReusableByteBuffer()
ReusableByteBuffer
with the default initial capacity of 1024 bytes.public ReusableByteBuffer(int initialCapacity)
ReusableByteBuffer
with the specified initial capacity.initialCapacity
- the initial buffer size in bytespublic void write(int datum) throws IOException
write
in class OutputStream
IOException
public void write(byte[] data, int offset, int length) throws IOException
write
in class OutputStream
IOException
public void close()
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
public int size()
public void reset()
ReusableByteBuffer
.
All currently accumulated output in the output stream is discarded. The output stream can be used again.
public void writeTo(OutputStream out) throws IOException
out
- the OutputStream to write toIOException
- in case of problems writing into the output streampublic byte[] toByteArray()
Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
size()
Copyright © 2013–2022. All rights reserved.