-
public class MessageBufferMessageBuffer class is an abstraction of memory with fast methods to serialize and deserialize primitive values to/from the memory. All MessageBuffer implementations ensure short/int/float/long/double values are written in big-endian order.
Applications can allocate a new buffer using allocate method, or wrap an byte array or ByteBuffer using wrap methods. wrap method supports both direct buffers and array-backed buffers.
MessageBuffer class itself is optimized for little-endian CPU archtectures so that JVM (HotSpot) can take advantage of the fastest JIT format which skips TypeProfile checking. To ensure this performance, applications must not import unnecessary classes such as MessagePackBE. On big-endian CPU archtectures, it automatically uses a subclass that includes TypeProfile overhead but still faster than stndard ByteBuffer class. On JVMs older than Java 7 and JVMs without Unsafe API (such as Android), implementation falls back to an universal implementation that uses ByteBuffer internally.
-
-
Method Summary
Modifier and Type Method Description static MessageBufferallocate(int size)Allocates a new MessageBuffer backed by a byte array. static MessageBufferwrap(Array<byte> array)Wraps a byte array into a MessageBuffer. static MessageBufferwrap(Array<byte> array, int offset, int length)Wraps a byte array into a MessageBuffer. static MessageBufferwrap(ByteBuffer bb)Wraps a ByteBuffer into a MessageBuffer. static voidreleaseBuffer(MessageBuffer buffer)intsize()Gets the size of the buffer. MessageBufferslice(int offset, int length)bytegetByte(int index)booleangetBoolean(int index)shortgetShort(int index)intgetInt(int index)Read a big-endian int value at the specified index floatgetFloat(int index)longgetLong(int index)doublegetDouble(int index)voidgetBytes(int index, Array<byte> dst, int dstOffset, int length)voidgetBytes(int index, int len, ByteBuffer dst)voidputByte(int index, byte v)voidputBoolean(int index, boolean v)voidputShort(int index, short v)voidputInt(int index, int v)Write a big-endian integer value to the memory voidputFloat(int index, float v)voidputLong(int index, long l)voidputDouble(int index, double v)voidputBytes(int index, Array<byte> src, int srcOffset, int length)voidputByteBuffer(int index, ByteBuffer src, int len)voidputMessageBuffer(int index, MessageBuffer src, int srcOffset, int len)ByteBuffersliceAsByteBuffer(int index, int length)Create a ByteBuffer view of the range [index, index+length) of this memory ByteBuffersliceAsByteBuffer()Get a ByteBuffer view of this buffer booleanhasArray()Array<byte>toByteArray()Get a copy of this buffer Array<byte>array()intarrayOffset()voidcopyTo(int index, MessageBuffer dst, int offset, int length)Copy this buffer contents to another MessageBuffer StringtoHexString(int offset, int length)-
-
Method Detail
-
allocate
static MessageBuffer allocate(int size)
Allocates a new MessageBuffer backed by a byte array.
-
wrap
static MessageBuffer wrap(Array<byte> array)
Wraps a byte array into a MessageBuffer.
The new MessageBuffer will be backed by the given byte array. Modifications to the new MessageBuffer will cause the byte array to be modified and vice versa.
The new buffer's size will be array.length. hasArray() will return true.
- Parameters:
array- the byte array that will gack this MessageBuffer
-
wrap
static MessageBuffer wrap(Array<byte> array, int offset, int length)
Wraps a byte array into a MessageBuffer.
The new MessageBuffer will be backed by the given byte array. Modifications to the new MessageBuffer will cause the byte array to be modified and vice versa.
The new buffer's size will be length. hasArray() will return true.
- Parameters:
array- the byte array that will gack this MessageBufferoffset- The offset of the subarray to be used; must be non-negative and no larger than array.length- The length of the subarray to be used; must be non-negative and no larger than array.
-
wrap
static MessageBuffer wrap(ByteBuffer bb)
Wraps a ByteBuffer into a MessageBuffer.
The new MessageBuffer will be backed by the given byte buffer. Modifications to the new MessageBuffer will cause the byte buffer to be modified and vice versa. However, change of position, limit, or mark of given byte buffer doesn't affect MessageBuffer.
The new buffer's size will be bb.remaining(). hasArray() will return the same result with bb.hasArray().
- Parameters:
bb- the byte buffer that will gack this MessageBuffer
-
releaseBuffer
static void releaseBuffer(MessageBuffer buffer)
-
size
int size()
Gets the size of the buffer.
MessageBuffer doesn't have limit unlike ByteBuffer. Instead, you can use slice to get apart of the buffer.
-
slice
MessageBuffer slice(int offset, int length)
-
getByte
byte getByte(int index)
-
getBoolean
boolean getBoolean(int index)
-
getShort
short getShort(int index)
-
getInt
int getInt(int index)
Read a big-endian int value at the specified index
-
getFloat
float getFloat(int index)
-
getLong
long getLong(int index)
-
getDouble
double getDouble(int index)
-
getBytes
void getBytes(int index, int len, ByteBuffer dst)
-
putByte
void putByte(int index, byte v)
-
putBoolean
void putBoolean(int index, boolean v)
-
putShort
void putShort(int index, short v)
-
putInt
void putInt(int index, int v)
Write a big-endian integer value to the memory
-
putFloat
void putFloat(int index, float v)
-
putLong
void putLong(int index, long l)
-
putDouble
void putDouble(int index, double v)
-
putByteBuffer
void putByteBuffer(int index, ByteBuffer src, int len)
-
putMessageBuffer
void putMessageBuffer(int index, MessageBuffer src, int srcOffset, int len)
-
sliceAsByteBuffer
ByteBuffer sliceAsByteBuffer(int index, int length)
Create a ByteBuffer view of the range [index, index+length) of this memory
-
sliceAsByteBuffer
ByteBuffer sliceAsByteBuffer()
Get a ByteBuffer view of this buffer
-
hasArray
boolean hasArray()
-
toByteArray
Array<byte> toByteArray()
Get a copy of this buffer
-
arrayOffset
int arrayOffset()
-
copyTo
void copyTo(int index, MessageBuffer dst, int offset, int length)
Copy this buffer contents to another MessageBuffer
-
toHexString
String toHexString(int offset, int length)
-
-
-
-