Class IoBuffer
- All Implemented Interfaces:
Comparable<IoBuffer>
- Direct Known Subclasses:
AbstractIoBuffer, IoBufferWrapper
This is a replacement for ByteBuffer. Please refer to ByteBuffer documentation
for preliminary usage. MINA does not use NIO ByteBuffer directly for two reasons:
- It doesn't provide useful getters and putters such as
fill,get/putString, andget/putAsciiInt()enough. - It is difficult to write variable-length data due to its fixed capacity
Allocation
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);
Wrapping existing NIO buffers and arrays
This class provides a few wrap(...) methods that wraps any NIO buffers and byte arrays.
AutoExpand
Writing variable-length data using NIO ByteBuffers is not really easy, and it is because
its size is fixed. IoBuffer 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");
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.
AutoShrink
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 shrink() method manually to shrink the capacity of the buffer.
The underlying ByteBuffer is reallocated by IoBuffer behind the scene, and
therefore buf() will return a different ByteBuffer instance once capacity
changes. Please also note compact() or shrink() will not decrease the capacity
if the new capacity is less than the minimumCapacity() of the buffer.
Derived Buffers
Derived buffers are the buffers which were created by duplicate(), slice(), or
asReadOnlyBuffer(). They are useful especially when you broadcast the same messages to
multiple
invalid reference
IoSessionsetAutoExpand(boolean)
or setAutoShrink(boolean) with true parameter will raise an
IllegalStateException.
Changing Buffer Allocation Policy
IoBufferAllocator interface lets you override the default buffer management behavior.
There are two allocators provided out-of-the-box:
SimpleBufferAllocator(default)CachedBufferAllocator
setAllocator(IoBufferAllocator).
- Version:
- $Rev: 748525 $, $Date: 2009-02-27 14:45:31 +0100 (Fri, 27 Feb 2009) $
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static IoBufferAllocatorThe allocator used to create new buffersprivate static booleanA flag indicating which type of buffer we are using : heap or direct -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic IoBufferallocate(int capacity) Returns the direct or heap buffer which is capable to store the specified amount of bytes.static IoBufferallocate(int capacity, boolean direct) Returns the buffer which is capable of the specified size.abstract byte[]array()abstract intabstract CharBufferabstract DoubleBufferabstract FloatBufferabstract InputStreamReturns anInputStreamthat reads the data from this buffer.abstract IntBufferabstract LongBufferabstract OutputStreamReturns anOutputStreamthat appends the data into this buffer.abstract IoBufferabstract ShortBufferabstract ByteBufferbuf()Returns the underlying NIO buffer instance.abstract intcapacity()abstract IoBuffercapacity(int newCapacity) Increases the capacity of this buffer.abstract IoBufferclear()abstract IoBuffercompact()abstract IoBufferabstract IoBufferexpand(int expectedRemaining) Changes the capacity and limit of this buffer so this buffer get the specified expectedRemaining room from the current position.abstract IoBufferexpand(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 IoBufferfill(byte value, int size) Fills this buffer with the specified value.abstract IoBufferfill(int size) Fills this buffer withNUL (0x00).abstract IoBufferfillAndReset(byte value, int size) Fills this buffer with the specified value.abstract IoBufferfillAndReset(int size) Fills this buffer withNUL (0x00).abstract IoBufferflip()abstract voidfree()Declares this buffer and all its derived buffers are not used anymore so that it can be reused by someIoBufferAllocatorimplementations.abstract byteget()abstract IoBufferget(byte[] dst) abstract IoBufferget(byte[] dst, int offset, int length) abstract byteget(int index) static IoBufferAllocatorReturns the allocator used by existing and new buffersabstract chargetChar()abstract chargetChar(int index) abstract doubleabstract doublegetDouble(int index) abstract <E extends Enum<E>>
EReads a byte from the buffer and returns the correlating enum constant defined by the specified enum type.abstract <E extends Enum<E>>
EReads a byte from the buffer and returns the correlating enum constant defined by the specified enum type.abstract <E extends Enum<E>>
EgetEnumInt(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>>
EgetEnumInt(Class<E> enumClass) Reads an int from the buffer and returns the correlating enum constant defined by the specified enum type.getEnumSet(int index, Class<E> enumClass) Reads a byte sized bit vector and converts it to anEnumSet.getEnumSet(Class<E> enumClass) Reads a byte sized bit vector and converts it to anEnumSet.getEnumSetInt(int index, Class<E> enumClass) Reads an int sized bit vector and converts it to anEnumSet.getEnumSetInt(Class<E> enumClass) Reads an int sized bit vector and converts it to anEnumSet.getEnumSetLong(int index, Class<E> enumClass) Reads a long sized bit vector and converts it to anEnumSet.getEnumSetLong(Class<E> enumClass) Reads a long sized bit vector and converts it to anEnumSet.getEnumSetShort(int index, Class<E> enumClass) Reads a short sized bit vector and converts it to anEnumSet.getEnumSetShort(Class<E> enumClass) Reads a short sized bit vector and converts it to anEnumSet.abstract <E extends Enum<E>>
EgetEnumShort(int index, 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>>
EgetEnumShort(Class<E> enumClass) Reads a short from the buffer and returns the correlating enum constant defined by the specified enum type.abstract floatgetFloat()abstract floatgetFloat(int index) abstract StringReturns hexdump of this buffer.abstract StringgetHexDump(int lengthLimit) Return hexdump of this buffer with limited length.abstract intgetInt()abstract intgetInt(int index) abstract longgetLong()abstract longgetLong(int index) abstract intRelative get method for reading a medium int value.abstract intgetMediumInt(int index) Absolute get method for reading a medium int value.abstract ObjectReads a Java object from the buffer using the contextClassLoaderof the current thread.abstract ObjectgetObject(ClassLoader classLoader) Reads a Java object from the buffer using the specified classLoader.abstract StringgetPrefixedString(int prefixLength, CharsetDecoder decoder) Reads a string which has a length field before the actual encoded string, using the specifieddecoderand returns it.abstract StringgetPrefixedString(CharsetDecoder decoder) Reads a string which has a 16-bit length field before the actual encoded string, using the specifieddecoderand returns it.abstract shortgetShort()abstract shortgetShort(int index) abstract IoBuffergetSlice(int length) TODO document me.abstract IoBuffergetSlice(int index, int length) TODO document me.abstract StringgetString(int fieldSize, CharsetDecoder decoder) Reads aNUL-terminated string from this buffer using the specifieddecoderand returns it.abstract StringgetString(CharsetDecoder decoder) Reads aNUL-terminated string from this buffer using the specifieddecoderand returns it.abstract shortReads one unsigned byte as a short integer.abstract shortgetUnsigned(int index) Reads one byte as an unsigned short integer.abstract longReads four bytes unsigned integer.abstract longgetUnsignedInt(int index) Reads four bytes unsigned integer.abstract intRelative get method for reading an unsigned medium int value.abstract intgetUnsignedMediumInt(int index) Absolute get method for reading an unsigned medium int value.abstract intReads two bytes unsigned integer.abstract intgetUnsignedShort(int index) Reads two bytes unsigned integer.abstract booleanhasArray()abstract booleanabstract intindexOf(byte b) Returns the first occurence position of the specified byte from the current position to the current limit.abstract booleanReturns true if and only if autoExpand is turned on.abstract booleanReturns true if and only if autoShrink is turned on.abstract booleanreturns true if and only if this buffer is derived from other buffer viaduplicate(),slice()orasReadOnlyBuffer().abstract booleanisDirect()abstract booleanstatic booleanReturns true if and only if a direct buffer is allocated by default when the type of the new buffer is not specified.abstract intlimit()abstract IoBufferlimit(int newLimit) abstract IoBuffermark()abstract intReturns the position of the current mark.abstract intabstract IoBufferminimumCapacity(int minimumCapacity) protected static intnormalizeCapacity(int requestedCapacity) Normalizes the specified capacity of the buffer to power of 2, which is often helpful for optimal memory usage and performance.abstract ByteOrderorder()abstract IoBufferabstract intposition()abstract IoBufferposition(int newPosition) abstract booleanprefixedDataAvailable(int prefixLength) Returns true if this buffer contains a data which has a data length as a prefix and the buffer has remaining data as enough as specified in the data length field.abstract booleanprefixedDataAvailable(int prefixLength, int maxDataLength) Returns true if this buffer contains a data which has a data length as a prefix and the buffer has remaining data as enough as specified in the data length field.abstract IoBufferput(byte b) abstract IoBufferput(byte[] src) abstract IoBufferput(byte[] src, int offset, int length) abstract IoBufferput(int index, byte b) abstract IoBufferWrites the content of the specified src into this buffer.abstract IoBufferput(ByteBuffer src) Writes the content of the specified src into this buffer.abstract IoBufferputChar(char value) abstract IoBufferputChar(int index, char value) abstract IoBufferputDouble(double value) abstract IoBufferputDouble(int index, double value) abstract IoBufferWrites an enum's ordinal value to the buffer as a byte.abstract IoBufferWrites an enum's ordinal value to the buffer as a byte.abstract IoBufferputEnumInt(int index, Enum<?> e) Writes an enum's ordinal value to the buffer as an integer.abstract IoBufferputEnumInt(Enum<?> e) Writes an enum's ordinal value to the buffer as an integer.putEnumSet(int index, Set<E> set) Writes the specifiedSetto the buffer as a byte sized bit vector.putEnumSet(Set<E> set) Writes the specifiedSetto the buffer as a byte sized bit vector.putEnumSetInt(int index, Set<E> set) Writes the specifiedSetto the buffer as an int sized bit vector.putEnumSetInt(Set<E> set) Writes the specifiedSetto the buffer as an int sized bit vector.putEnumSetLong(int index, Set<E> set) Writes the specifiedSetto the buffer as a long sized bit vector.putEnumSetLong(Set<E> set) Writes the specifiedSetto the buffer as a long sized bit vector.putEnumSetShort(int index, Set<E> set) Writes the specifiedSetto the buffer as a short sized bit vector.putEnumSetShort(Set<E> set) Writes the specifiedSetto the buffer as a short sized bit vector.abstract IoBufferputEnumShort(int index, Enum<?> e) Writes an enum's ordinal value to the buffer as a short.abstract IoBufferputEnumShort(Enum<?> e) Writes an enum's ordinal value to the buffer as a short.abstract IoBufferputFloat(float value) abstract IoBufferputFloat(int index, float value) abstract IoBufferputInt(int value) abstract IoBufferputInt(int index, int value) abstract IoBufferputLong(int index, long value) abstract IoBufferputLong(long value) abstract IoBufferputMediumInt(int value) Relative put method for writing a medium int value.abstract IoBufferputMediumInt(int index, int value) Absolute put method for writing a medium int value.abstract IoBufferWrites the specified Java object to the buffer.abstract IoBufferputPrefixedString(CharSequence val, int prefixLength, int padding, byte padValue, CharsetEncoder encoder) Writes the content ofininto this buffer as a string which has a 16-bit length field before the actual encoded string, using the specifiedencoder.abstract IoBufferputPrefixedString(CharSequence in, int prefixLength, int padding, CharsetEncoder encoder) Writes the content ofininto this buffer as a string which has a 16-bit length field before the actual encoded string, using the specifiedencoder.abstract IoBufferputPrefixedString(CharSequence in, int prefixLength, CharsetEncoder encoder) Writes the content ofininto this buffer as a string which has a 16-bit length field before the actual encoded string, using the specifiedencoder.abstract IoBufferputPrefixedString(CharSequence in, CharsetEncoder encoder) Writes the content ofininto this buffer as a string which has a 16-bit length field before the actual encoded string, using the specifiedencoder.abstract IoBufferputShort(int index, short value) abstract IoBufferputShort(short value) abstract IoBufferputString(CharSequence val, int fieldSize, CharsetEncoder encoder) Writes the content ofininto this buffer as aNUL-terminated string using the specifiedencoder.abstract IoBufferputString(CharSequence val, CharsetEncoder encoder) Writes the content ofininto this buffer using the specifiedencoder.abstract intabstract IoBufferreset()abstract IoBufferrewind()static voidsetAllocator(IoBufferAllocator newAllocator) Sets the allocator used by existing and new buffersabstract IoBuffersetAutoExpand(boolean autoExpand) Turns on or off autoExpand.abstract IoBuffersetAutoShrink(boolean autoShrink) Turns on or off autoShrink.static voidsetUseDirectBuffer(boolean useDirectBuffer) Sets if a direct buffer should be allocated by default when the type of the new buffer is not specified.abstract IoBuffershrink()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 IoBufferskip(int size) Forwards the position of this buffer as the specifiedsizebytes.abstract IoBufferslice()abstract IoBuffersweep()Clears this buffer and fills its content with NUL.abstract IoBuffersweep(byte value) double Clears this buffer and fills its content with value.static IoBufferwrap(byte[] byteArray) Wraps the specified byte array into MINA heap buffer.static IoBufferwrap(byte[] byteArray, int offset, int length) Wraps the specified byte array into MINA heap buffer.static IoBufferwrap(ByteBuffer nioBuffer) Wraps the specified NIOByteBufferinto MINA buffer.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Comparable
compareTo
-
Field Details
-
allocator
The allocator used to create new buffers -
useDirectBuffer
private static boolean useDirectBufferA flag indicating which type of buffer we are using : heap or direct
-
-
Constructor Details
-
IoBuffer
protected IoBuffer()Creates a new instance. This is an empty constructor.
-
-
Method Details
-
getAllocator
Returns the allocator used by existing and new buffers -
setAllocator
Sets the allocator used by existing and new buffers -
isUseDirectBuffer
public static boolean isUseDirectBuffer()Returns true if and only if a direct buffer is allocated by default when the type of the new buffer is not specified. The default value is false. -
setUseDirectBuffer
public 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. The default value is false. -
allocate
Returns the direct or heap buffer which is capable to store the specified amount of bytes.- Parameters:
capacity- the capacity of the buffer- See Also:
-
allocate
Returns the buffer which is capable of the specified size.- Parameters:
capacity- the capacity of the bufferdirect- true to get a direct buffer, false to get a heap buffer.
-
wrap
Wraps the specified NIOByteBufferinto MINA buffer. -
wrap
Wraps the specified byte array into MINA heap buffer. -
wrap
Wraps the specified byte array into MINA heap buffer. -
normalizeCapacity
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. If it is greater than or equal toInteger.MAX_VALUE, it returnsInteger.MAX_VALUE. If it is zero, it returns zero. -
free
public abstract void free()Declares this buffer and all its derived buffers are not used anymore so that it can be reused by someIoBufferAllocatorimplementations. It is not mandatory to call this method, but you might want to invoke this method for maximum performance. -
buf
Returns the underlying NIO buffer instance. -
isDirect
public abstract boolean isDirect()- See Also:
-
isDerived
public abstract boolean isDerived()returns true if and only if this buffer is derived from other buffer viaduplicate(),slice()orasReadOnlyBuffer(). -
isReadOnly
public abstract boolean isReadOnly()- See Also:
-
minimumCapacity
-
minimumCapacity
-
capacity
public abstract int capacity()- See Also:
-
capacity
Increases the capacity of this buffer. If the new capacity is less than or equal to the current capacity, this method returns silently. If the new capacity is greater than the current capacity, the buffer is reallocated while retaining the position, limit, mark and the content of the buffer. -
isAutoExpand
public abstract boolean isAutoExpand()Returns true if and only if autoExpand is turned on. -
setAutoExpand
Turns on or off autoExpand. -
isAutoShrink
public abstract boolean isAutoShrink()Returns true if and only if autoShrink is turned on. -
setAutoShrink
Turns on or off autoShrink. -
expand
Changes the capacity and limit of this buffer so this buffer get the specified expectedRemaining room from the current position. This method works even if you didn't set autoExpand to true. -
expand
Changes the capacity and limit of this buffer so this buffer get the specified expectedRemaining room from the specified position. This method works even if you didn't set autoExpand to true. -
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. The capacity of the buffer never becomes less thanminimumCapacity(). The mark is discarded once the capacity changes. -
position
public abstract int position()- See Also:
-
position
- See Also:
-
limit
public abstract int limit()- See Also:
-
limit
- See Also:
-
mark
- See Also:
-
markValue
public abstract int markValue()Returns the position of the current mark. This method returns -1 if no mark is set. -
reset
- See Also:
-
clear
- See Also:
-
sweep
Clears this buffer and fills its content with NUL. The position is set to zero, the limit is set to the capacity, and the mark is discarded. -
sweep
double Clears this buffer and fills its content with value. The position is set to zero, the limit is set to the capacity, and the mark is discarded. -
flip
- See Also:
-
rewind
- See Also:
-
remaining
public abstract int remaining()- See Also:
-
hasRemaining
public abstract boolean hasRemaining()- See Also:
-
duplicate
- See Also:
-
slice
- See Also:
-
asReadOnlyBuffer
- See Also:
-
hasArray
public abstract boolean hasArray()- See Also:
-
array
public abstract byte[] array()- See Also:
-
arrayOffset
public abstract int arrayOffset()- See Also:
-
get
public abstract byte get()- See Also:
-
getUnsigned
public abstract short getUnsigned()Reads one unsigned byte as a short integer. -
put
- See Also:
-
get
public abstract byte get(int index) - See Also:
-
getUnsigned
public abstract short getUnsigned(int index) Reads one byte as an unsigned short integer. -
put
- See Also:
-
get
- See Also:
-
get
- See Also:
-
getSlice
TODO document me. -
getSlice
TODO document me. -
put
Writes the content of the specified src into this buffer. -
put
-
put
- See Also:
-
put
- See Also:
-
compact
- See Also:
-
order
- See Also:
-
order
-
getChar
public abstract char getChar()- See Also:
-
putChar
- See Also:
-
getChar
public abstract char getChar(int index) - See Also:
-
putChar
- See Also:
-
asCharBuffer
- See Also:
-
getShort
public abstract short getShort()- See Also:
-
getUnsignedShort
public abstract int getUnsignedShort()Reads two bytes unsigned integer. -
putShort
- See Also:
-
getShort
public abstract short getShort(int index) - See Also:
-
getUnsignedShort
public abstract int getUnsignedShort(int index) Reads two bytes unsigned integer. -
putShort
- See Also:
-
asShortBuffer
- See Also:
-
getInt
public abstract int getInt()- See Also:
-
getUnsignedInt
public abstract long getUnsignedInt()Reads four bytes unsigned integer. -
getMediumInt
public abstract int getMediumInt()Relative get method for reading a medium int value.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.
- Returns:
- The medium int value at the buffer's current position
-
getUnsignedMediumInt
public abstract int getUnsignedMediumInt()Relative get method for reading an unsigned medium int value.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.
- Returns:
- The unsigned medium int value at the buffer's current position
-
getMediumInt
public abstract int getMediumInt(int index) Absolute get method for reading a medium int value.Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order.
- Parameters:
index- The index from which the medium int will be read- Returns:
- The medium int value at the given index
- Throws:
IndexOutOfBoundsException- If index is negative or not smaller than the buffer's limit
-
getUnsignedMediumInt
public abstract int getUnsignedMediumInt(int index) Absolute get method for reading an unsigned medium int value.Reads the next three bytes at this buffer's current position, composing them into an int value according to the current byte order.
- Parameters:
index- The index from which the unsigned medium int will be read- Returns:
- The unsigned medium int value at the given index
- Throws:
IndexOutOfBoundsException- If index is negative or not smaller than the buffer's limit
-
putMediumInt
Relative put method for writing a medium 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.
- Parameters:
value- The medium int value to be written- Returns:
- This buffer
- Throws:
BufferOverflowException- If there are fewer than three bytes remaining in this bufferReadOnlyBufferException- If this buffer is read-only
-
putMediumInt
Absolute put method for writing a medium int value.Writes three bytes containing the given int value, in the current byte order, into this buffer at the given index.
- Parameters:
index- The index at which the bytes will be writtenvalue- The medium int value to be written- Returns:
- This buffer
- Throws:
IndexOutOfBoundsException- If index is negative or not smaller than the buffer's limit, minus threeReadOnlyBufferException- If this buffer is read-only
-
putInt
- See Also:
-
getInt
public abstract int getInt(int index) - See Also:
-
getUnsignedInt
public abstract long getUnsignedInt(int index) Reads four bytes unsigned integer. -
putInt
- See Also:
-
asIntBuffer
- See Also:
-
getLong
public abstract long getLong()- See Also:
-
putLong
- See Also:
-
getLong
public abstract long getLong(int index) - See Also:
-
putLong
- See Also:
-
asLongBuffer
- See Also:
-
getFloat
public abstract float getFloat()- See Also:
-
putFloat
- See Also:
-
getFloat
public abstract float getFloat(int index) - See Also:
-
putFloat
- See Also:
-
asFloatBuffer
- See Also:
-
getDouble
public abstract double getDouble()- See Also:
-
putDouble
- See Also:
-
getDouble
public abstract double getDouble(int index) - See Also:
-
putDouble
- See Also:
-
asDoubleBuffer
- See Also:
-
asInputStream
Returns anInputStreamthat reads the data from this buffer.InputStream.read()returns -1 if the buffer position reaches to the limit. -
asOutputStream
Returns anOutputStreamthat appends the data into this buffer. Please note that theOutputStream.write(int)will throw aBufferOverflowExceptioninstead of anIOExceptionin case of buffer overflow. Please set autoExpand property by callingsetAutoExpand(boolean)to prevent the unexpected runtime exception. -
getHexDump
Returns hexdump of this buffer. The data and pointer are not changed as a result of this method call.- Returns:
- hexidecimal representation of this buffer
-
getHexDump
Return hexdump of this buffer with limited length.- Parameters:
lengthLimit- The maximum number of bytes to dump from the current buffer position.- Returns:
- hexidecimal representation of this buffer
-
getString
Reads aNUL-terminated string from this buffer using the specifieddecoderand returns it. This method reads until the limit of this buffer if no NUL is found.- Throws:
CharacterCodingException
-
getString
public abstract String getString(int fieldSize, CharsetDecoder decoder) throws CharacterCodingException Reads aNUL-terminated string from this buffer using the specifieddecoderand returns it.- Parameters:
fieldSize- the maximum number of bytes to read- Throws:
CharacterCodingException
-
putString
public abstract IoBuffer putString(CharSequence val, CharsetEncoder encoder) throws CharacterCodingException Writes the content ofininto this buffer using the specifiedencoder. This method doesn't terminate string with NUL. You have to do it by yourself.- Throws:
BufferOverflowException- if the specified string doesn't fitCharacterCodingException
-
putString
public abstract IoBuffer putString(CharSequence val, int fieldSize, CharsetEncoder encoder) throws CharacterCodingException Writes the content ofininto this buffer as aNUL-terminated string using the specifiedencoder.If the charset name of the encoder is UTF-16, you cannot specify odd
fieldSize, and this method will append twoNULs as a terminator.Please note that this method doesn't terminate with
NULif the input string is longer than fieldSize.- Parameters:
fieldSize- the maximum number of bytes to write- Throws:
CharacterCodingException
-
getPrefixedString
Reads a string which has a 16-bit length field before the actual encoded string, using the specifieddecoderand returns it. This method is a shortcut for getPrefixedString(2, decoder).- Throws:
CharacterCodingException
-
getPrefixedString
public abstract String getPrefixedString(int prefixLength, CharsetDecoder decoder) throws CharacterCodingException Reads a string which has a length field before the actual encoded string, using the specifieddecoderand returns it.- Parameters:
prefixLength- the length of the length field (1, 2, or 4)- Throws:
CharacterCodingException
-
putPrefixedString
public abstract IoBuffer putPrefixedString(CharSequence in, CharsetEncoder encoder) throws CharacterCodingException Writes the content ofininto this buffer as a string which has a 16-bit length field before the actual encoded string, using the specifiedencoder. This method is a shortcut for putPrefixedString(in, 2, 0, encoder).- Throws:
BufferOverflowException- if the specified string doesn't fitCharacterCodingException
-
putPrefixedString
public abstract IoBuffer putPrefixedString(CharSequence in, int prefixLength, CharsetEncoder encoder) throws CharacterCodingException Writes the content ofininto this buffer as a string which has a 16-bit length field before the actual encoded string, using the specifiedencoder. This method is a shortcut for putPrefixedString(in, prefixLength, 0, encoder).- Parameters:
prefixLength- the length of the length field (1, 2, or 4)- Throws:
BufferOverflowException- if the specified string doesn't fitCharacterCodingException
-
putPrefixedString
public abstract IoBuffer putPrefixedString(CharSequence in, int prefixLength, int padding, CharsetEncoder encoder) throws CharacterCodingException Writes the content ofininto this buffer as a string which has a 16-bit length field before the actual encoded string, using the specifiedencoder. This method is a shortcut for putPrefixedString(in, prefixLength, padding, ( byte ) 0, encoder) .- Parameters:
prefixLength- the length of the length field (1, 2, or 4)padding- the number of padded NULs (1 (or 0), 2, or 4)- Throws:
BufferOverflowException- if the specified string doesn't fitCharacterCodingException
-
putPrefixedString
public abstract IoBuffer putPrefixedString(CharSequence val, int prefixLength, int padding, byte padValue, CharsetEncoder encoder) throws CharacterCodingException Writes the content ofininto this buffer as a string which has a 16-bit length field before the actual encoded string, using the specifiedencoder.- Parameters:
prefixLength- 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 bytes- Throws:
BufferOverflowException- if the specified string doesn't fitCharacterCodingException
-
getObject
Reads a Java object from the buffer using the contextClassLoaderof the current thread.- Throws:
ClassNotFoundException
-
getObject
Reads a Java object from the buffer using the specified classLoader.- Throws:
ClassNotFoundException
-
putObject
-
prefixedDataAvailable
public abstract boolean prefixedDataAvailable(int prefixLength) Returns true if this buffer contains a data which has a data length as a prefix and the buffer has remaining data as enough as specified in the data length field. This method is identical with prefixedDataAvailable( prefixLength, Integer.MAX_VALUE ). Please not that using this method can allow DoS (Denial of Service) attack in case the remote peer sends too big data length value. It is recommended to useprefixedDataAvailable(int, int)instead.- Parameters:
prefixLength- the length of the prefix field (1, 2, or 4)- Throws:
IllegalArgumentException- if prefixLength is wrongBufferDataException- if data length is negative
-
prefixedDataAvailable
public abstract boolean prefixedDataAvailable(int prefixLength, int maxDataLength) Returns true if this buffer contains a data which has a data length as a prefix and the buffer has remaining data as enough as specified in the data length field.- Parameters:
prefixLength- the length of the prefix field (1, 2, or 4)maxDataLength- the allowed maximum of the read data length- Throws:
IllegalArgumentException- if prefixLength is wrongBufferDataException- if data length is negative or greater then maxDataLength
-
indexOf
public abstract int indexOf(byte b) Returns the first occurence position of the specified byte from the current position to the current limit.- Returns:
- -1 if the specified byte is not found
-
skip
Forwards the position of this buffer as the specifiedsizebytes. -
fill
Fills this buffer with the specified value. This method moves buffer position forward. -
fillAndReset
Fills this buffer with the specified value. This method does not change buffer position. -
fill
Fills this buffer withNUL (0x00). This method moves buffer position forward. -
fillAndReset
Fills this buffer withNUL (0x00). This method does not change buffer position. -
getEnum
-
getEnum
Reads a byte from the buffer and returns the correlating enum constant defined by the specified enum type.- Type Parameters:
E- The enum type to return- Parameters:
index- the index from which the byte will be readenumClass- The enum's class object
-
getEnumShort
-
getEnumShort
Reads a short from the buffer and returns the correlating enum constant defined by the specified enum type.- Type Parameters:
E- The enum type to return- Parameters:
index- the index from which the bytes will be readenumClass- The enum's class object
-
getEnumInt
-
getEnumInt
Reads an int from the buffer and returns the correlating enum constant defined by the specified enum type.- Type Parameters:
E- The enum type to return- Parameters:
index- the index from which the bytes will be readenumClass- The enum's class object
-
putEnum
-
putEnum
-
putEnumShort
-
putEnumShort
-
putEnumInt
-
putEnumInt
-
getEnumSet
Reads a byte sized bit vector and converts it to anEnumSet.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.
- Type Parameters:
E- the enum type- Parameters:
enumClass- the enum class used to create the EnumSet- Returns:
- the EnumSet representation of the bit vector
-
getEnumSet
Reads a byte sized bit vector and converts it to anEnumSet.- Type Parameters:
E- the enum type- Parameters:
index- the index from which the byte will be readenumClass- the enum class used to create the EnumSet- Returns:
- the EnumSet representation of the bit vector
- See Also:
-
getEnumSetShort
Reads a short sized bit vector and converts it to anEnumSet.- Type Parameters:
E- the enum type- Parameters:
enumClass- the enum class used to create the EnumSet- Returns:
- the EnumSet representation of the bit vector
- See Also:
-
getEnumSetShort
Reads a short sized bit vector and converts it to anEnumSet.- Type Parameters:
E- the enum type- Parameters:
index- the index from which the bytes will be readenumClass- the enum class used to create the EnumSet- Returns:
- the EnumSet representation of the bit vector
- See Also:
-
getEnumSetInt
Reads an int sized bit vector and converts it to anEnumSet.- Type Parameters:
E- the enum type- Parameters:
enumClass- the enum class used to create the EnumSet- Returns:
- the EnumSet representation of the bit vector
- See Also:
-
getEnumSetInt
Reads an int sized bit vector and converts it to anEnumSet.- Type Parameters:
E- the enum type- Parameters:
index- the index from which the bytes will be readenumClass- the enum class used to create the EnumSet- Returns:
- the EnumSet representation of the bit vector
- See Also:
-
getEnumSetLong
Reads a long sized bit vector and converts it to anEnumSet.- Type Parameters:
E- the enum type- Parameters:
enumClass- the enum class used to create the EnumSet- Returns:
- the EnumSet representation of the bit vector
- See Also:
-
getEnumSetLong
Reads a long sized bit vector and converts it to anEnumSet.- Type Parameters:
E- the enum type- Parameters:
index- the index from which the bytes will be readenumClass- the enum class used to create the EnumSet- Returns:
- the EnumSet representation of the bit vector
- See Also:
-
putEnumSet
-
putEnumSet
-
putEnumSetShort
-
putEnumSetShort
Writes the specifiedSetto the buffer as a short sized bit vector.- Type Parameters:
E- the enum type of the Set- Parameters:
index- the index at which the bytes will be writtenset- the enum set to write to the buffer
-
putEnumSetInt
-
putEnumSetInt
Writes the specifiedSetto the buffer as an int sized bit vector.- Type Parameters:
E- the enum type of the Set- Parameters:
index- the index at which the bytes will be writtenset- the enum set to write to the buffer
-
putEnumSetLong
-
putEnumSetLong
Writes the specifiedSetto the buffer as a long sized bit vector.- Type Parameters:
E- the enum type of the Set- Parameters:
index- the index at which the bytes will be writtenset- the enum set to write to the buffer
-