|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.mina.common.ByteBuffer
public abstract class ByteBuffer
A pooled byte buffer used by MINA applications.
This is a replacement for ByteBuffer
. Please refer to
ByteBuffer
and Buffer
documentation for
usage. MINA does not use NIO ByteBuffer
directly for two
reasons:
fill
, get/putString
, and
get/putAsciiInt()
enough.You can get a heap buffer from buffer pool:
ByteBuffer buf = ByteBuffer.allocate(1024, false);you can also get a direct buffer from buffer pool:
ByteBuffer buf = ByteBuffer.allocate(1024, true);or you can let MINA choose:
ByteBuffer buf = ByteBuffer.allocate(1024);
Please note that you never need to release the allocated buffer because MINA will release it automatically when:
IoSession.write(ByteBuffer, Object)
.ProtocolEncoderOutput.write(ByteBuffer)
.ByteBuffer
which is passed as a parameter
of IoHandler.dataRead(IoSession, ByteBuffer)
method. They are released
automatically when the method returns.
You have to release buffers manually by calling release()
when:
acquire()
to prevent the buffer from being released.
This class provides a few wrap(...) methods that wraps
any NIO buffers and byte arrays. Wrapped MINA buffers are not returned
to the buffer pool by default to prevent unexpected memory leakage by default.
In case you want to make it pooled, you can call setPooled(boolean)
with true flag to enable pooling.
Writing variable-length data using NIO ByteBuffers is not really
easy, and it is because its size is fixed. MINA ByteBuffer
introduces autoExpand property. If autoExpand property
is true, you never get BufferOverflowException
or
IndexOutOfBoundsException
(except when index is negative).
It automatically expands its capacity and limit value. For example:
String greeting = messageBundle.getMessage( "hello" ); ByteBuffer buf = ByteBuffer.allocate( 16 ); // Turn on autoExpand (it is off by default) buf.setAutoExpand( true ); buf.putString( greeting, utf8encoder );NIO ByteBuffer is reallocated by MINA ByteBuffer behind the scene if the encoded data is larger than 16 bytes. Its capacity will increase by two times, and its limit will increase to the last position the string is written.
Constructor Summary | |
---|---|
protected |
ByteBuffer()
|
Method Summary | |
---|---|
abstract void |
acquire()
Increases the internal reference count of this buffer to defer automatic release. |
static ByteBuffer |
allocate(int capacity)
Returns the direct or heap buffer which is capable of the specified size. |
static ByteBuffer |
allocate(int capacity,
boolean direct)
Returns the buffer which is capable of the specified size. |
abstract CharBuffer |
asCharBuffer()
|
abstract DoubleBuffer |
asDoubleBuffer()
|
abstract FloatBuffer |
asFloatBuffer()
|
abstract IntBuffer |
asIntBuffer()
|
abstract LongBuffer |
asLongBuffer()
|
abstract ShortBuffer |
asShortBuffer()
|
abstract ByteBuffer |
buf()
Returns the underlying NIO buffer instance. |
abstract int |
capacity()
|
abstract ByteBuffer |
clear()
|
abstract ByteBuffer |
compact()
|
abstract int |
compareTo(ByteBuffer that)
|
abstract boolean |
equals(Object ob)
|
abstract ByteBuffer |
fill(byte value,
int size)
Fills this buffer with the specified value. |
abstract ByteBuffer |
fill(int size)
Fills this buffer with NUL (0x00) . |
abstract ByteBuffer |
fillAndReset(byte value,
int size)
Fills this buffer with the specified value. |
abstract ByteBuffer |
fillAndReset(int size)
Fills this buffer with NUL (0x00) . |
abstract ByteBuffer |
flip()
|
abstract byte |
get()
|
abstract ByteBuffer |
get(byte[] dst)
|
abstract ByteBuffer |
get(byte[] dst,
int offset,
int length)
|
abstract byte |
get(int index)
|
abstract char |
getChar()
|
abstract char |
getChar(int index)
|
abstract double |
getDouble()
|
abstract double |
getDouble(int index)
|
abstract float |
getFloat()
|
abstract float |
getFloat(int index)
|
abstract String |
getHexDump()
Returns hexdump of this buffer. |
abstract int |
getInt()
|
abstract int |
getInt(int index)
|
abstract long |
getLong()
|
abstract long |
getLong(int index)
|
abstract short |
getShort()
|
abstract short |
getShort(int index)
|
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()
|
abstract short |
getUnsigned(int index)
|
abstract long |
getUnsignedInt()
|
abstract long |
getUnsignedInt(int index)
|
abstract int |
getUnsignedShort()
|
abstract int |
getUnsignedShort(int index)
|
abstract int |
hashCode()
|
abstract boolean |
hasRemaining()
|
abstract boolean |
isAutoExpand()
Returns true if and only if autoExpand is turned on. |
abstract boolean |
isDirect()
|
abstract boolean |
isPooled()
Returns true if and only if this buffer is returned back to the buffer pool when released. |
abstract int |
limit()
|
abstract ByteBuffer |
limit(int newLimit)
|
abstract ByteBuffer |
mark()
|
abstract ByteOrder |
order()
|
abstract ByteBuffer |
order(ByteOrder bo)
|
abstract int |
position()
|
abstract ByteBuffer |
position(int newPosition)
|
abstract ByteBuffer |
put(byte b)
|
abstract ByteBuffer |
put(byte[] src)
|
abstract ByteBuffer |
put(byte[] src,
int offset,
int length)
|
abstract ByteBuffer |
put(ByteBuffer src)
|
abstract ByteBuffer |
put(ByteBuffer src)
|
abstract ByteBuffer |
put(int index,
byte b)
|
abstract ByteBuffer |
putChar(char value)
|
abstract ByteBuffer |
putChar(int index,
char value)
|
abstract ByteBuffer |
putDouble(double value)
|
abstract ByteBuffer |
putDouble(int index,
double value)
|
abstract ByteBuffer |
putFloat(float value)
|
abstract ByteBuffer |
putFloat(int index,
float value)
|
abstract ByteBuffer |
putInt(int value)
|
abstract ByteBuffer |
putInt(int index,
int value)
|
abstract ByteBuffer |
putLong(int index,
long value)
|
abstract ByteBuffer |
putLong(long value)
|
abstract ByteBuffer |
putShort(int index,
short value)
|
abstract ByteBuffer |
putShort(short value)
|
abstract ByteBuffer |
putString(CharSequence in,
CharsetEncoder encoder)
Writes the content of in into this buffer using the
specified encoder . |
abstract ByteBuffer |
putString(CharSequence in,
int fieldSize,
CharsetEncoder encoder)
Writes the content of in into this buffer as a
NUL -terminated string using the specified
encoder . |
abstract void |
release()
Releases the specified buffer to buffer pool. |
abstract int |
remaining()
|
abstract ByteBuffer |
reset()
|
abstract ByteBuffer |
rewind()
|
abstract ByteBuffer |
setAutoExpand(boolean autoExpand)
Turns on or off autoExpand. |
abstract void |
setPooled(boolean pooled)
Sets whether this buffer is returned back to the buffer pool when released. |
abstract ByteBuffer |
skip(int size)
Forwards the position of this buffer as the specified size
bytes. |
abstract String |
toString()
|
static ByteBuffer |
wrap(byte[] byteArray)
Wraps the specified byte array into MINA heap buffer. |
static ByteBuffer |
wrap(byte[] byteArray,
int offset,
int length)
Wraps the specified byte array into MINA heap buffer. |
static ByteBuffer |
wrap(ByteBuffer nioBuffer)
Wraps the specified NIO ByteBuffer into MINA buffer. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
protected ByteBuffer()
Method Detail |
---|
public static ByteBuffer allocate(int capacity)
allocate(int, boolean)
to allocate buffers of specific type.
capacity
- the capacity of the bufferpublic static ByteBuffer allocate(int capacity, boolean direct)
capacity
- the capacity of the bufferdirect
- true to get a direct buffer,
false to get a heap buffer.public static ByteBuffer wrap(ByteBuffer nioBuffer)
ByteBuffer
into MINA buffer.
public static ByteBuffer wrap(byte[] byteArray)
public static ByteBuffer wrap(byte[] byteArray, int offset, int length)
public abstract void acquire()
release()
as many
as you invoked this method to release this buffer.
IllegalStateException
- if you attempt to acquire already
released buffer.public abstract void release()
IllegalStateException
- if you attempt to release already
released buffer.public abstract ByteBuffer buf()
public abstract boolean isDirect()
public abstract int capacity()
public abstract boolean isAutoExpand()
public abstract ByteBuffer setAutoExpand(boolean autoExpand)
public abstract boolean isPooled()
The default value of this property is true if and only if you
allocated this buffer using allocate(int)
or allocate(int, boolean)
,
or false otherwise. (i.e. wrap(byte[])
, wrap(byte[], int, int)
,
and wrap(java.nio.ByteBuffer)
)
public abstract void setPooled(boolean pooled)
The default value of this property is true if and only if you
allocated this buffer using allocate(int)
or allocate(int, boolean)
,
or false otherwise. (i.e. wrap(byte[])
, wrap(byte[], int, int)
,
and wrap(java.nio.ByteBuffer)
)
public abstract int position()
public abstract ByteBuffer position(int newPosition)
public abstract int limit()
public abstract ByteBuffer limit(int newLimit)
public abstract ByteBuffer mark()
public abstract ByteBuffer reset()
public abstract ByteBuffer clear()
public abstract ByteBuffer flip()
public abstract ByteBuffer rewind()
public abstract int remaining()
public abstract boolean hasRemaining()
public abstract byte get()
public abstract short getUnsigned()
public abstract ByteBuffer put(byte b)
public abstract byte get(int index)
public abstract short getUnsigned(int index)
public abstract ByteBuffer put(int index, byte b)
public abstract ByteBuffer get(byte[] dst, int offset, int length)
public abstract ByteBuffer get(byte[] dst)
public abstract ByteBuffer put(ByteBuffer src)
public abstract ByteBuffer put(ByteBuffer src)
public abstract ByteBuffer put(byte[] src, int offset, int length)
public abstract ByteBuffer put(byte[] src)
public abstract ByteBuffer compact()
public abstract String toString()
toString
in class Object
public abstract int hashCode()
hashCode
in class Object
public abstract boolean equals(Object ob)
equals
in class Object
public abstract int compareTo(ByteBuffer that)
public abstract ByteOrder order()
public abstract ByteBuffer order(ByteOrder bo)
public abstract char getChar()
public abstract ByteBuffer putChar(char value)
public abstract char getChar(int index)
public abstract ByteBuffer putChar(int index, char value)
public abstract CharBuffer asCharBuffer()
public abstract short getShort()
public abstract int getUnsignedShort()
public abstract ByteBuffer putShort(short value)
public abstract short getShort(int index)
public abstract int getUnsignedShort(int index)
public abstract ByteBuffer putShort(int index, short value)
public abstract ShortBuffer asShortBuffer()
public abstract int getInt()
public abstract long getUnsignedInt()
public abstract ByteBuffer putInt(int value)
public abstract int getInt(int index)
public abstract long getUnsignedInt(int index)
public abstract ByteBuffer putInt(int index, int value)
public abstract IntBuffer asIntBuffer()
public abstract long getLong()
public abstract ByteBuffer putLong(long value)
public abstract long getLong(int index)
public abstract ByteBuffer putLong(int index, long value)
public abstract LongBuffer asLongBuffer()
public abstract float getFloat()
public abstract ByteBuffer putFloat(float value)
public abstract float getFloat(int index)
public abstract ByteBuffer putFloat(int index, float value)
public abstract FloatBuffer asFloatBuffer()
public abstract double getDouble()
public abstract ByteBuffer putDouble(double value)
public abstract double getDouble(int index)
public abstract ByteBuffer putDouble(int index, double value)
public abstract DoubleBuffer asDoubleBuffer()
public abstract String getHexDump()
public 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.
CharacterCodingException
public 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 read
CharacterCodingException
public abstract ByteBuffer putString(CharSequence in, 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.
BufferOverflowException
- if the specified string doesn't fit
CharacterCodingException
public abstract ByteBuffer putString(CharSequence in, 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.
fieldSize
- the maximum number of bytes to write
CharacterCodingException
public abstract ByteBuffer skip(int size)
size
bytes.
public abstract ByteBuffer fill(byte value, int size)
public abstract ByteBuffer fillAndReset(byte value, int size)
public abstract ByteBuffer fill(int size)
NUL (0x00)
.
This method moves buffer position forward.
public abstract ByteBuffer fillAndReset(int size)
NUL (0x00)
.
This method does not change buffer position.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |