public class IOElasticBuffer extends Object
An IOElasticBuffer implements an elastic and zero GC IO buffer. It holds
the buffer contents in an underlying IOBuffer and provides methods to put
and get data of various types to/from its underlying buffer in an elastic, zero
GC manner through pooling and auto growth of the underlying IO buffer. An elastic
IO buffer also implements copy on write and read only facilities.
A user supplies an instance of IOElasticBuffer.Sizer when creating a new IOElasticBuffer.
The sizer is queried by the buffer for the initial buffer length when it needs to create
a new IO buffer. This is done implicitly by any of the IOElasticBuffer methods
that dymamically create/resize the underlying IO buffer.
IOElasticBuffer is not safe for concurrent access by multiple
threads except when contents of the buffer is being concurrently read by multiple
threads using the getXXX() methods provided by the IOElasticBuffer
to read buffer contents.| Modifier and Type | Class and Description |
|---|---|
static interface |
IOElasticBuffer.Initializer
Used to initialize the buffer
The user registers an instance of this interface with an
IOElasticBuffer
buffer to initialize the contents of the backing IO buffer when first created by
the IOElasticBuffer buffer. |
static interface |
IOElasticBuffer.Sizer
Used as an optional provider for the size of the backing
IOBuffer
to create. |
| Modifier and Type | Method and Description |
|---|---|
IOElasticBuffer |
clearCopyOnWrite()
Clear a buffer's 'copy on write' flag.
|
IOElasticBuffer |
clearReadOnly()
Clear a buffer's 'read only' flag.
|
static IOElasticBuffer |
create(int size)
Create a new
IOElasticBuffer buffer using a size |
static IOElasticBuffer |
create(int size,
IOElasticBuffer.Initializer initializer)
Create a new
IOElasticBuffer buffer using a size and initializer |
static IOElasticBuffer |
create(IOElasticBuffer.Sizer sizer)
Create a new
IOElasticBuffer buffer using a sizer |
static IOElasticBuffer |
create(IOElasticBuffer.Sizer sizer,
IOElasticBuffer.Initializer initializer)
Create a new
IOElasticBuffer buffer using a sizer and initializer |
String |
dump(String prefix,
int index,
int len)
Dump the contents of an IO Elastic buffer
|
IOBuffer |
ensureBackingBufferExists()
Ensure the underlying IO buffer exists
|
byte |
get(int index)
Get a byte value from a specified index in a buffer
|
IOBuffer |
getBackingBufferUnsafe()
Get the backing buffer
NOTE: THIS METHOD IS INTENDED FOR X PLATFORM INTERNAL USE AND SHOULD
NOT BE USED.
|
byte |
getByte(int index)
Get a byte value from a specified index in a buffer
|
char |
getChar(int index)
Get a char value from a specified index in a buffer
|
double |
getDouble(int index)
Get a double value from a specified index in a buffer
|
float |
getFloat(int index)
Get a float value from a specified index in a buffer
|
int |
getHighwater()
Get the buffer's highwater mark
|
IOElasticBuffer.Initializer |
getInitializer()
Get the buffer initializer
|
int |
getInt(int index)
Get a int value from a specified index in a buffer
|
int |
getLength()
Get the buffer's length
|
long |
getLong(int index)
Get a long value from a specified index in a buffer
|
int |
getOffset()
Get the buffer's offset into the underlying
IOBuffer. |
short |
getShort(int index)
Get a short value from a specified index in a buffer
|
String |
getString(int index,
int length)
Get a string value from a specified index in a buffer
|
IOElasticBuffer |
getTo(int index,
byte[] array,
int arrayOffset,
int length)
Copy contents from a buffer to a supplied byte array
|
IOElasticBuffer |
getTo(int index,
ByteBuffer buffer,
int length)
Copy contents from a buffer to a supplied byte buffer
This method invokes
getTo((index, buffer, buffer.position(), length)
|
IOElasticBuffer |
getTo(int index,
ByteBuffer buffer,
int bufferOffset,
int length)
Copy contents from a buffer to a supplied byte buffer
|
IOElasticBuffer |
getTo(int index,
char[] array,
int arrayOffset,
int length)
Copy contents from a buffer to a supplied char array
|
IOElasticBuffer |
getTo(int index,
double[] array,
int arrayOffset,
int length)
Copy contents from a buffer to a supplied double array
|
IOElasticBuffer |
getTo(int index,
float[] array,
int arrayOffset,
int length)
Copy contents from a buffer to a supplied float array
|
IOElasticBuffer |
getTo(int index,
int[] array,
int arrayOffset,
int length)
Copy contents from a buffer to a supplied int array
|
IOElasticBuffer |
getTo(int index,
IOBuffer buffer,
int bufferOffset,
int length)
Copy contents from a buffer to a supplied IO buffer
|
IOElasticBuffer |
getTo(int index,
long[] array,
int arrayOffset,
int length)
Copy contents from a buffer to a supplied long array
|
IOElasticBuffer |
getTo(int index,
long addr,
int addrOffset,
int length)
Copy contents from a buffer to native memory
|
IOElasticBuffer |
getTo(int index,
short[] array,
int arrayOffset,
int length)
Copy contents from a buffer to a supplied short array
|
IOElasticBuffer |
getToNative(int index,
long addr,
int addrOffset,
int length)
Copy contents from a buffer to native memory
This method invokes
getTo(index, addr, addrOffset, length)
|
int |
getUnsignedByte(int index)
Get a byte value from a specified index in a buffer
|
long |
getUnsignedInt(int index)
Get a byte value from a specified index in a buffer
|
int |
getUnsignedShort(int index)
Get a byte value from a specified index in a buffer
|
boolean |
hasBackingBuffer()
Get whether a buffer has a backing buffer
|
InputStream |
inputStream(int index)
Get an input stream to read the contents of a buffer
|
boolean |
isCopyOnWrite()
Get whether the buffer is marked 'copy on write'.
|
boolean |
isReadOnly()
Get whether the buffer is marked 'read only'.
|
boolean |
isResizeNeededFor(int index,
int numBytes)
Tests whether the underlying buffer would need to be resized to append
the specified number of bytes at the given index.
|
boolean |
isWrapped() |
OutputStream |
outputStream(int index)
Get an output stream to write to a buffer
|
IOElasticBuffer |
put(int index,
byte val)
Put a byte value at a specified index into the buffer
This method invokes
putByte(index, val)
|
int |
putASCIICharSequence(int index,
CharSequence val)
Put the char sequence endoded as ASCII into the buffer
|
IOElasticBuffer |
putByte(int index,
byte val)
Put a byte value at a specified index into the buffer
|
IOElasticBuffer |
putChar(int index,
char val)
Put a character value at a specified index into the buffer
|
IOElasticBuffer |
putDouble(int index,
double val)
Put a double value at a specified index into the buffer
|
IOElasticBuffer |
putFloat(int index,
float val)
Put a float value at a specified index into the buffer
|
IOElasticBuffer |
putFrom(int index,
byte[] array,
int arrayOffset,
int length)
Copy contents from a supplied byte array to a buffer
|
IOElasticBuffer |
putFrom(int index,
ByteBuffer buffer,
int length)
Copy contents from a supplied byte buffer to a this buffer
This method invokes
putFrom(index, buffer, buffer.position(), length) |
IOElasticBuffer |
putFrom(int index,
ByteBuffer buffer,
int bufferOffset,
int length)
Copy contents from a supplied byte buffer to a this buffer
|
IOElasticBuffer |
putFrom(int index,
char[] array,
int arrayOffset,
int length)
Copy contents from a supplied char array to a buffer
|
IOElasticBuffer |
putFrom(int index,
double[] array,
int arrayOffset,
int length)
Copy contents from a supplied double array to a buffer
|
IOElasticBuffer |
putFrom(int index,
float[] array,
int arrayOffset,
int length)
Copy contents from a supplied float array to a buffer
|
IOElasticBuffer |
putFrom(int index,
int[] array,
int arrayOffset,
int length)
Copy contents from a supplied int array to a buffer
|
IOElasticBuffer |
putFrom(int index,
IOBuffer buffer,
int bufferOffset,
int length)
Copy contents from a supplied IO buffer to this buffer
|
IOElasticBuffer |
putFrom(int index,
IOElasticBuffer buffer,
int bufferOffset,
int length)
Copy contents from a supplied elastic IO buffer to this buffer
|
IOElasticBuffer |
putFrom(int index,
long[] array,
int arrayOffset,
int length)
Copy contents from a supplied long array to a buffer
|
IOElasticBuffer |
putFrom(int index,
long addr,
int addrOffset,
int length)
Copy contents from supplied native memory address to this buffer.
|
IOElasticBuffer |
putFrom(int index,
short[] array,
int arrayOffset,
int length)
Copy contents from a supplied short array to a buffer
|
IOElasticBuffer |
putFromNative(int index,
long addr,
int addrOffset,
int length)
Copy contents from supplied native memory address to this buffer.
|
IOElasticBuffer |
putInt(int index,
int val)
Put an integer value at a specified index into the buffer
|
IOElasticBuffer |
putLong(int index,
long val)
Put a long value at a specified index into the buffer
|
IOElasticBuffer |
putShort(int index,
short val)
Put a short value at a specified index into the buffer
|
int |
putString(int index,
String val)
Put a string value at a specified index into the buffer
|
int |
putUTF8Char(int index,
char val)
Put a character encoded in UTF-8 at the specified index into the buffer.
|
int |
putUTF8CharSequence(int index,
CharSequence val)
Put the char sequence endoded as UTF-8 into the buffer
|
void |
reset()
Reset an
IOElasticBuffer. |
void |
reset(boolean setSizeToHighwater)
Reset an
IOElasticBuffer. |
IOElasticBuffer |
setCopyOnWrite()
Mark the buffer as 'copy on write'.
|
IOElasticBuffer |
setInitializer(IOElasticBuffer.Initializer initializer)
Set the buffer initializer
|
IOElasticBuffer |
setLength(int newLength)
Set the buffer's length
|
IOElasticBuffer |
setReadOnly()
Mark a buffer as 'read only'.
|
void |
shareBackingBuffer(IOElasticBuffer buffer)
Share a backing buffer with another elastic buffer
|
String |
toString()
Return a string representation of a buffer
|
void |
wipe(int index,
int len)
Wipe a packet buffer
|
void |
wrapBackingBuffer(IOBuffer iobuf,
int offset,
int length)
Wrap a buffer around an existing IO buffer
|
public static IOElasticBuffer create(IOElasticBuffer.Sizer sizer, IOElasticBuffer.Initializer initializer)
IOElasticBuffer buffer using a sizer and initializersizer - A callback to the the provider of the size that should be used for
the creation of the backing buffer. This callback is invoked the first time the
backing buffer is created during the life of an IOElasticBuffer. This size
returned by the sizer is interpreted as a an estimate of how much data caller
expects to populate in the buffer. The more the accurate the size specified
here, the less internal resizing will occur for the backing buffer as data
is added to this buffer.initializer - A callback to the initializer of the backing buffer. This
callback is invoked, if null, the first time the backing buffer is created
during the life of an IOElasticBuffer.IOElasticBuffer buffer.public static IOElasticBuffer create(IOElasticBuffer.Sizer sizer)
IOElasticBuffer buffer using a sizerIOElasticBuffer buffer.
This method is invokes create(sizer, null)
public static IOElasticBuffer create(int size, IOElasticBuffer.Initializer initializer)
IOElasticBuffer buffer using a size and initializersize - Specifies the size to be used for the initial creation of the
backing buffer. This size is interpreted as a an estimate of how much data
the call expects to populate in the buffer. The more the accurate the size
specified here, the less internal resizing will occur for the backing buffer
as data is added to this buffer.initializer - A callback to the initializer of the backing buffer. This
callback is invoked, if null, the first time the backing buffer is created
during the life of an IOElasticBuffer.IOElasticBuffer buffer.public static IOElasticBuffer create(int size)
IOElasticBuffer buffer using a sizeIOElasticBuffer buffer.
This method is invokes create(size, null)
public final boolean isWrapped()
public final IOElasticBuffer setInitializer(IOElasticBuffer.Initializer initializer)
public final IOElasticBuffer.Initializer getInitializer()
public final void reset(boolean setSizeToHighwater)
IOElasticBuffer.setSizeToHighwater - This parameter controls whether the buffer size is
changed to the buffer's highwater mark if the highwater exceeded the buffer's
size. If set the true, then the size is set the the higher value between the
buffer's size and highwater i.e. if the highwater exceeded the buffer size
between the buffer creation and reset or between successive resets, then the
size is set to the highwater. This is useful to prevent excessive resizing
after reset in situations where the expected usage of the buffer between
resets is expected to be similar and the usage exceeds the size set during
initial creation of the buffer.
This method resets the contents of an IOElasticBuffer buffer. Upon return
from this method, the IOElasticBuffer buffer is reset to the same state
as when it was first created except that the buffer size is potentially changed
to the buffer's highwater (if setSizeToHighwater it set to true and the
highwater exceeded the currently set size)
public final void reset()
IOElasticBuffer.
This method invokes reset(true)
public final boolean isResizeNeededFor(int index,
int numBytes)
index - The index at which to append the data.numBytes - The number of bytes to append.IllegalArgumentException - If index or numBytes is less than 0.IllegalStateException - If buffer is read onlypublic final boolean hasBackingBuffer()
public final IOBuffer getBackingBufferUnsafe()
NOTE: THIS METHOD IS INTENDED FOR X PLATFORM INTERNAL USE AND SHOULD NOT BE USED. PLEASE CONSULT NEEVE TECHNICAL SUPPORT BEFORE YOU USE THIS METHOD. THIS METHOD CAN BE DEPRECATED/REMOVED/CHANGED AT ANY TIME WIHOUT NOTICE.
public final void wrapBackingBuffer(IOBuffer iobuf, int offset, int length)
iobuf - The IO buffer to wrap aroundoffset - The offset into the IO buffer to wraplength - The number of bytes to wrap
This method wrap an elastic buffer around an IO buffer. The method acquires a reference to the supplied buffer and sets the buffer as read only
public final void shareBackingBuffer(IOElasticBuffer buffer)
buffer - The elastic buffer to share the backing buffer with
This method sets another elastic buffer's backing buffer as its own backing buffer. On return from this method, both the buffers will have the same reference to their backing buffer, will have the same length and high water mark as each other and will be marked as copy on write
public final IOBuffer ensureBackingBufferExists()
public final IOElasticBuffer setReadOnly()
This method marks the buffer as 'read only'. Any attempt to write to a buffer or change the metadata of the backing buffer will result in an exception
Marking a buffer as 'read only' clears the 'copy on write' flag if set.
public final IOElasticBuffer clearReadOnly()
This method clear's the buffer 'read only' flag.
public final boolean isReadOnly()
public final IOElasticBuffer setCopyOnWrite()
This method marks the buffer as 'copy on write'. The next call to modify the length or contents of the buffer will cause the contained buffer to be cloned before performing the operation.
IllegalStateException - Thrown if method is invoked on
a buffer marked as 'read only'.public final IOElasticBuffer clearCopyOnWrite()
This method clear's the buffer 'copy on write' flag.
public final boolean isCopyOnWrite()
public final int getOffset()
IOBuffer.public final IOElasticBuffer setLength(int newLength)
IllegalStateException - if buffer is marked as read-only
and the act of setting the length will result in a modification
of the backing buffer's metadatapublic final int getLength()
public final int getHighwater()
public final IOElasticBuffer put(int index, byte val)
This method invokes putByte(index, val)
public final IOElasticBuffer putByte(int index, byte val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final IOElasticBuffer putChar(int index, char val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final int putUTF8Char(int index,
char val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final IOElasticBuffer putShort(int index, short val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final IOElasticBuffer putInt(int index, int val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final IOElasticBuffer putFloat(int index, float val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final IOElasticBuffer putLong(int index, long val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final IOElasticBuffer putDouble(int index, double val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final int putASCIICharSequence(int index,
CharSequence val)
index - The index into the buffer.str - The char sequenceIllegalStateException - Thrown if the buffer is marked to read onlypublic final int putUTF8CharSequence(int index,
CharSequence val)
index - The index into the buffer.str - The char sequenceIllegalStateException - Thrown if the buffer is marked to read onlypublic final int putString(int index,
String val)
index - The index into the buffer to put the valueval - The value to putIllegalStateException - Thrown if the buffer is marked to read onlypublic final IOElasticBuffer putFrom(int index, byte[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy toarray - The byte array to put from.arrayOffset - The offset into the byte array to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, char[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy toarray - The char array to put from.arrayOffset - The offset into the char array to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, short[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy toarray - The short array to put from.arrayOffset - The offset into the short array to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, int[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy toarray - The int array to put from.arrayOffset - The offset into the int array to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, float[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy toarray - The float array to put from.arrayOffset - The offset into the float array to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, long[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy toarray - The long array to put from.arrayOffset - The offset into the long array to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, double[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy toarray - The double array to put from.arrayOffset - The offset into the double array to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, ByteBuffer buffer, int bufferOffset, int length)
index - The index relative to the buffer offset to copy tobuffer - The byte buffer to put from.bufferOffset - The offset into the byte buffer to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, ByteBuffer buffer, int length)
This method invokes putFrom(index, buffer, buffer.position(), length)
public final IOElasticBuffer putFrom(int index, IOBuffer buffer, int bufferOffset, int length)
index - The index relative to the buffer offset to copy tobuffer - The IO buffer to copy from.bufferOffset - The offset into the source buffer to copy fromlength - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, IOElasticBuffer buffer, int bufferOffset, int length)
index - The index relative to the buffer offset to copy tobuffer - The buffer to copy from.bufferOffset - The offset into the source buffer to copy from.length - The number of bytes to copypublic final IOElasticBuffer putFrom(int index, long addr, int addrOffset, int length)
index - The index into this buffer at which to copy the bytes.address - The address of the native memory to copy.addressOffset - The offset from the native address to copy fromlength - The number of bytes to copy.public final IOElasticBuffer putFromNative(int index, long addr, int addrOffset, int length)
This method invokes putFrom(index, addr, addrOffset, length)
public final byte get(int index)
public final byte getByte(int index)
public final int getUnsignedByte(int index)
public final char getChar(int index)
public final short getShort(int index)
public final int getUnsignedShort(int index)
public final int getInt(int index)
public final long getUnsignedInt(int index)
public final float getFloat(int index)
public final long getLong(int index)
public final double getDouble(int index)
public final String getString(int index, int length)
public final IOElasticBuffer getTo(int index, byte[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy fromarray - The array to copy to.arrayOffset - The offset into the byte array to copy tolength - The number of array elements to copypublic final IOElasticBuffer getTo(int index, char[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy fromarray - The array to copy to.arrayOffset - The offset into the byte array to copy tolength - The number of array elements to copypublic final IOElasticBuffer getTo(int index, short[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy fromarray - The array to copy to.arrayOffset - The offset into the byte array to copy tolength - The number of array elements to copypublic final IOElasticBuffer getTo(int index, int[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy fromarray - The array to copy to.arrayOffset - The offset into the byte array to copy tolength - The number of array elements to copypublic final IOElasticBuffer getTo(int index, float[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy fromarray - The array to copy to.arrayOffset - The offset into the byte array to copy tolength - The number of array elements to copypublic final IOElasticBuffer getTo(int index, long[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy fromarray - The array to copy to.arrayOffset - The offset into the byte array to copy tolength - The number of array elements to copypublic final IOElasticBuffer getTo(int index, double[] array, int arrayOffset, int length)
index - The index relative to the buffer offset to copy fromarray - The array to copy to.arrayOffset - The offset into the byte array to copy tolength - The number of array elements to copypublic final IOElasticBuffer getTo(int index, ByteBuffer buffer, int bufferOffset, int length)
index - The index relative to the buffer offset to copy frombuffer - The byte buffer to copy to.bufferOffset - The offset into the byte buffer to copy tolength - The number of bytes to copypublic final IOElasticBuffer getTo(int index, ByteBuffer buffer, int length)
This method invokes getTo((index, buffer, buffer.position(), length)
public final IOElasticBuffer getTo(int index, IOBuffer buffer, int bufferOffset, int length)
index - The index relative to the buffer offset to copy frombuffer - The buffer to copy to.bufferOffset - The offset into the buffer to copy tolength - The number of bytes to copypublic final IOElasticBuffer getTo(int index, long addr, int addrOffset, int length)
index - The index relative to the buffer offset to copy fromaddress - The address of the native memory to copy to.length - The number of bytes to copy.public final IOElasticBuffer getToNative(int index, long addr, int addrOffset, int length)
This method invokes getTo(index, addr, addrOffset, length)
public final InputStream inputStream(int index)
index - The index relative to the buffer offset
from where the input stream will start reading.public final OutputStream outputStream(int index)
index - The index relative to the buffer offset
from where the output stream will start writing.public final void wipe(int index,
int len)
index - The index relative to the buffer offset to copy fromlen - The length to clean. A value of 0 indicates that the whole
buffer (upto capacity) needs to be wipedpublic final String dump(String prefix, int index, int len)
prefix - The string to prefix the dumped contentsindex - The index relative to the buffer offset to dump fromlen - The length to dump.Copyright © 2019 N5 Technologies, Inc. All Rights Reserved.