public class DynamicOutputBuffer
extends java.lang.Object
A random-access buffer that resizes itself. This buffer differentiates
from ByteArrayOutputStream in the following points:
The buffer has an initial size. This is also the size of each internal buffer, so if a new buffer has to be allocated it will take exactly that many bytes.
By calling flushTo(OutputStream) or flushTo(WritableByteChannel)
some of this buffer's internal buffers are flushed and then deallocated. The
buffer maintains an internal counter for all flushed buffers. This allows the
writeTo(OutputStream) and writeTo(WritableByteChannel)
methods to only write non-flushed buffers. So, this class can be used for
streaming by flushing internal buffers from time to time and at the end
writing the rest:
... buf.flushTo(out); ... buf.flushTo(out); ... buf.flushTo(out); ... buf.writeTo(out);
If flushing is never used a single call to one of the writeTo
methods is enough to write the whole buffer.
Once the buffer has been written to an output stream or channel, putting
elements into it is not possible anymore and will lead to an
IndexOutOfBoundsException.
| Modifier and Type | Field and Description |
|---|---|
protected java.util.List<java.nio.ByteBuffer> |
_buffers
A linked list of internal buffers
|
protected int |
_bufferSize
The size of each internal buffer (also the initial buffer size)
|
protected java.util.Queue<java.nio.ByteBuffer> |
_buffersToReuse
A queue of buffers that have already been flushed and are
free to reuse.
|
protected int |
_flushPosition
The position of the first byte that has not been already
flushed.
|
protected java.nio.ByteOrder |
_order
The byte order of this buffer
|
protected int |
_position
The current write position
|
protected int |
_reuseBuffersCount
The number of buffers to reuse
|
protected int |
_size
The current buffer size (changes dynamically)
|
protected java.nio.charset.Charset |
_utf8
The character set used in
putUTF8(String). |
protected java.nio.charset.CharsetEncoder |
_utf8Encoder
The encoder used in
putUTF8(String). |
protected static StaticBuffers.Key |
BUFFER_KEY
A unique key to make the first buffer re-usable
|
static int |
DEFAULT_BUFFER_SIZE
The default initial buffer size if nothing is specified
|
static java.nio.ByteOrder |
DEFAULT_BYTE_ORDER
The default byte order if nothing is specified
|
| Constructor and Description |
|---|
DynamicOutputBuffer()
Creates a dynamic buffer with BIG_ENDIAN byte order and
a default initial buffer size of
DEFAULT_BUFFER_SIZE bytes. |
DynamicOutputBuffer(java.nio.ByteOrder order)
Creates a dynamic buffer with the given byte order and
a default initial buffer size of
DEFAULT_BUFFER_SIZE bytes. |
DynamicOutputBuffer(java.nio.ByteOrder order,
int initialSize)
Creates a dynamic buffer with the given byte order and
the given initial buffer size.
|
DynamicOutputBuffer(int initialSize)
Creates a dynamic buffer with BIG_ENDIAN byte order and
the given initial buffer size.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
adaptSize(int size)
Adapts the buffer size so it is at least equal to the
given number of bytes.
|
protected java.nio.ByteBuffer |
addNewBuffer()
Adds a new buffer to the list of internal buffers
|
protected java.nio.ByteBuffer |
allocateBuffer()
Allocates a new buffer or attempts to reuse an existing one.
|
void |
clear()
Clear the buffer and reset size and write position
|
protected void |
deallocateBuffer(int n)
Removes a buffer from the list of internal buffers and saves it for
reuse if this feature is enabled.
|
void |
flushTo(java.io.OutputStream out)
Tries to copy as much bytes as possible from this buffer to
the given channel.
|
void |
flushTo(java.nio.channels.WritableByteChannel out)
Tries to copy as much bytes as possible from this buffer to
the given channel.
|
protected java.nio.ByteBuffer |
getBuffer(int position)
Gets the buffer that holds the byte at the given absolute position.
|
protected java.nio.charset.Charset |
getUTF8Charset() |
protected java.nio.charset.CharsetEncoder |
getUTF8Encoder() |
void |
putByte(byte b)
Puts a byte into the buffer at the current write position
and increases the write position accordingly.
|
void |
putByte(int pos,
byte b)
Puts a byte into the buffer at the given position.
|
void |
putBytes(byte... bs)
Puts several bytes into the buffer at the given position
and increases the write position accordingly.
|
void |
putBytes(int pos,
byte... bs)
Puts several bytes into the buffer at the given position.
|
void |
putDouble(double d)
Puts a 64-bit floating point number into the buffer at the current
write position and increases the write position accordingly.
|
void |
putDouble(int pos,
double d)
Puts a 64-bit floating point number into the buffer at the given
position.
|
void |
putFloat(float f)
Puts a 32-bit floating point number into the buffer at the current
write position and increases the write position accordingly.
|
void |
putFloat(int pos,
float f)
Puts a 32-bit floating point number into the buffer at the given
position.
|
void |
putInt(int i)
Puts a 32-bit integer into the buffer at the current write position
and increases write position accordingly.
|
void |
putInt(int pos,
int i)
Puts a 32-bit integer into the buffer at the given position.
|
void |
putLong(int pos,
long l)
Puts a 64-bit integer into the buffer at the given position.
|
void |
putLong(long l)
Puts a 64-bit integer into the buffer at the current write position
and increases the write position accordingly.
|
void |
putString(java.lang.CharSequence s)
Puts a character sequence into the buffer at the current
write position and increases the write position accordingly.
|
void |
putString(int pos,
java.lang.CharSequence s)
Puts a character sequence into the buffer at the given
position.
|
int |
putUTF8(int pos,
java.lang.String s)
Puts the given string as UTF-8 into the buffer at the
given position.
|
int |
putUTF8(java.lang.String s)
Encodes the given string as UTF-8, puts it into the buffer
and increases the write position accordingly.
|
void |
setReuseBuffersCount(int count)
Sets the number of buffers to save for reuse after they have been
invalidated by
flushTo(OutputStream) or flushTo(WritableByteChannel). |
int |
size() |
void |
writeTo(java.io.OutputStream out)
Writes all non-flushed internal buffers to the given output
stream.
|
void |
writeTo(java.nio.channels.WritableByteChannel out)
Writes all non-flushed internal buffers to the given channel.
|
public static final java.nio.ByteOrder DEFAULT_BYTE_ORDER
protected static final StaticBuffers.Key BUFFER_KEY
public static final int DEFAULT_BUFFER_SIZE
protected final java.nio.ByteOrder _order
protected final int _bufferSize
protected int _position
protected int _flushPosition
IndexOutOfBoundsException to be thrown.protected int _size
protected java.util.List<java.nio.ByteBuffer> _buffers
protected java.nio.charset.Charset _utf8
putUTF8(String). Will be
created lazily in getUTF8Charset()protected java.nio.charset.CharsetEncoder _utf8Encoder
putUTF8(String). Will be created
lazily in getUTF8Encoder()protected java.util.Queue<java.nio.ByteBuffer> _buffersToReuse
_reuseBuffersCountprotected int _reuseBuffersCount
_buffersToReusepublic DynamicOutputBuffer()
DEFAULT_BUFFER_SIZE bytes.public DynamicOutputBuffer(int initialSize)
initialSize - the initial buffer sizepublic DynamicOutputBuffer(java.nio.ByteOrder order)
DEFAULT_BUFFER_SIZE bytes.order - the byte orderpublic DynamicOutputBuffer(java.nio.ByteOrder order,
int initialSize)
order - the byte orderinitialSize - the initial buffer sizepublic void setReuseBuffersCount(int count)
flushTo(OutputStream) or flushTo(WritableByteChannel).
Invalidated buffers will be saved in an internal queue. When the buffer
needs a new internal buffer, it first attempts to reuse an existing one
before allocating a new one.count - the number of buffers to save for reuseprotected java.nio.ByteBuffer allocateBuffer()
protected void deallocateBuffer(int n)
n - the number of the buffer to removeprotected java.nio.ByteBuffer addNewBuffer()
protected java.nio.ByteBuffer getBuffer(int position)
position - the positionprotected void adaptSize(int size)
size - the minimum buffer sizeprotected java.nio.charset.Charset getUTF8Charset()
protected java.nio.charset.CharsetEncoder getUTF8Encoder()
public int size()
public void clear()
public void putByte(byte b)
b - the byte to putpublic void putBytes(byte... bs)
bs - an array of bytes to putpublic void putByte(int pos,
byte b)
pos - the position where to put the byteb - the byte to putpublic void putBytes(int pos,
byte... bs)
pos - the position where to put the bytesbs - an array of bytes to putpublic void putInt(int i)
i - the integer to putpublic void putInt(int pos,
int i)
pos - the position where to put the integeri - the integer to putpublic void putLong(long l)
l - the 64-bit integer to putpublic void putLong(int pos,
long l)
pos - the position where to put the integerl - the 64-bit integer to putpublic void putFloat(float f)
f - the float to putpublic void putFloat(int pos,
float f)
pos - the position where to put the floatf - the float to putpublic void putDouble(double d)
d - the double to putpublic void putDouble(int pos,
double d)
pos - the position where to put the doubled - the double to putpublic void putString(java.lang.CharSequence s)
s - the character sequence to putpublic void putString(int pos,
java.lang.CharSequence s)
pos - the position where to put the character sequences - the character sequence to putpublic int putUTF8(java.lang.String s)
s - the string to putpublic int putUTF8(int pos,
java.lang.String s)
pos - the position where to put the strings - the string to putpublic void flushTo(java.io.OutputStream out)
throws java.io.IOException
flushTo(WritableByteChannel)
for further information.out - the output stream to write tojava.io.IOException - if the buffer could not be flushedpublic void flushTo(java.nio.channels.WritableByteChannel out)
throws java.io.IOException
out - the channel to write tojava.io.IOException - if the buffer could not be flushedpublic void writeTo(java.io.OutputStream out)
throws java.io.IOException
flushTo(OutputStream) has not been called
before, this method writes the whole buffer to the output stream.out - the output stream to write tojava.io.IOException - if the buffer could not be writtenpublic void writeTo(java.nio.channels.WritableByteChannel out)
throws java.io.IOException
flushTo(WritableByteChannel) has not been called
before, this method writes the whole buffer to the channel.out - the channel to write tojava.io.IOException - if the buffer could not be written