public abstract class IoBuffer extends Object implements Comparable<IoBuffer>
This is a replacement for ByteBuffer
. Please refer to
ByteBuffer
documentation for preliminary usage. MINA does not use NIO
ByteBuffer
directly for two reasons:
fill
,
get/putString
, and get/putAsciiInt()
enough.You can allocate a new heap buffer.
IoBuffer buf = IoBuffer.allocate(1024, false);You can also allocate a new direct buffer:
IoBuffer buf = IoBuffer.allocate(1024, true);or you can set the default buffer type.
// Allocate heap buffer by default. IoBuffer.setUseDirectBuffer(false); // A new heap buffer is returned. IoBuffer buf = IoBuffer.allocate(1024);
This class provides a few wrap(...) methods that wraps any NIO buffers and byte arrays.
Writing variable-length data using NIO ByteBuffers is not really
easy, and it is because its size is fixed at allocation. IoBuffer
introduces the autoExpand property. If autoExpand property
is set to true, you never get a BufferOverflowException
or an
IndexOutOfBoundsException
(except when index is negative). It
automatically expands its capacity. For instance:
String greeting = messageBundle.getMessage("hello"); IoBuffer buf = IoBuffer.allocate(16); // Turn on autoExpand (it is off by default) buf.setAutoExpand(true); buf.putString(greeting, utf8encoder);The underlying
ByteBuffer
is reallocated by IoBuffer
behind
the scene if the encoded data is larger than 16 bytes in the example above.
Its capacity will double, and its limit will increase to the last position
the string is written.
You might also want to decrease the capacity of the buffer when most of the
allocated memory area is not being used. IoBuffer
provides
autoShrink property to take care of this issue. If
autoShrink is turned on, IoBuffer
halves the capacity of the
buffer when compact()
is invoked and only 1/4 or less of the current
capacity is being used.
You can also call the shrink()
method manually to shrink the
capacity of the buffer.
The underlying ByteBuffer
is reallocated by the IoBuffer
behind the scene, and therefore buf()
will return a different
ByteBuffer
instance once capacity changes. Please also note that the
compact()
method or the shrink()
method will not decrease
the capacity if the new capacity is less than the minimumCapacity()
of the buffer.
Derived buffers are the buffers which were created by the
duplicate()
, slice()
, or asReadOnlyBuffer()
methods. They are useful especially when you broadcast the same messages to
multiple IoSession
s. Please note that the buffer derived from and its
derived buffers are not auto-expandable nor auto-shrinkable. Trying to call
setAutoExpand(boolean)
or setAutoShrink(boolean)
with
true parameter will raise an IllegalStateException
.
The IoBufferAllocator
interface lets you override the default buffer
management behavior. There are two allocators provided out-of-the-box:
SimpleBufferAllocator
(default)CachedBufferAllocator
setAllocator(IoBufferAllocator)
.Modifier | Constructor and Description |
---|---|
protected |
IoBuffer()
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
static IoBuffer |
allocate(int capacity)
Returns the direct or heap buffer which is capable to store the specified
amount of bytes.
|
static IoBuffer |
allocate(int capacity,
boolean useDirectBuffer)
Returns a direct or heap IoBuffer which can contain the specified number of
bytes.
|
abstract byte[] |
array() |
abstract int |
arrayOffset() |
abstract CharBuffer |
asCharBuffer() |
abstract DoubleBuffer |
asDoubleBuffer() |
abstract FloatBuffer |
asFloatBuffer() |
abstract InputStream |
asInputStream() |
abstract IntBuffer |
asIntBuffer() |
abstract LongBuffer |
asLongBuffer() |
abstract OutputStream |
asOutputStream() |
abstract IoBuffer |
asReadOnlyBuffer() |
abstract ShortBuffer |
asShortBuffer() |
abstract ByteBuffer |
buf() |
abstract int |
capacity() |
abstract IoBuffer |
capacity(int newCapacity)
Increases the capacity of this buffer.
|
abstract IoBuffer |
clear() |
abstract IoBuffer |
compact() |
abstract IoBuffer |
duplicate() |
abstract IoBuffer |
expand(int expectedRemaining)
Changes the capacity and limit of this buffer so this buffer get the
specified expectedRemaining room from the current position.
|
abstract IoBuffer |
expand(int position,
int expectedRemaining)
Changes the capacity and limit of this buffer so this buffer get the
specified expectedRemaining room from the specified
position.
|
abstract IoBuffer |
fill(byte value,
int size)
Fills this buffer with the specified value.
|
abstract IoBuffer |
fill(int size)
Fills this buffer with
NUL (0x00) . |
abstract IoBuffer |
fillAndReset(byte value,
int size)
Fills this buffer with the specified value.
|
abstract IoBuffer |
fillAndReset(int size)
Fills this buffer with
NUL (0x00) . |
abstract IoBuffer |
flip() |
abstract void |
free()
Declares this buffer and all its derived buffers are not used anymore so that
it can be reused by some
IoBufferAllocator implementations. |
abstract byte |
get() |
abstract IoBuffer |
get(byte[] dst) |
abstract IoBuffer |
get(byte[] dst,
int offset,
int length) |
abstract byte |
get(int index) |
static IoBufferAllocator |
getAllocator() |
abstract char |
getChar() |
abstract char |
getChar(int index) |
abstract double |
getDouble() |
abstract double |
getDouble(int index) |
abstract <E extends Enum<E>> |
getEnum(Class<E> enumClass)
Reads a byte from the buffer and returns the correlating enum constant
defined by the specified enum type.
|
abstract <E extends Enum<E>> |
getEnum(int index,
Class<E> enumClass)
Reads a byte from the buffer and returns the correlating enum constant
defined by the specified enum type.
|
abstract <E extends Enum<E>> |
getEnumInt(Class<E> enumClass)
Reads an int from the buffer and returns the correlating enum constant
defined by the specified enum type.
|
abstract <E extends Enum<E>> |
getEnumInt(int index,
Class<E> enumClass)
Reads an int from the buffer and returns the correlating enum constant
defined by the specified enum type.
|
abstract <E extends Enum<E>> |
getEnumSet(Class<E> enumClass)
Reads a byte sized bit vector and converts it to an
EnumSet . |
abstract <E extends Enum<E>> |
getEnumSet(int index,
Class<E> enumClass)
Reads a byte sized bit vector and converts it to an
EnumSet . |
abstract <E extends Enum<E>> |
getEnumSetInt(Class<E> enumClass)
Reads an int sized bit vector and converts it to an
EnumSet . |
abstract <E extends Enum<E>> |
getEnumSetInt(int index,
Class<E> enumClass)
Reads an int sized bit vector and converts it to an
EnumSet . |
abstract <E extends Enum<E>> |
getEnumSetLong(Class<E> enumClass)
Reads a long sized bit vector and converts it to an
EnumSet . |
abstract <E extends Enum<E>> |
getEnumSetLong(int index,
Class<E> enumClass)
Reads a long sized bit vector and converts it to an
EnumSet . |
abstract <E extends Enum<E>> |
getEnumSetShort(Class<E> enumClass)
Reads a short sized bit vector and converts it to an
EnumSet . |
abstract <E extends Enum<E>> |
getEnumSetShort(int index,
Class<E> enumClass)
Reads a short sized bit vector and converts it to an
EnumSet . |
abstract <E extends Enum<E>> |
getEnumShort(Class<E> enumClass)
Reads a short from the buffer and returns the correlating enum constant
defined by the specified enum type.
|
abstract <E extends Enum<E>> |
getEnumShort(int index,
Class<E> enumClass)
Reads a short from the buffer and returns the correlating enum constant
defined by the specified enum type.
|
abstract float |
getFloat() |
abstract float |
getFloat(int index) |
String |
getHexDump()
Returns hexdump of this buffer.
|
String |
getHexDump(boolean pretty)
Returns hexdump of this buffer.
|
String |
getHexDump(int length)
Return hexdump of this buffer with limited length.
|
String |
getHexDump(int length,
boolean pretty)
Return hexdump of this buffer with limited length.
|
abstract int |
getInt() |
abstract int |
getInt(int index) |
abstract long |
getLong() |
abstract long |
getLong(int index) |
abstract int |
getMediumInt()
Relative get method for reading a medium int value.
|
abstract int |
getMediumInt(int index)
Absolute get method for reading a medium int value.
|
abstract Object |
getObject()
Reads a Java object from the buffer using the context
ClassLoader of
the current thread. |
abstract Object |
getObject(ClassLoader classLoader)
Reads a Java object from the buffer using the specified classLoader.
|
abstract String |
getPrefixedString(CharsetDecoder decoder)
Reads a string which has a 16-bit length field before the actual encoded
string, using the specified
decoder and returns it. |
abstract String |
getPrefixedString(int prefixLength,
CharsetDecoder decoder)
Reads a string which has a length field before the actual encoded string,
using the specified
decoder and returns it. |
abstract short |
getShort() |
abstract short |
getShort(int index) |
abstract IoBuffer |
getSlice(int length)
Get a new IoBuffer containing a slice of the current buffer
|
abstract IoBuffer |
getSlice(int index,
int length)
Get a new IoBuffer containing a slice of the current buffer
|
abstract String |
getString(CharsetDecoder decoder)
Reads a
NUL -terminated string from this buffer using the
specified decoder and returns it. |
abstract String |
getString(int fieldSize,
CharsetDecoder decoder)
Reads a
NUL -terminated string from this buffer using the
specified decoder and returns it. |
abstract short |
getUnsigned()
Reads one unsigned byte as a short integer.
|
abstract short |
getUnsigned(int index)
Reads one byte as an unsigned short integer.
|
abstract long |
getUnsignedInt()
Reads four bytes unsigned integer.
|
abstract long |
getUnsignedInt(int index)
Reads four bytes unsigned integer.
|
abstract int |
getUnsignedMediumInt()
Relative get method for reading an unsigned medium int value.
|
abstract int |
getUnsignedMediumInt(int index)
Absolute get method for reading an unsigned medium int value.
|
abstract int |
getUnsignedShort()
Reads two bytes unsigned integer.
|
abstract int |
getUnsignedShort(int index)
Reads two bytes unsigned integer.
|
abstract boolean |
hasArray() |
abstract boolean |
hasRemaining() |
abstract int |
indexOf(byte b)
Returns the first occurrence position of the specified byte from the current
position to the current limit.
|
abstract boolean |
isAutoExpand() |
abstract boolean |
isAutoShrink() |
abstract boolean |
isDerived() |
abstract boolean |
isDirect() |
abstract boolean |
isReadOnly() |
static boolean |
isUseDirectBuffer() |
abstract int |
limit() |
abstract IoBuffer |
limit(int newLimit) |
abstract IoBuffer |
mark() |
abstract int |
markValue() |
abstract int |
minimumCapacity() |
abstract IoBuffer |
minimumCapacity(int minimumCapacity)
|
protected static int |
normalizeCapacity(int requestedCapacity)
Normalizes the specified capacity of the buffer to power of 2, which is often
helpful for optimal memory usage and performance.
|
abstract ByteOrder |
order() |
abstract IoBuffer |
order(ByteOrder bo) |
abstract int |
position() |
abstract IoBuffer |
position(int newPosition) |
abstract boolean |
prefixedDataAvailable(int prefixLength) |
abstract boolean |
prefixedDataAvailable(int prefixLength,
int maxDataLength) |
abstract IoBuffer |
put(byte b) |
abstract IoBuffer |
put(byte[] src) |
abstract IoBuffer |
put(byte[] src,
int offset,
int length) |
abstract IoBuffer |
put(ByteBuffer src)
Writes the content of the specified src into this buffer.
|
abstract IoBuffer |
put(int index,
byte b) |
abstract IoBuffer |
put(IoBuffer src)
Writes the content of the specified src into this buffer.
|
abstract IoBuffer |
putChar(char value) |
abstract IoBuffer |
putChar(int index,
char value) |
abstract IoBuffer |
putDouble(double value) |
abstract IoBuffer |
putDouble(int index,
double value) |
abstract IoBuffer |
putEnum(Enum<?> e)
Writes an enum's ordinal value to the buffer as a byte.
|
abstract IoBuffer |
putEnum(int index,
Enum<?> e)
Writes an enum's ordinal value to the buffer as a byte.
|
abstract IoBuffer |
putEnumInt(Enum<?> e)
Writes an enum's ordinal value to the buffer as an integer.
|
abstract IoBuffer |
putEnumInt(int index,
Enum<?> e)
Writes an enum's ordinal value to the buffer as an integer.
|
abstract <E extends Enum<E>> |
putEnumSet(int index,
Set<E> set)
Writes the specified
Set to the buffer as a byte sized bit vector. |
abstract <E extends Enum<E>> |
putEnumSet(Set<E> set)
Writes the specified
Set to the buffer as a byte sized bit vector. |
abstract <E extends Enum<E>> |
putEnumSetInt(int index,
Set<E> set)
Writes the specified
Set to the buffer as an int sized bit vector. |
abstract <E extends Enum<E>> |
putEnumSetInt(Set<E> set)
Writes the specified
Set to the buffer as an int sized bit vector. |
abstract <E extends Enum<E>> |
putEnumSetLong(int index,
Set<E> set)
Writes the specified
Set to the buffer as a long sized bit vector. |
abstract <E extends Enum<E>> |
putEnumSetLong(Set<E> set)
Writes the specified
Set to the buffer as a long sized bit vector. |
abstract <E extends Enum<E>> |
putEnumSetShort(int index,
Set<E> set)
Writes the specified
Set to the buffer as a short sized bit vector. |
abstract <E extends Enum<E>> |
putEnumSetShort(Set<E> set)
Writes the specified
Set to the buffer as a short sized bit vector. |
abstract IoBuffer |
putEnumShort(Enum<?> e)
Writes an enum's ordinal value to the buffer as a short.
|
abstract IoBuffer |
putEnumShort(int index,
Enum<?> e)
Writes an enum's ordinal value to the buffer as a short.
|
abstract IoBuffer |
putFloat(float value) |
abstract IoBuffer |
putFloat(int index,
float value) |
abstract IoBuffer |
putInt(int value) |
abstract IoBuffer |
putInt(int index,
int value) |
abstract IoBuffer |
putLong(int index,
long value) |
abstract IoBuffer |
putLong(long value) |
abstract IoBuffer |
putMediumInt(int value)
Relative put method for writing a medium int value.
|
abstract IoBuffer |
putMediumInt(int index,
int value)
Absolute put method for writing a medium int value.
|
abstract IoBuffer |
putObject(Object o)
Writes the specified Java object to the buffer.
|
abstract IoBuffer |
putPrefixedString(CharSequence in,
CharsetEncoder encoder)
Writes the content of
in into this buffer as a string which has
a 16-bit length field before the actual encoded string, using the specified
encoder . |
abstract IoBuffer |
putPrefixedString(CharSequence in,
int prefixLength,
CharsetEncoder encoder)
Writes the content of
in into this buffer as a string which has
a 16-bit length field before the actual encoded string, using the specified
encoder . |
abstract IoBuffer |
putPrefixedString(CharSequence val,
int prefixLength,
int padding,
byte padValue,
CharsetEncoder encoder)
Writes the content of
val into this buffer as a string which has
a 16-bit length field before the actual encoded string, using the specified
encoder . |
abstract IoBuffer |
putPrefixedString(CharSequence in,
int prefixLength,
int padding,
CharsetEncoder encoder)
Writes the content of
in into this buffer as a string which has
a 16-bit length field before the actual encoded string, using the specified
encoder . |
abstract IoBuffer |
putShort(int index,
short value) |
abstract IoBuffer |
putShort(short value) |
abstract IoBuffer |
putString(CharSequence val,
CharsetEncoder encoder)
Writes the content of
in into this buffer using the specified
encoder . |
abstract IoBuffer |
putString(CharSequence val,
int fieldSize,
CharsetEncoder encoder)
Writes the content of
in into this buffer as a
NUL -terminated string using the specified encoder . |
abstract IoBuffer |
putUnsigned(byte value)
Writes an unsigned byte into the ByteBuffer
|
abstract IoBuffer |
putUnsigned(int value)
Writes an unsigned byte into the ByteBuffer
|
abstract IoBuffer |
putUnsigned(int index,
byte value)
Writes an unsigned byte into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsigned(int index,
int value)
Writes an unsigned byte into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsigned(int index,
long value)
Writes an unsigned byte into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsigned(int index,
short value)
Writes an unsigned byte into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsigned(long value)
Writes an unsigned byte into the ByteBuffer
|
abstract IoBuffer |
putUnsigned(short value)
Writes an unsigned byte into the ByteBuffer
|
abstract IoBuffer |
putUnsignedInt(byte value)
Writes an unsigned int into the ByteBuffer
|
abstract IoBuffer |
putUnsignedInt(int value)
Writes an unsigned int into the ByteBuffer
|
abstract IoBuffer |
putUnsignedInt(int index,
byte value)
Writes an unsigned int into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsignedInt(int index,
int value)
Writes an unsigned int into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsignedInt(int index,
long value)
Writes an unsigned int into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsignedInt(int index,
short value)
Writes an unsigned int into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsignedInt(long value)
Writes an unsigned int into the ByteBuffer
|
abstract IoBuffer |
putUnsignedInt(short value)
Writes an unsigned int into the ByteBuffer
|
abstract IoBuffer |
putUnsignedShort(byte value)
Writes an unsigned short into the ByteBuffer
|
abstract IoBuffer |
putUnsignedShort(int value)
Writes an unsigned Short into the ByteBuffer
|
abstract IoBuffer |
putUnsignedShort(int index,
byte value)
Writes an unsigned Short into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsignedShort(int index,
int value)
Writes an unsigned Short into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsignedShort(int index,
long value)
Writes an unsigned Short into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsignedShort(int index,
short value)
Writes an unsigned Short into the ByteBuffer at a specified position
|
abstract IoBuffer |
putUnsignedShort(long value)
Writes an unsigned Short into the ByteBuffer
|
abstract IoBuffer |
putUnsignedShort(short value)
Writes an unsigned Short into the ByteBuffer
|
abstract int |
remaining() |
abstract IoBuffer |
reset() |
abstract IoBuffer |
rewind() |
static void |
setAllocator(IoBufferAllocator newAllocator)
Sets the allocator used by existing and new buffers
|
abstract IoBuffer |
setAutoExpand(boolean autoExpand)
Turns on or off autoExpand.
|
abstract IoBuffer |
setAutoShrink(boolean autoShrink)
Turns on or off autoShrink.
|
static void |
setUseDirectBuffer(boolean useDirectBuffer)
Sets if a direct buffer should be allocated by default when the type of the
new buffer is not specified.
|
abstract IoBuffer |
shrink()
Changes the capacity of this buffer so this buffer occupies as less memory as
possible while retaining the position, limit and the buffer content between
the position and limit.
|
abstract IoBuffer |
skip(int size)
Forwards the position of this buffer as the specified
size
bytes. |
abstract IoBuffer |
slice() |
abstract IoBuffer |
sweep()
Clears this buffer and fills its content with NUL.
|
abstract IoBuffer |
sweep(byte value)
double Clears this buffer and fills its content with value.
|
static IoBuffer |
wrap(byte[] byteArray)
Wraps the specified byte array into a MINA heap buffer.
|
static IoBuffer |
wrap(byte[] byteArray,
int offset,
int length)
Wraps the specified byte array into MINA heap buffer.
|
static IoBuffer |
wrap(ByteBuffer nioBuffer)
Wraps the specified NIO
ByteBuffer into a MINA buffer (either direct
or heap). |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
compareTo
protected IoBuffer()
public static IoBufferAllocator getAllocator()
public static void setAllocator(IoBufferAllocator newAllocator)
newAllocator
- the new allocator to usepublic static boolean isUseDirectBuffer()
public static void setUseDirectBuffer(boolean useDirectBuffer)
useDirectBuffer
- Tells if direct buffers should be allocatedpublic static IoBuffer allocate(int capacity)
capacity
- the capacity of the buffersetUseDirectBuffer(boolean)
public static IoBuffer allocate(int capacity, boolean useDirectBuffer)
capacity
- the capacity of the bufferuseDirectBuffer
- true to get a direct buffer, false
to get a heap buffer.public static IoBuffer wrap(ByteBuffer nioBuffer)
ByteBuffer
into a MINA buffer (either direct
or heap).nioBuffer
- The ByteBuffer
to wrapByteBuffer
public static IoBuffer wrap(byte[] byteArray)
byteArray
- The byte array to wrappublic static IoBuffer wrap(byte[] byteArray, int offset, int length)
byteArray
- The byte array to wrapoffset
- The starting point in the byte arraylength
- The number of bytes to storeprotected static int normalizeCapacity(int requestedCapacity)
Integer.MAX_VALUE
, it returns Integer.MAX_VALUE
. If
it is zero, it returns zero.requestedCapacity
- The IoBuffer capacity we want to be able to storepublic abstract void free()
IoBufferAllocator
implementations. It is not
mandatory to call this method, but you might want to invoke this method for
maximum performance.public abstract ByteBuffer buf()
ByteBuffer
instance.public abstract boolean isDirect()
ByteBuffer.isDirect()
public abstract boolean isDerived()
duplicate()
, slice()
or
asReadOnlyBuffer()
methods.public abstract boolean isReadOnly()
Buffer.isReadOnly()
public abstract int minimumCapacity()
public abstract IoBuffer minimumCapacity(int minimumCapacity)
compact()
and shrink()
operation. The default value is the initial capacity of the buffer.minimumCapacity
- the wanted minimum capacityByteBuffer
instance.public abstract int capacity()
Buffer.capacity()
public abstract IoBuffer capacity(int newCapacity)
Initial buffer : 0 L C +--------+----------+ |XXXXXXXX| | +--------+----------+ ^ ^ ^ | | | pos limit capacity V <= C : 0 L C +--------+----------+ |XXXXXXXX| | +--------+----------+ ^ ^ ^ | | | pos limit newCapacity V > C : 0 L C V +--------+-----------------------+ |XXXXXXXX| : | +--------+-----------------------+ ^ ^ ^ ^ | | | | pos limit oldCapacity newCapacity The buffer has been increased.
newCapacity
- the wanted capacityByteBuffer
instance.public abstract boolean isAutoExpand()
public abstract IoBuffer setAutoExpand(boolean autoExpand)
autoExpand
- The flag value to setpublic abstract boolean isAutoShrink()
public abstract IoBuffer setAutoShrink(boolean autoShrink)
autoShrink
- The flag value to setpublic abstract IoBuffer expand(int expectedRemaining)
Initial buffer : 0 L C +--------+----------+ |XXXXXXXX| | +--------+----------+ ^ ^ ^ | | | pos limit capacity ( pos + V ) <= L, no change : 0 L C +--------+----------+ |XXXXXXXX| | +--------+----------+ ^ ^ ^ | | | pos limit newCapacity You can still put ( L - pos ) bytes in the buffer ( pos + V ) > L & ( pos + V ) <= C : 0 L C +------------+------+ |XXXXXXXX:...| | +------------+------+ ^ ^ ^ | | | pos newlimit newCapacity You can now put ( L - pos + V ) bytes in the buffer. ( pos + V ) > C 0 L C +-------------------+----+ |XXXXXXXX:..........:....| +------------------------+ ^ ^ | | pos +-- newlimit | +-- newCapacity You can now put ( L - pos + V ) bytes in the buffer, which limit is now equals to the capacity.Note that the expecting remaining bytes starts at the current position. In all those examples, the position is 0.
expectedRemaining
- The expected remaining bytes in the bufferpublic abstract IoBuffer expand(int position, int expectedRemaining)
Initial buffer : P L C +--------+----------+ |XXXXXXXX| | +--------+----------+ ^ ^ ^ | | | pos limit capacity ( pos + V ) <= L, no change : P L C +--------+----------+ |XXXXXXXX| | +--------+----------+ ^ ^ ^ | | | pos limit newCapacity You can still put ( L - pos ) bytes in the buffer ( pos + V ) > L & ( pos + V ) <= C : P L C +------------+------+ |XXXXXXXX:...| | +------------+------+ ^ ^ ^ | | | pos newlimit newCapacity You can now put ( L - pos + V) bytes in the buffer. ( pos + V ) > C P L C +-------------------+----+ |XXXXXXXX:..........:....| +------------------------+ ^ ^ | | pos +-- newlimit | +-- newCapacity You can now put ( L - pos + V ) bytes in the buffer, which limit is now equals to the capacity.Note that the expecting remaining bytes starts at the current position. In all those examples, the position is P.
position
- The starting position from which we want to define a
remaining number of bytesexpectedRemaining
- The expected remaining bytes in the bufferpublic abstract IoBuffer shrink()
minimumCapacity()
minimumCapacity()
. For
instance, if the limit is 7 and the capacity is 36, with a minimum capacity
of 8, shrinking the buffer will left a capacity of 9 (we go down from 36 to
18, then from 18 to 9).
Initial buffer : +--------+----------+ |XXXXXXXX| | +--------+----------+ ^ ^ ^ ^ | | | | pos | | capacity | | | +-- minimumCapacity | +-- limit Resulting buffer : +--------+--+-+ |XXXXXXXX| | | +--------+--+-+ ^ ^ ^ ^ | | | | | | | +-- new capacity | | | pos | +-- minimum capacity | +-- limit
public abstract int position()
Buffer.position()
public abstract IoBuffer position(int newPosition)
newPosition
- Sets the new position in the bufferBuffer.position(int)
public abstract int limit()
Buffer.limit()
public abstract IoBuffer limit(int newLimit)
newLimit
- The new buffer's limitBuffer.limit(int)
public abstract IoBuffer mark()
Buffer.mark()
public abstract int markValue()
public abstract IoBuffer reset()
Buffer.reset()
public abstract IoBuffer clear()
Buffer.clear()
public abstract IoBuffer sweep()
public abstract IoBuffer sweep(byte value)
value
- The value to put in the bufferpublic abstract IoBuffer flip()
Buffer.flip()
public abstract IoBuffer rewind()
Buffer.rewind()
public abstract int remaining()
Buffer.remaining()
public abstract boolean hasRemaining()
Buffer.hasRemaining()
public abstract IoBuffer duplicate()
ByteBuffer.duplicate()
public abstract IoBuffer slice()
ByteBuffer.slice()
public abstract IoBuffer asReadOnlyBuffer()
ByteBuffer.asReadOnlyBuffer()
public abstract boolean hasArray()
array()
method will return a byte[]ByteBuffer.hasArray()
public abstract byte[] array()
ByteBuffer.array()
public abstract int arrayOffset()
array()
method is
calledByteBuffer.arrayOffset()
public abstract byte get()
ByteBuffer.get()
public abstract short getUnsigned()
public abstract IoBuffer put(byte b)
b
- The byte to put in the bufferByteBuffer.put(byte)
public abstract byte get(int index)
index
- The position for which we want to read a byteByteBuffer.get(int)
public abstract short getUnsigned(int index)
index
- The position for which we want to read an unsigned bytepublic abstract IoBuffer put(int index, byte b)
index
- The position where the byte will be putb
- The byte to putByteBuffer.put(int, byte)
public abstract IoBuffer get(byte[] dst, int offset, int length)
dst
- The destination bufferoffset
- The position in the original bufferlength
- The number of bytes to copyByteBuffer.get(byte[], int, int)
public abstract IoBuffer get(byte[] dst)
dst
- The byte[] that will contain the read bytesByteBuffer.get(byte[])
public abstract IoBuffer getSlice(int index, int length)
index
- The position in the bufferlength
- The number of bytes to copypublic abstract IoBuffer getSlice(int length)
length
- The number of bytes to copypublic abstract IoBuffer put(ByteBuffer src)
src
- The source ByteBufferpublic abstract IoBuffer put(IoBuffer src)
src
- The source IoBufferpublic abstract IoBuffer put(byte[] src, int offset, int length)
src
- The byte[] to putoffset
- The position in the sourcelength
- The number of bytes to copyByteBuffer.put(byte[], int, int)
public abstract IoBuffer put(byte[] src)
src
- The byte[] to putByteBuffer.put(byte[])
public abstract IoBuffer compact()
ByteBuffer.compact()
public abstract ByteOrder order()
ByteBuffer.order()
public abstract IoBuffer order(ByteOrder bo)
bo
- The new ByteBuffer to use for this IoBufferByteBuffer.order(ByteOrder)
public abstract char getChar()
ByteBuffer.getChar()
public abstract IoBuffer putChar(char value)
value
- The char to put at the current positionByteBuffer.putChar(char)
public abstract char getChar(int index)
index
- The index in the IoBuffer where we will read a char fromByteBuffer.getChar(int)
public abstract IoBuffer putChar(int index, char value)
index
- The index in the IoBuffer where we will put a char invalue
- The char to put at the current positionByteBuffer.putChar(int, char)
public abstract CharBuffer asCharBuffer()
ByteBuffer.asCharBuffer()
public abstract short getShort()
ByteBuffer.getShort()
public abstract int getUnsignedShort()
public abstract IoBuffer putShort(short value)
value
- The short to put at the current positionByteBuffer.putShort(short)
public abstract short getShort(int index)
index
- The index in the IoBuffer where we will read a short fromByteBuffer.getShort()
public abstract int getUnsignedShort(int index)
index
- The index in the IoBuffer where we will read an unsigned short
frompublic abstract IoBuffer putShort(int index, short value)
index
- The position at which the short should be writtenvalue
- The short to put at the current positionByteBuffer.putShort(int, short)
public abstract ShortBuffer asShortBuffer()
ByteBuffer.asShortBuffer()
public abstract int getInt()
ByteBuffer.getInt()
public abstract long getUnsignedInt()
public abstract int getMediumInt()
Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by three.
public abstract int getUnsignedMediumInt()
Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order, and then increments the position by three.
public abstract int getMediumInt(int index)
Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order.
index
- The index from which the medium int will be readIndexOutOfBoundsException
- If index is negative or not
smaller than the buffer's limitpublic abstract int getUnsignedMediumInt(int index)
Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order.
index
- The index from which the unsigned medium int will be readIndexOutOfBoundsException
- If index is negative or not
smaller than the buffer's limitpublic abstract IoBuffer putMediumInt(int value)
Writes three bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by three.
value
- The medium int value to be writtenpublic abstract IoBuffer putMediumInt(int index, int value)
Writes three bytes containing the given int value, in the current byte order, into this buffer at the given index.
index
- The index at which the bytes will be writtenvalue
- The medium int value to be writtenIndexOutOfBoundsException
- If index is negative or not
smaller than the buffer's limit, minus
threepublic abstract IoBuffer putInt(int value)
value
- The int to put at the current positionByteBuffer.putInt(int)
public abstract IoBuffer putUnsigned(byte value)
value
- the byte to writepublic abstract IoBuffer putUnsigned(int index, byte value)
index
- the position in the buffer to write the valuevalue
- the byte to writepublic abstract IoBuffer putUnsigned(short value)
value
- the short to writepublic abstract IoBuffer putUnsigned(int index, short value)
index
- the position in the buffer to write the valuevalue
- the short to writepublic abstract IoBuffer putUnsigned(int value)
value
- the int to writepublic abstract IoBuffer putUnsigned(int index, int value)
index
- the position in the buffer to write the valuevalue
- the int to writepublic abstract IoBuffer putUnsigned(long value)
value
- the long to writepublic abstract IoBuffer putUnsigned(int index, long value)
index
- the position in the buffer to write the valuevalue
- the long to writepublic abstract IoBuffer putUnsignedInt(byte value)
value
- the byte to writepublic abstract IoBuffer putUnsignedInt(int index, byte value)
index
- the position in the buffer to write the valuevalue
- the byte to writepublic abstract IoBuffer putUnsignedInt(short value)
value
- the short to writepublic abstract IoBuffer putUnsignedInt(int index, short value)
index
- the position in the buffer to write the valuevalue
- the short to writepublic abstract IoBuffer putUnsignedInt(int value)
value
- the int to writepublic abstract IoBuffer putUnsignedInt(int index, int value)
index
- the position in the buffer to write the valuevalue
- the int to writepublic abstract IoBuffer putUnsignedInt(long value)
value
- the long to writepublic abstract IoBuffer putUnsignedInt(int index, long value)
index
- the position in the buffer to write the valuevalue
- the long to writepublic abstract IoBuffer putUnsignedShort(byte value)
value
- the byte to writepublic abstract IoBuffer putUnsignedShort(int index, byte value)
index
- the position in the buffer to write the valuevalue
- the byte to writepublic abstract IoBuffer putUnsignedShort(short value)
value
- the short to writepublic abstract IoBuffer putUnsignedShort(int index, short value)
index
- the position in the buffer to write the unsigned shortvalue
- the unsigned short to writepublic abstract IoBuffer putUnsignedShort(int value)
value
- the int to writepublic abstract IoBuffer putUnsignedShort(int index, int value)
index
- the position in the buffer to write the valuevalue
- the int to writepublic abstract IoBuffer putUnsignedShort(long value)
value
- the long to writepublic abstract IoBuffer putUnsignedShort(int index, long value)
index
- the position in the buffer to write the shortvalue
- the long to writepublic abstract int getInt(int index)
index
- The index in the IoBuffer where we will read an int fromByteBuffer.getInt(int)
public abstract long getUnsignedInt(int index)
index
- The index in the IoBuffer where we will read an unsigned int
frompublic abstract IoBuffer putInt(int index, int value)
index
- The position where to put the intvalue
- The int to put in the IoBufferByteBuffer.putInt(int, int)
public abstract IntBuffer asIntBuffer()
ByteBuffer.asIntBuffer()
public abstract long getLong()
ByteBuffer.getLong()
public abstract IoBuffer putLong(long value)
value
- The log to put in the IoBufferByteBuffer.putLong(int, long)
public abstract long getLong(int index)
index
- The index in the IoBuffer where we will read a long fromByteBuffer.getLong(int)
public abstract IoBuffer putLong(int index, long value)
index
- The position where to put the longvalue
- The long to put in the IoBufferByteBuffer.putLong(int, long)
public abstract LongBuffer asLongBuffer()
ByteBuffer.asLongBuffer()
public abstract float getFloat()
ByteBuffer.getFloat()
public abstract IoBuffer putFloat(float value)
value
- The float to put in the IoBufferByteBuffer.putFloat(float)
public abstract float getFloat(int index)
index
- The index in the IoBuffer where we will read a float fromByteBuffer.getFloat(int)
public abstract IoBuffer putFloat(int index, float value)
index
- The position where to put the floatvalue
- The float to put in the IoBufferByteBuffer.putFloat(int, float)
public abstract FloatBuffer asFloatBuffer()
ByteBuffer.asFloatBuffer()
public abstract double getDouble()
ByteBuffer.getDouble()
public abstract IoBuffer putDouble(double value)
value
- The double to put at the IoBuffer current positionByteBuffer.putDouble(double)
public abstract double getDouble(int index)
index
- The position where to get the double fromByteBuffer.getDouble(int)
public abstract IoBuffer putDouble(int index, double value)
index
- The position where to put the doublevalue
- The double to put in the IoBufferByteBuffer.putDouble(int, double)
public abstract DoubleBuffer asDoubleBuffer()
ByteBuffer.asDoubleBuffer()
public abstract InputStream asInputStream()
InputStream
that reads the data from this buffer.
InputStream.read()
returns -1 if the buffer position
reaches to the limit.public abstract OutputStream asOutputStream()
OutputStream
that appends the data into this buffer.
Please note that the OutputStream.write(int)
will throw a
BufferOverflowException
instead of an IOException
in
case of buffer overflow. Please set autoExpand property by
calling setAutoExpand(boolean)
to prevent the unexpected
runtime exception.public String getHexDump()
public String getHexDump(boolean pretty)
pretty
- tells if the ourput should be verbose or notpublic String getHexDump(int length)
length
- The maximum number of bytes to dump from the current buffer
position.public String getHexDump(int length, boolean pretty)
length
- The maximum number of bytes to dump from the current buffer
position.pretty
- tells if the ourput should be verbose or notpublic abstract String getString(CharsetDecoder decoder) throws CharacterCodingException
NUL
-terminated string from this buffer using the
specified decoder
and returns it. This method reads until the
limit of this buffer if no NUL is found.decoder
- The CharsetDecoder
to useCharacterCodingException
- Thrown when an error occurred while
decoding the bufferpublic abstract String getString(int fieldSize, CharsetDecoder decoder) throws CharacterCodingException
NUL
-terminated string from this buffer using the
specified decoder
and returns it.fieldSize
- the maximum number of bytes to readdecoder
- The CharsetDecoder
to useCharacterCodingException
- Thrown when an error occurred while
decoding the bufferpublic abstract IoBuffer putString(CharSequence val, CharsetEncoder encoder) throws CharacterCodingException
in
into this buffer using the specified
encoder
. This method doesn't terminate string with NUL.
You have to do it by yourself.val
- The CharSequence to put in the IoBufferencoder
- The CharsetEncoder to useCharacterCodingException
- When we have an error while decoding the
Stringpublic abstract IoBuffer putString(CharSequence val, int fieldSize, CharsetEncoder encoder) throws CharacterCodingException
in
into this buffer as a
NUL
-terminated string using the specified encoder
.
If the charset name of the encoder is UTF-16, you cannot specify odd
fieldSize
, and this method will append two NUL
s as
a terminator.
Please note that this method doesn't terminate with NUL
if the
input string is longer than fieldSize.
val
- The CharSequence to put in the IoBufferfieldSize
- the maximum number of bytes to writeencoder
- The CharsetEncoder to useCharacterCodingException
- When we have an error while decoding the
Stringpublic abstract String getPrefixedString(CharsetDecoder decoder) throws CharacterCodingException
decoder
and returns it. This method
is a shortcut for getPrefixedString(2, decoder).decoder
- The CharsetDecoder to useCharacterCodingException
- When we have an error while decoding the
Stringpublic abstract String getPrefixedString(int prefixLength, CharsetDecoder decoder) throws CharacterCodingException
decoder
and returns it.prefixLength
- the length of the length field (1, 2, or 4)decoder
- The CharsetDecoder to useCharacterCodingException
- When we have an error while decoding the
Stringpublic abstract IoBuffer putPrefixedString(CharSequence in, CharsetEncoder encoder) throws CharacterCodingException
in
into this buffer as a string which has
a 16-bit length field before the actual encoded string, using the specified
encoder
. This method is a shortcut for
putPrefixedString(in, 2, 0, encoder).in
- The CharSequence to put in the IoBufferencoder
- The CharsetEncoder to useCharacterCodingException
- When we have an error while decoding the
CharSequencepublic abstract IoBuffer putPrefixedString(CharSequence in, int prefixLength, CharsetEncoder encoder) throws CharacterCodingException
in
into this buffer as a string which has
a 16-bit length field before the actual encoded string, using the specified
encoder
. This method is a shortcut for
putPrefixedString(in, prefixLength, 0, encoder).in
- The CharSequence to put in the IoBufferprefixLength
- the length of the length field (1, 2, or 4)encoder
- The CharsetEncoder to useCharacterCodingException
- When we have an error while decoding the
CharSequencepublic abstract IoBuffer putPrefixedString(CharSequence in, int prefixLength, int padding, CharsetEncoder encoder) throws CharacterCodingException
in
into this buffer as a string which has
a 16-bit length field before the actual encoded string, using the specified
encoder
. This method is a shortcut for
putPrefixedString(in, prefixLength, padding, ( byte ) 0, encoder)in
- The CharSequence to put in the IoBufferprefixLength
- the length of the length field (1, 2, or 4)padding
- the number of padded NULs (1 (or 0), 2, or 4)encoder
- The CharsetEncoder to useCharacterCodingException
- When we have an error while decoding the
CharSequencepublic abstract IoBuffer putPrefixedString(CharSequence val, int prefixLength, int padding, byte padValue, CharsetEncoder encoder) throws CharacterCodingException
val
into this buffer as a string which has
a 16-bit length field before the actual encoded string, using the specified
encoder
.val
- The CharSequence to put in teh IoBufferprefixLength
- the length of the length field (1, 2, or 4)padding
- the number of padded bytes (1 (or 0), 2, or 4)padValue
- the value of padded bytesencoder
- The CharsetEncoder to useCharacterCodingException
- When we have an error while decoding the
CharSequencepublic abstract Object getObject() throws ClassNotFoundException
ClassLoader
of
the current thread.ClassNotFoundException
- thrown when we can't find the Class to usepublic abstract Object getObject(ClassLoader classLoader) throws ClassNotFoundException
classLoader
- The classLoader to use to read an Object from the IoBufferClassNotFoundException
- thrown when we can't find the Class to usepublic abstract IoBuffer putObject(Object o)
o
- The Object to write in the IoBufferpublic abstract boolean prefixedDataAvailable(int prefixLength)
prefixLength
- the length of the prefix field (1, 2, or 4)prefixedDataAvailable(int, int)
instead.IllegalArgumentException
- if prefixLength is wrongBufferDataException
- if data length is negativepublic abstract boolean prefixedDataAvailable(int prefixLength, int maxDataLength)
prefixLength
- the length of the prefix field (1, 2, or 4)maxDataLength
- the allowed maximum of the read data lengthIllegalArgumentException
- if prefixLength is wrongBufferDataException
- if data length is negative or greater then
maxDataLengthpublic abstract int indexOf(byte b)
b
- The byte we are looking forpublic abstract IoBuffer skip(int size)
size
bytes.size
- The added sizepublic abstract IoBuffer fill(byte value, int size)
value
- The value to fill the IoBuffer withsize
- The added sizepublic abstract IoBuffer fillAndReset(byte value, int size)
value
- The value to fill the IoBuffer withsize
- The added sizepublic abstract IoBuffer fill(int size)
NUL (0x00)
. This method moves buffer
position forward.size
- The added sizepublic abstract IoBuffer fillAndReset(int size)
NUL (0x00)
. This method does not change
buffer position.size
- The added sizepublic abstract <E extends Enum<E>> E getEnum(Class<E> enumClass)
E
- The enum type to returnenumClass
- The enum's class objectpublic abstract <E extends Enum<E>> E getEnum(int index, Class<E> enumClass)
E
- The enum type to returnindex
- the index from which the byte will be readenumClass
- The enum's class objectpublic abstract <E extends Enum<E>> E getEnumShort(Class<E> enumClass)
E
- The enum type to returnenumClass
- The enum's class objectpublic abstract <E extends Enum<E>> E getEnumShort(int index, Class<E> enumClass)
E
- The enum type to returnindex
- the index from which the bytes will be readenumClass
- The enum's class objectpublic abstract <E extends Enum<E>> E getEnumInt(Class<E> enumClass)
E
- The enum type to returnenumClass
- The enum's class objectpublic abstract <E extends Enum<E>> E getEnumInt(int index, Class<E> enumClass)
E
- The enum type to returnindex
- the index from which the bytes will be readenumClass
- The enum's class objectpublic abstract IoBuffer putEnum(Enum<?> e)
e
- The enum to write to the bufferpublic abstract IoBuffer putEnum(int index, Enum<?> e)
index
- The index at which the byte will be writtene
- The enum to write to the bufferpublic abstract IoBuffer putEnumShort(Enum<?> e)
e
- The enum to write to the bufferpublic abstract IoBuffer putEnumShort(int index, Enum<?> e)
index
- The index at which the bytes will be writtene
- The enum to write to the bufferpublic abstract IoBuffer putEnumInt(Enum<?> e)
e
- The enum to write to the bufferpublic abstract IoBuffer putEnumInt(int index, Enum<?> e)
index
- The index at which the bytes will be writtene
- The enum to write to the bufferpublic abstract <E extends Enum<E>> Set<E> getEnumSet(Class<E> enumClass)
EnumSet
.
Each bit is mapped to a value in the specified enum. The least significant bit maps to the first entry in the specified enum and each subsequent bit maps to each subsequent bit as mapped to the subsequent enum value.
E
- the enum typeenumClass
- the enum class used to create the EnumSetpublic abstract <E extends Enum<E>> Set<E> getEnumSet(int index, Class<E> enumClass)
EnumSet
.E
- the enum typeindex
- the index from which the byte will be readenumClass
- the enum class used to create the EnumSetgetEnumSet(Class)
public abstract <E extends Enum<E>> Set<E> getEnumSetShort(Class<E> enumClass)
EnumSet
.E
- the enum typeenumClass
- the enum class used to create the EnumSetgetEnumSet(Class)
public abstract <E extends Enum<E>> Set<E> getEnumSetShort(int index, Class<E> enumClass)
EnumSet
.E
- the enum typeindex
- the index from which the bytes will be readenumClass
- the enum class used to create the EnumSetgetEnumSet(Class)
public abstract <E extends Enum<E>> Set<E> getEnumSetInt(Class<E> enumClass)
EnumSet
.E
- the enum typeenumClass
- the enum class used to create the EnumSetgetEnumSet(Class)
public abstract <E extends Enum<E>> Set<E> getEnumSetInt(int index, Class<E> enumClass)
EnumSet
.E
- the enum typeindex
- the index from which the bytes will be readenumClass
- the enum class used to create the EnumSetgetEnumSet(Class)
public abstract <E extends Enum<E>> Set<E> getEnumSetLong(Class<E> enumClass)
EnumSet
.E
- the enum typeenumClass
- the enum class used to create the EnumSetgetEnumSet(Class)
public abstract <E extends Enum<E>> Set<E> getEnumSetLong(int index, Class<E> enumClass)
EnumSet
.E
- the enum typeindex
- the index from which the bytes will be readenumClass
- the enum class used to create the EnumSetgetEnumSet(Class)
public abstract <E extends Enum<E>> IoBuffer putEnumSet(Set<E> set)
Set
to the buffer as a byte sized bit vector.E
- the enum type of the Setset
- the enum set to write to the bufferpublic abstract <E extends Enum<E>> IoBuffer putEnumSet(int index, Set<E> set)
Set
to the buffer as a byte sized bit vector.E
- the enum type of the Setindex
- the index at which the byte will be writtenset
- the enum set to write to the bufferpublic abstract <E extends Enum<E>> IoBuffer putEnumSetShort(Set<E> set)
Set
to the buffer as a short sized bit vector.E
- the enum type of the Setset
- the enum set to write to the bufferpublic abstract <E extends Enum<E>> IoBuffer putEnumSetShort(int index, Set<E> set)
Set
to the buffer as a short sized bit vector.E
- the enum type of the Setindex
- the index at which the bytes will be writtenset
- the enum set to write to the bufferpublic abstract <E extends Enum<E>> IoBuffer putEnumSetInt(Set<E> set)
Set
to the buffer as an int sized bit vector.E
- the enum type of the Setset
- the enum set to write to the bufferpublic abstract <E extends Enum<E>> IoBuffer putEnumSetInt(int index, Set<E> set)
Set
to the buffer as an int sized bit vector.E
- the enum type of the Setindex
- the index at which the bytes will be writtenset
- the enum set to write to the bufferpublic abstract <E extends Enum<E>> IoBuffer putEnumSetLong(Set<E> set)
Set
to the buffer as a long sized bit vector.E
- the enum type of the Setset
- the enum set to write to the bufferpublic abstract <E extends Enum<E>> IoBuffer putEnumSetLong(int index, Set<E> set)
Set
to the buffer as a long sized bit vector.E
- the enum type of the Setindex
- the index at which the bytes will be writtenset
- the enum set to write to the bufferCopyright © 2004–2022 Apache MINA Project. All rights reserved.