Class DynamicOutputBuffer
A random-access buffer that resizes itself. This buffer differentiates
from ByteArrayOutputStream in the following points:
- It allows specifying the byte order.
- It assigns several internal buffers instead of one and therefore saves garbage collection cycles.
- It is able to flush some of its internal buffers to an output stream or to a writable channel.
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected List<ByteBuffer>A linked list of internal buffersprotected final intThe size of each internal buffer (also the initial buffer size)protected Queue<ByteBuffer>A queue of buffers that have already been flushed and are free to reuse.protected intThe position of the first byte that has not been already flushed.protected final ByteOrderThe byte order of this bufferprotected intThe current write positionprotected intThe number of buffers to reuseprotected intThe current buffer size (changes dynamically)protected static final StaticBuffers.KeyA unique key to make the first buffer re-usablestatic final intThe default initial buffer size if nothing is specifiedstatic final ByteOrderThe default byte order if nothing is specified -
Constructor Summary
ConstructorsConstructorDescriptionCreates a dynamic buffer with BIG_ENDIAN byte order and a default initial buffer size ofDEFAULT_BUFFER_SIZEbytes.DynamicOutputBuffer(int initialSize) Creates a dynamic buffer with BIG_ENDIAN byte order and the given initial buffer size.DynamicOutputBuffer(ByteOrder order) Creates a dynamic buffer with the given byte order and a default initial buffer size ofDEFAULT_BUFFER_SIZEbytes.DynamicOutputBuffer(ByteOrder order, int initialSize) Creates a dynamic buffer with the given byte order and the given initial buffer size. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidadaptSize(int size) Adapts the buffer size so it is at least equal to the given number of bytes.protected ByteBufferAdds a new buffer to the list of internal buffersprotected ByteBufferAllocates a new buffer or attempts to reuse an existing one.voidclear()Clear the buffer and reset size and write positionprotected voiddeallocateBuffer(int n) Removes a buffer from the list of internal buffers and saves it for reuse if this feature is enabled.voidflushTo(OutputStream out) Tries to copy as much bytes as possible from this buffer to the given channel.voidTries to copy as much bytes as possible from this buffer to the given channel.protected ByteBuffergetBuffer(int position) Gets the buffer that holds the byte at the given absolute position.voidputByte(byte b) Puts a byte into the buffer at the current write position and increases the write position accordingly.voidputByte(int pos, byte b) Puts a byte into the buffer at the given position.voidputBytes(byte... bs) Puts several bytes into the buffer at the given position and increases the write position accordingly.voidputBytes(byte[] bs, int offset, int length) Puts a byte array into the buffer at the given position with the given offset and increases the write position accordingly.voidputBytes(int pos, byte... bs) Puts several bytes into the buffer at the given position.voidputBytes(int pos, byte[] bs, int offset, int length) Puts a byte array into the buffer at the given position with the given offset.voidputDouble(double d) Puts a 64-bit floating point number into the buffer at the current write position and increases the write position accordingly.voidputDouble(int pos, double d) Puts a 64-bit floating point number into the buffer at the given position.voidputFloat(float f) Puts a 32-bit floating point number into the buffer at the current write position and increases the write position accordingly.voidputFloat(int pos, float f) Puts a 32-bit floating point number into the buffer at the given position.voidputInt(int i) Puts a 32-bit integer into the buffer at the current write position and increases write position accordingly.voidputInt(int pos, int i) Puts a 32-bit integer into the buffer at the given position.voidputLong(int pos, long l) Puts a 64-bit integer into the buffer at the given position.voidputLong(long l) Puts a 64-bit integer into the buffer at the current write position and increases the write position accordingly.voidputString(int pos, CharSequence s) Puts a character sequence into the buffer at the given position.voidPuts a character sequence into the buffer at the current write position and increases the write position accordingly.intPuts the given string as UTF-8 into the buffer at the given position.intEncodes the given string as UTF-8, puts it into the buffer and increases the write position accordingly.voidsetReuseBuffersCount(int count) Sets the number of buffers to save for reuse after they have been invalidated byflushTo(OutputStream)orflushTo(WritableByteChannel).intsize()voidwriteTo(OutputStream out) Writes all non-flushed internal buffers to the given output stream.voidWrites all non-flushed internal buffers to the given channel.
-
Field Details
-
DEFAULT_BYTE_ORDER
The default byte order if nothing is specified -
BUFFER_KEY
A unique key to make the first buffer re-usable -
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEThe default initial buffer size if nothing is specified -
_order
The byte order of this buffer -
_bufferSize
protected final int _bufferSizeThe size of each internal buffer (also the initial buffer size) -
_position
protected int _positionThe current write position -
_flushPosition
protected int _flushPositionThe position of the first byte that has not been already flushed. Any attempt to put something into the buffer at a position before this first byte is invalid and causes aIndexOutOfBoundsExceptionto be thrown. -
_size
protected int _sizeThe current buffer size (changes dynamically) -
_buffers
A linked list of internal buffers -
_buffersToReuse
A queue of buffers that have already been flushed and are free to reuse.- See Also:
-
_reuseBuffersCount
protected int _reuseBuffersCountThe number of buffers to reuse- See Also:
-
-
Constructor Details
-
DynamicOutputBuffer
public DynamicOutputBuffer()Creates a dynamic buffer with BIG_ENDIAN byte order and a default initial buffer size ofDEFAULT_BUFFER_SIZEbytes. -
DynamicOutputBuffer
public DynamicOutputBuffer(int initialSize) Creates a dynamic buffer with BIG_ENDIAN byte order and the given initial buffer size.- Parameters:
initialSize- the initial buffer size
-
DynamicOutputBuffer
Creates a dynamic buffer with the given byte order and a default initial buffer size ofDEFAULT_BUFFER_SIZEbytes.- Parameters:
order- the byte order
-
DynamicOutputBuffer
Creates a dynamic buffer with the given byte order and the given initial buffer size.- Parameters:
order- the byte orderinitialSize- the initial buffer size
-
-
Method Details
-
setReuseBuffersCount
public void setReuseBuffersCount(int count) Sets the number of buffers to save for reuse after they have been invalidated byflushTo(OutputStream)orflushTo(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.- Parameters:
count- the number of buffers to save for reuse
-
allocateBuffer
Allocates a new buffer or attempts to reuse an existing one.- Returns:
- a new buffer with the current buffer size and the current byte order
-
deallocateBuffer
protected void deallocateBuffer(int n) Removes a buffer from the list of internal buffers and saves it for reuse if this feature is enabled.- Parameters:
n- the number of the buffer to remove
-
addNewBuffer
Adds a new buffer to the list of internal buffers- Returns:
- the new buffer
-
getBuffer
Gets the buffer that holds the byte at the given absolute position. Automatically adds new internal buffers if the position lies outside the current range of all internal buffers.- Parameters:
position- the position- Returns:
- the buffer at the requested position
-
adaptSize
protected void adaptSize(int size) Adapts the buffer size so it is at least equal to the given number of bytes. This method does not add new internal buffers.- Parameters:
size- the minimum buffer size
-
size
public int size()- Returns:
- the current buffer size (changes dynamically)
-
clear
public void clear()Clear the buffer and reset size and write position -
putByte
public void putByte(byte b) Puts a byte into the buffer at the current write position and increases the write position accordingly.- Parameters:
b- the byte to put
-
putBytes
public void putBytes(byte... bs) Puts several bytes into the buffer at the given position and increases the write position accordingly.- Parameters:
bs- an array of bytes to put
-
putBytes
public void putBytes(byte[] bs, int offset, int length) Puts a byte array into the buffer at the given position with the given offset and increases the write position accordingly.- Parameters:
bs- an array of bytes to putoffset- the offset within the array of the first byte to be readlength- the number of bytes to be read from the given array
-
putByte
public void putByte(int pos, byte b) Puts a byte into the buffer at the given position. Does not increase the write position.- Parameters:
pos- the position where to put the byteb- the byte to put
-
putBytes
public void putBytes(int pos, byte... bs) Puts several bytes into the buffer at the given position. Does not increase the write position.- Parameters:
pos- the position where to put the bytesbs- an array of bytes to put
-
putBytes
public void putBytes(int pos, byte[] bs, int offset, int length) Puts a byte array into the buffer at the given position with the given offset. Does not increase the write position.- Parameters:
pos- the position where to put the bytesbs- an array of bytes to putoffset- the offset within the array of the first byte to be readlength- the number of bytes to be read from the given array
-
putInt
public void putInt(int i) Puts a 32-bit integer into the buffer at the current write position and increases write position accordingly.- Parameters:
i- the integer to put
-
putInt
public void putInt(int pos, int i) Puts a 32-bit integer into the buffer at the given position. Does not increase the write position.- Parameters:
pos- the position where to put the integeri- the integer to put
-
putLong
public void putLong(long l) Puts a 64-bit integer into the buffer at the current write position and increases the write position accordingly.- Parameters:
l- the 64-bit integer to put
-
putLong
public void putLong(int pos, long l) Puts a 64-bit integer into the buffer at the given position. Does not increase the write position.- Parameters:
pos- the position where to put the integerl- the 64-bit integer to put
-
putFloat
public 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.- Parameters:
f- the float to put
-
putFloat
public void putFloat(int pos, float f) Puts a 32-bit floating point number into the buffer at the given position. Does not increase the write position.- Parameters:
pos- the position where to put the floatf- the float to put
-
putDouble
public 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.- Parameters:
d- the double to put
-
putDouble
public void putDouble(int pos, double d) Puts a 64-bit floating point number into the buffer at the given position. Does not increase the write position.- Parameters:
pos- the position where to put the doubled- the double to put
-
putString
Puts a character sequence into the buffer at the current write position and increases the write position accordingly.- Parameters:
s- the character sequence to put
-
putString
Puts a character sequence into the buffer at the given position. Does not increase the write position.- Parameters:
pos- the position where to put the character sequences- the character sequence to put
-
putUTF8
Encodes the given string as UTF-8, puts it into the buffer and increases the write position accordingly.- Parameters:
s- the string to put- Returns:
- the number of UTF-8 bytes put
-
putUTF8
Puts the given string as UTF-8 into the buffer at the given position. This method does not increase the write position.- Parameters:
pos- the position where to put the strings- the string to put- Returns:
- the number of UTF-8 bytes put
-
flushTo
Tries to copy as much bytes as possible from this buffer to the given channel. SeeflushTo(WritableByteChannel)for further information.- Parameters:
out- the output stream to write to- Throws:
IOException- if the buffer could not be flushed
-
flushTo
Tries to copy as much bytes as possible from this buffer to the given channel. This method always copies whole internal buffers and deallocates them afterwards. It does not deallocate the buffer the write position is currently pointing to nor does it deallocate buffers following the write position. The method increases an internal pointer so consecutive calls also copy consecutive bytes.- Parameters:
out- the channel to write to- Throws:
IOException- if the buffer could not be flushed
-
writeTo
Writes all non-flushed internal buffers to the given output stream. IfflushTo(OutputStream)has not been called before, this method writes the whole buffer to the output stream.- Parameters:
out- the output stream to write to- Throws:
IOException- if the buffer could not be written
-
writeTo
Writes all non-flushed internal buffers to the given channel. IfflushTo(WritableByteChannel)has not been called before, this method writes the whole buffer to the channel.- Parameters:
out- the channel to write to- Throws:
IOException- if the buffer could not be written
-