Class ByteBuffer
- java.lang.Object
-
- org.apache.mina.common.ByteBuffer
-
- All Implemented Interfaces:
java.lang.Comparable<ByteBuffer>
- Direct Known Subclasses:
BaseByteBuffer,ByteBufferProxy
public abstract class ByteBuffer extends java.lang.Object implements java.lang.Comparable<ByteBuffer>
A byte buffer used by MINA applications.This is a replacement for
ByteBuffer. Please refer toByteBufferandBufferdocumentation for usage. MINA does not use NIOByteBufferdirectly for two reasons:- It doesn't provide useful getters and putters such as
fill,get/putString, andget/putAsciiInt()enough. - It is hard to distinguish if the buffer is created from MINA buffer pool or not. MINA have to return used buffers back to pool.
- It is difficult to write variable-length data due to its fixed capacity
Allocation
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);
Acquire/Release
Please note that you never need to release the allocated buffer because MINA will release it automatically when:
- You pass the buffer by calling
IoSession.write(Object). - You pass the buffer by calling
IoFilter.NextFilter.filterWrite(IoSession,IoFilter.WriteRequest). - You pass the buffer by calling
ProtocolEncoderOutput.write(ByteBuffer).
ByteBufferwhich is passed as a parameter ofIoHandler.messageReceived(IoSession, Object)method. They are released automatically when the method returns.You have to release buffers manually by calling
release()when:- You allocated a buffer, but didn't pass the buffer to any of two methods above.
- You called
acquire()to prevent the buffer from being released.
Wrapping existing NIO buffers and arrays
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.AutoExpand
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
BufferOverflowExceptionorIndexOutOfBoundsException(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 and its limit will increase to the last position the string is written.Derived Buffers
Derived buffers are the buffers which were created by
duplicate(),slice(), orasReadOnlyBuffer(). They are useful especially when you broadcast the same messages to multipleIoSessions. Please note that the derived buffers are neither pooled nor auto-expandable. Trying to expand a derived buffer will raiseIllegalStateException.Changing Buffer Allocation and Management Policy
MINA provides a
You can change the allocator by callingByteBufferAllocatorinterface to let you override the default buffer management behavior. There are two allocators provided out-of-the-box:setAllocator(ByteBufferAllocator).- See Also:
ByteBufferAllocator
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedByteBuffer()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidacquire()Increases the internal reference count of this buffer to defer automatic release.static ByteBufferallocate(int capacity)Returns the direct or heap buffer which is capable of the specified size.static ByteBufferallocate(int capacity, boolean direct)Returns the buffer which is capable of the specified size.abstract byte[]array()abstract intarrayOffset()abstract java.nio.CharBufferasCharBuffer()abstract java.nio.DoubleBufferasDoubleBuffer()abstract java.nio.FloatBufferasFloatBuffer()java.io.InputStreamasInputStream()Returns anInputStreamthat reads the data from this buffer.abstract java.nio.IntBufferasIntBuffer()abstract java.nio.LongBufferasLongBuffer()java.io.OutputStreamasOutputStream()Returns anOutputStreamthat appends the data into this buffer.abstract ByteBufferasReadOnlyBuffer()abstract java.nio.ShortBufferasShortBuffer()protected ByteBufferautoExpand(int expectedRemaining)This method forwards the call toexpand(int)only when autoExpand property is true.protected ByteBufferautoExpand(int pos, int expectedRemaining)This method forwards the call toexpand(int)only when autoExpand property is true.abstract java.nio.ByteBufferbuf()Returns the underlying NIO buffer instance.abstract intcapacity()abstract ByteBuffercapacity(int newCapacity)Changes the capacity of this buffer.abstract ByteBufferclear()abstract ByteBuffercompact()intcompareTo(ByteBuffer that)abstract ByteBufferduplicate()booleanequals(java.lang.Object o)ByteBufferexpand(int expectedRemaining)Changes the capacity and limit of this buffer so this buffer get the specified expectedRemaining room from the current position.abstract ByteBufferexpand(int pos, int expectedRemaining)Changes the capacity and limit of this buffer so this buffer get the specified expectedRemaining room from the specified pos.ByteBufferfill(byte value, int size)Fills this buffer with the specified value.ByteBufferfill(int size)Fills this buffer withNUL (0x00).ByteBufferfillAndReset(byte value, int size)Fills this buffer with the specified value.ByteBufferfillAndReset(int size)Fills this buffer withNUL (0x00).abstract ByteBufferflip()abstract byteget()ByteBufferget(byte[] dst)abstract ByteBufferget(byte[] dst, int offset, int length)abstract byteget(int index)static ByteBufferAllocatorgetAllocator()Returns the current allocator which manages the allocated buffers.abstract chargetChar()abstract chargetChar(int index)abstract doublegetDouble()abstract doublegetDouble(int index)abstract floatgetFloat()abstract floatgetFloat(int index)java.lang.StringgetHexDump()Returns hexdump of this buffer.abstract intgetInt()abstract intgetInt(int index)abstract longgetLong()abstract longgetLong(int index)java.lang.ObjectgetObject()Reads a Java object from the buffer using the contextClassLoaderof the current thread.java.lang.ObjectgetObject(java.lang.ClassLoader classLoader)Reads a Java object from the buffer using the specified classLoader.java.lang.StringgetPrefixedString(int prefixLength, java.nio.charset.CharsetDecoder decoder)Reads a string which has a length field before the actual encoded string, using the specifieddecoderand returns it.java.lang.StringgetPrefixedString(java.nio.charset.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)java.lang.StringgetString(int fieldSize, java.nio.charset.CharsetDecoder decoder)Reads aNUL-terminated string from this buffer using the specifieddecoderand returns it.java.lang.StringgetString(java.nio.charset.CharsetDecoder decoder)Reads aNUL-terminated string from this buffer using the specifieddecoderand returns it.shortgetUnsigned()Reads one unsigned byte as a short integer.shortgetUnsigned(int index)Reads one byte as an unsigned short integer.longgetUnsignedInt()Reads four bytes unsigned integer.longgetUnsignedInt(int index)Reads four bytes unsigned integer.intgetUnsignedShort()Reads two bytes unsigned integer.intgetUnsignedShort(int index)Reads two bytes unsigned integer.inthashCode()booleanhasRemaining()abstract booleanisAutoExpand()Returns true if and only if autoExpand is turned on.abstract booleanisDirect()abstract booleanisPooled()Returns true if and only if this buffer is returned back to the buffer pool when released.abstract booleanisReadOnly()static booleanisUseDirectBuffers()abstract intlimit()abstract ByteBufferlimit(int newLimit)abstract ByteBuffermark()abstract intmarkValue()Returns the position of the current mark.abstract java.nio.ByteOrderorder()abstract ByteBufferorder(java.nio.ByteOrder bo)abstract intposition()abstract ByteBufferposition(int newPosition)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.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 ByteBufferput(byte b)ByteBufferput(byte[] src)abstract ByteBufferput(byte[] src, int offset, int length)abstract ByteBufferput(int index, byte b)abstract ByteBufferput(java.nio.ByteBuffer src)Writes the content of the specified src into this buffer.ByteBufferput(ByteBuffer src)Writes the content of the specified src into this buffer.abstract ByteBufferputChar(char value)abstract ByteBufferputChar(int index, char value)abstract ByteBufferputDouble(double value)abstract ByteBufferputDouble(int index, double value)abstract ByteBufferputFloat(float value)abstract ByteBufferputFloat(int index, float value)abstract ByteBufferputInt(int value)abstract ByteBufferputInt(int index, int value)abstract ByteBufferputLong(int index, long value)abstract ByteBufferputLong(long value)ByteBufferputObject(java.lang.Object o)Writes the specified Java object to the buffer.ByteBufferputPrefixedString(java.lang.CharSequence val, int prefixLength, int padding, byte padValue, java.nio.charset.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.ByteBufferputPrefixedString(java.lang.CharSequence in, int prefixLength, int padding, java.nio.charset.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.ByteBufferputPrefixedString(java.lang.CharSequence in, int prefixLength, java.nio.charset.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.ByteBufferputPrefixedString(java.lang.CharSequence in, java.nio.charset.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 ByteBufferputShort(int index, short value)abstract ByteBufferputShort(short value)ByteBufferputString(java.lang.CharSequence val, int fieldSize, java.nio.charset.CharsetEncoder encoder)Writes the content ofininto this buffer as aNUL-terminated string using the specifiedencoder.ByteBufferputString(java.lang.CharSequence val, java.nio.charset.CharsetEncoder encoder)Writes the content ofininto this buffer using the specifiedencoder.abstract voidrelease()Releases the specified buffer to buffer pool.intremaining()abstract ByteBufferreset()abstract ByteBufferrewind()static voidsetAllocator(ByteBufferAllocator newAllocator)Changes the current allocator with the specified one to manage the allocated buffers from now.abstract ByteBuffersetAutoExpand(boolean autoExpand)Turns on or off autoExpand.abstract voidsetPooled(boolean pooled)Sets whether this buffer is returned back to the buffer pool when released.static voidsetUseDirectBuffers(boolean useDirectBuffers)ByteBufferskip(int size)Forwards the position of this buffer as the specifiedsizebytes.abstract ByteBufferslice()ByteBuffersweep()Clears this buffer and fills its content with NUL.ByteBuffersweep(byte value)Clears this buffer and fills its content with value.java.lang.StringtoString()static ByteBufferwrap(byte[] byteArray)Wraps the specified byte array into MINA heap buffer.static ByteBufferwrap(byte[] byteArray, int offset, int length)Wraps the specified byte array into MINA heap buffer.static ByteBufferwrap(java.nio.ByteBuffer nioBuffer)Wraps the specified NIOByteBufferinto MINA buffer.
-
-
-
Method Detail
-
getAllocator
public static ByteBufferAllocator getAllocator()
Returns the current allocator which manages the allocated buffers.
-
setAllocator
public static void setAllocator(ByteBufferAllocator newAllocator)
Changes the current allocator with the specified one to manage the allocated buffers from now.
-
isUseDirectBuffers
public static boolean isUseDirectBuffers()
-
setUseDirectBuffers
public static void setUseDirectBuffers(boolean useDirectBuffers)
-
allocate
public static ByteBuffer allocate(int capacity)
Returns the direct or heap buffer which is capable of the specified size. This method tries to allocate direct buffer first, and then tries heap buffer if direct buffer memory is exhausted. Please useallocate(int, boolean)to allocate buffers of specific type.- Parameters:
capacity- the capacity of the buffer
-
allocate
public static ByteBuffer allocate(int capacity, boolean direct)
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
public static ByteBuffer wrap(java.nio.ByteBuffer nioBuffer)
Wraps the specified NIOByteBufferinto MINA buffer.
-
wrap
public static ByteBuffer wrap(byte[] byteArray)
Wraps the specified byte array into MINA heap buffer.
-
wrap
public static ByteBuffer wrap(byte[] byteArray, int offset, int length)
Wraps the specified byte array into MINA heap buffer. Please note that MINA buffers are going to be pooled, and therefore there can be waste of memory if you wrap your byte array specifying offset and length.
-
acquire
public abstract void acquire()
Increases the internal reference count of this buffer to defer automatic release. You have to invokerelease()as many as you invoked this method to release this buffer.- Throws:
java.lang.IllegalStateException- if you attempt to acquire already released buffer.
-
release
public abstract void release()
Releases the specified buffer to buffer pool.- Throws:
java.lang.IllegalStateException- if you attempt to release already released buffer.
-
buf
public abstract java.nio.ByteBuffer buf()
Returns the underlying NIO buffer instance.
-
isDirect
public abstract boolean isDirect()
- See Also:
ByteBuffer.isDirect()
-
isReadOnly
public abstract boolean isReadOnly()
- See Also:
Buffer.isReadOnly()
-
capacity
public abstract int capacity()
- See Also:
Buffer.capacity()
-
capacity
public abstract ByteBuffer capacity(int newCapacity)
Changes the capacity of this buffer.
-
isAutoExpand
public abstract boolean isAutoExpand()
Returns true if and only if autoExpand is turned on.
-
setAutoExpand
public abstract ByteBuffer setAutoExpand(boolean autoExpand)
Turns on or off autoExpand.
-
expand
public ByteBuffer expand(int expectedRemaining)
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
public abstract ByteBuffer expand(int pos, int expectedRemaining)
Changes the capacity and limit of this buffer so this buffer get the specified expectedRemaining room from the specified pos. This method works even if you didn't set autoExpand to true.
-
isPooled
public abstract boolean isPooled()
Returns true if and only if this buffer is returned back to the buffer pool when released.The default value of this property is true if and only if you allocated this buffer using
allocate(int)orallocate(int, boolean), or false otherwise. (i.e.wrap(byte[]),wrap(byte[], int, int), andwrap(java.nio.ByteBuffer))
-
setPooled
public abstract void setPooled(boolean pooled)
Sets whether this buffer is returned back to the buffer pool when released.The default value of this property is true if and only if you allocated this buffer using
allocate(int)orallocate(int, boolean), or false otherwise. (i.e.wrap(byte[]),wrap(byte[], int, int), andwrap(java.nio.ByteBuffer))
-
position
public abstract int position()
- See Also:
Buffer.position()
-
position
public abstract ByteBuffer position(int newPosition)
- See Also:
Buffer.position(int)
-
limit
public abstract int limit()
- See Also:
Buffer.limit()
-
limit
public abstract ByteBuffer limit(int newLimit)
- See Also:
Buffer.limit(int)
-
mark
public abstract ByteBuffer mark()
- See Also:
Buffer.mark()
-
markValue
public abstract int markValue()
Returns the position of the current mark. This method returns -1 if no mark is set.
-
reset
public abstract ByteBuffer reset()
- See Also:
Buffer.reset()
-
clear
public abstract ByteBuffer clear()
- See Also:
Buffer.clear()
-
sweep
public ByteBuffer 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
public ByteBuffer sweep(byte value)
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
public abstract ByteBuffer flip()
- See Also:
Buffer.flip()
-
rewind
public abstract ByteBuffer rewind()
- See Also:
Buffer.rewind()
-
remaining
public int remaining()
- See Also:
Buffer.remaining()
-
hasRemaining
public boolean hasRemaining()
- See Also:
Buffer.hasRemaining()
-
duplicate
public abstract ByteBuffer duplicate()
- See Also:
ByteBuffer.duplicate()
-
slice
public abstract ByteBuffer slice()
- See Also:
ByteBuffer.slice()
-
asReadOnlyBuffer
public abstract ByteBuffer asReadOnlyBuffer()
- See Also:
ByteBuffer.asReadOnlyBuffer()
-
array
public abstract byte[] array()
- See Also:
ByteBuffer.array()
-
arrayOffset
public abstract int arrayOffset()
- See Also:
ByteBuffer.arrayOffset()
-
get
public abstract byte get()
- See Also:
ByteBuffer.get()
-
getUnsigned
public short getUnsigned()
Reads one unsigned byte as a short integer.
-
put
public abstract ByteBuffer put(byte b)
- See Also:
ByteBuffer.put(byte)
-
get
public abstract byte get(int index)
- See Also:
ByteBuffer.get(int)
-
getUnsigned
public short getUnsigned(int index)
Reads one byte as an unsigned short integer.
-
put
public abstract ByteBuffer put(int index, byte b)
- See Also:
ByteBuffer.put(int, byte)
-
get
public abstract ByteBuffer get(byte[] dst, int offset, int length)
- See Also:
ByteBuffer.get(byte[], int, int)
-
get
public ByteBuffer get(byte[] dst)
- See Also:
ByteBuffer.get(byte[])
-
put
public abstract ByteBuffer put(java.nio.ByteBuffer src)
Writes the content of the specified src into this buffer.
-
put
public ByteBuffer put(ByteBuffer src)
Writes the content of the specified src into this buffer.
-
put
public abstract ByteBuffer put(byte[] src, int offset, int length)
- See Also:
ByteBuffer.put(byte[], int, int)
-
put
public ByteBuffer put(byte[] src)
- See Also:
ByteBuffer.put(byte[])
-
compact
public abstract ByteBuffer compact()
- See Also:
ByteBuffer.compact()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
compareTo
public int compareTo(ByteBuffer that)
- Specified by:
compareToin interfacejava.lang.Comparable<ByteBuffer>
-
order
public abstract java.nio.ByteOrder order()
- See Also:
ByteBuffer.order()
-
order
public abstract ByteBuffer order(java.nio.ByteOrder bo)
- See Also:
ByteBuffer.order(ByteOrder)
-
getChar
public abstract char getChar()
- See Also:
ByteBuffer.getChar()
-
putChar
public abstract ByteBuffer putChar(char value)
- See Also:
ByteBuffer.putChar(char)
-
getChar
public abstract char getChar(int index)
- See Also:
ByteBuffer.getChar(int)
-
putChar
public abstract ByteBuffer putChar(int index, char value)
- See Also:
ByteBuffer.putChar(int, char)
-
asCharBuffer
public abstract java.nio.CharBuffer asCharBuffer()
- See Also:
ByteBuffer.asCharBuffer()
-
getShort
public abstract short getShort()
- See Also:
ByteBuffer.getShort()
-
getUnsignedShort
public int getUnsignedShort()
Reads two bytes unsigned integer.
-
putShort
public abstract ByteBuffer putShort(short value)
- See Also:
ByteBuffer.putShort(short)
-
getShort
public abstract short getShort(int index)
- See Also:
ByteBuffer.getShort()
-
getUnsignedShort
public int getUnsignedShort(int index)
Reads two bytes unsigned integer.
-
putShort
public abstract ByteBuffer putShort(int index, short value)
- See Also:
ByteBuffer.putShort(int, short)
-
asShortBuffer
public abstract java.nio.ShortBuffer asShortBuffer()
- See Also:
ByteBuffer.asShortBuffer()
-
getInt
public abstract int getInt()
- See Also:
ByteBuffer.getInt()
-
getUnsignedInt
public long getUnsignedInt()
Reads four bytes unsigned integer.
-
putInt
public abstract ByteBuffer putInt(int value)
- See Also:
ByteBuffer.putInt(int)
-
getInt
public abstract int getInt(int index)
- See Also:
ByteBuffer.getInt(int)
-
getUnsignedInt
public long getUnsignedInt(int index)
Reads four bytes unsigned integer.
-
putInt
public abstract ByteBuffer putInt(int index, int value)
- See Also:
ByteBuffer.putInt(int, int)
-
asIntBuffer
public abstract java.nio.IntBuffer asIntBuffer()
- See Also:
ByteBuffer.asIntBuffer()
-
getLong
public abstract long getLong()
- See Also:
ByteBuffer.getLong()
-
putLong
public abstract ByteBuffer putLong(long value)
- See Also:
ByteBuffer.putLong(int, long)
-
getLong
public abstract long getLong(int index)
- See Also:
ByteBuffer.getLong(int)
-
putLong
public abstract ByteBuffer putLong(int index, long value)
- See Also:
ByteBuffer.putLong(int, long)
-
asLongBuffer
public abstract java.nio.LongBuffer asLongBuffer()
- See Also:
ByteBuffer.asLongBuffer()
-
getFloat
public abstract float getFloat()
- See Also:
ByteBuffer.getFloat()
-
putFloat
public abstract ByteBuffer putFloat(float value)
- See Also:
ByteBuffer.putFloat(float)
-
getFloat
public abstract float getFloat(int index)
- See Also:
ByteBuffer.getFloat(int)
-
putFloat
public abstract ByteBuffer putFloat(int index, float value)
- See Also:
ByteBuffer.putFloat(int, float)
-
asFloatBuffer
public abstract java.nio.FloatBuffer asFloatBuffer()
- See Also:
ByteBuffer.asFloatBuffer()
-
getDouble
public abstract double getDouble()
- See Also:
ByteBuffer.getDouble()
-
putDouble
public abstract ByteBuffer putDouble(double value)
- See Also:
ByteBuffer.putDouble(double)
-
getDouble
public abstract double getDouble(int index)
- See Also:
ByteBuffer.getDouble(int)
-
putDouble
public abstract ByteBuffer putDouble(int index, double value)
- See Also:
ByteBuffer.putDouble(int, double)
-
asDoubleBuffer
public abstract java.nio.DoubleBuffer asDoubleBuffer()
- See Also:
ByteBuffer.asDoubleBuffer()
-
asInputStream
public java.io.InputStream asInputStream()
Returns anInputStreamthat reads the data from this buffer.InputStream.read()returns -1 if the buffer position reaches to the limit.
-
asOutputStream
public java.io.OutputStream 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
public java.lang.String getHexDump()
Returns hexdump of this buffer.
-
getString
public java.lang.String getString(java.nio.charset.CharsetDecoder decoder) throws java.nio.charset.CharacterCodingExceptionReads 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:
java.nio.charset.CharacterCodingException
-
getString
public java.lang.String getString(int fieldSize, java.nio.charset.CharsetDecoder decoder) throws java.nio.charset.CharacterCodingExceptionReads aNUL-terminated string from this buffer using the specifieddecoderand returns it.- Parameters:
fieldSize- the maximum number of bytes to read- Throws:
java.nio.charset.CharacterCodingException
-
putString
public ByteBuffer putString(java.lang.CharSequence val, java.nio.charset.CharsetEncoder encoder) throws java.nio.charset.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:
java.nio.BufferOverflowException- if the specified string doesn't fitjava.nio.charset.CharacterCodingException
-
putString
public ByteBuffer putString(java.lang.CharSequence val, int fieldSize, java.nio.charset.CharsetEncoder encoder) throws java.nio.charset.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:
java.nio.charset.CharacterCodingException
-
getPrefixedString
public java.lang.String getPrefixedString(java.nio.charset.CharsetDecoder decoder) throws java.nio.charset.CharacterCodingExceptionReads 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:
java.nio.charset.CharacterCodingException
-
getPrefixedString
public java.lang.String getPrefixedString(int prefixLength, java.nio.charset.CharsetDecoder decoder) throws java.nio.charset.CharacterCodingExceptionReads 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:
java.nio.charset.CharacterCodingException
-
putPrefixedString
public ByteBuffer putPrefixedString(java.lang.CharSequence in, java.nio.charset.CharsetEncoder encoder) throws java.nio.charset.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:
java.nio.BufferOverflowException- if the specified string doesn't fitjava.nio.charset.CharacterCodingException
-
putPrefixedString
public ByteBuffer putPrefixedString(java.lang.CharSequence in, int prefixLength, java.nio.charset.CharsetEncoder encoder) throws java.nio.charset.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:
java.nio.BufferOverflowException- if the specified string doesn't fitjava.nio.charset.CharacterCodingException
-
putPrefixedString
public ByteBuffer putPrefixedString(java.lang.CharSequence in, int prefixLength, int padding, java.nio.charset.CharsetEncoder encoder) throws java.nio.charset.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:
java.nio.BufferOverflowException- if the specified string doesn't fitjava.nio.charset.CharacterCodingException
-
putPrefixedString
public ByteBuffer putPrefixedString(java.lang.CharSequence val, int prefixLength, int padding, byte padValue, java.nio.charset.CharsetEncoder encoder) throws java.nio.charset.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:
java.nio.BufferOverflowException- if the specified string doesn't fitjava.nio.charset.CharacterCodingException
-
getObject
public java.lang.Object getObject() throws java.lang.ClassNotFoundExceptionReads a Java object from the buffer using the contextClassLoaderof the current thread.- Throws:
java.lang.ClassNotFoundException
-
getObject
public java.lang.Object getObject(java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundExceptionReads a Java object from the buffer using the specified classLoader.- Throws:
java.lang.ClassNotFoundException
-
putObject
public ByteBuffer putObject(java.lang.Object o)
Writes the specified Java object to the buffer.
-
prefixedDataAvailable
public 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:
java.lang.IllegalArgumentException- if prefixLength is wrongBufferDataException- if data length is negative
-
prefixedDataAvailable
public 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:
java.lang.IllegalArgumentException- if prefixLength is wrongBufferDataException- if data length is negative or greater then maxDataLength
-
skip
public ByteBuffer skip(int size)
Forwards the position of this buffer as the specifiedsizebytes.
-
fill
public ByteBuffer fill(byte value, int size)
Fills this buffer with the specified value. This method moves buffer position forward.
-
fillAndReset
public ByteBuffer fillAndReset(byte value, int size)
Fills this buffer with the specified value. This method does not change buffer position.
-
fill
public ByteBuffer fill(int size)
Fills this buffer withNUL (0x00). This method moves buffer position forward.
-
fillAndReset
public ByteBuffer fillAndReset(int size)
Fills this buffer withNUL (0x00). This method does not change buffer position.
-
autoExpand
protected ByteBuffer autoExpand(int expectedRemaining)
This method forwards the call toexpand(int)only when autoExpand property is true.
-
autoExpand
protected ByteBuffer autoExpand(int pos, int expectedRemaining)
This method forwards the call toexpand(int)only when autoExpand property is true.
-
-