public class MessageBuffer
extends java.lang.Object
Applications can allocate a new buffer using allocate(int) method, or wrap an byte array or ByteBuffer
using wrap(byte[], int, int) methods. wrap(ByteBuffer) 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.
| Modifier and Type | Field and Description |
|---|---|
protected long |
address
Head address of the underlying memory.
|
protected java.lang.Object |
base
Base object for resolving the relative address of the raw byte array.
|
protected java.nio.ByteBuffer |
reference
Reference is used to hold a reference to an object that holds the underlying memory so that it cannot be
released by the garbage collector.
|
protected int |
size
Size of the underlying memory
|
| Modifier | Constructor and Description |
|---|---|
protected |
MessageBuffer(java.lang.Object base,
long address,
int length) |
| Modifier and Type | Method and Description |
|---|---|
static MessageBuffer |
allocate(int size)
Allocates a new MessageBuffer backed by a byte array.
|
byte[] |
array() |
int |
arrayOffset() |
void |
copyTo(int index,
MessageBuffer dst,
int offset,
int length)
Copy this buffer contents to another MessageBuffer
|
boolean |
getBoolean(int index) |
byte |
getByte(int index) |
void |
getBytes(int index,
byte[] dst,
int dstOffset,
int length) |
void |
getBytes(int index,
int len,
java.nio.ByteBuffer dst) |
double |
getDouble(int index) |
float |
getFloat(int index) |
int |
getInt(int index)
Read a big-endian int value at the specified index
|
long |
getLong(int index) |
short |
getShort(int index) |
boolean |
hasArray() |
void |
putBoolean(int index,
boolean v) |
void |
putByte(int index,
byte v) |
void |
putByteBuffer(int index,
java.nio.ByteBuffer src,
int len) |
void |
putBytes(int index,
byte[] src,
int srcOffset,
int length) |
void |
putDouble(int index,
double v) |
void |
putFloat(int index,
float v) |
void |
putInt(int index,
int v)
Write a big-endian integer value to the memory
|
void |
putLong(int index,
long l) |
void |
putMessageBuffer(int index,
MessageBuffer src,
int srcOffset,
int len) |
void |
putShort(int index,
short v) |
static void |
releaseBuffer(MessageBuffer buffer) |
int |
size()
Gets the size of the buffer.
|
MessageBuffer |
slice(int offset,
int length) |
java.nio.ByteBuffer |
sliceAsByteBuffer()
Get a ByteBuffer view of this buffer
|
java.nio.ByteBuffer |
sliceAsByteBuffer(int index,
int length)
Create a ByteBuffer view of the range [index, index+length) of this memory
|
byte[] |
toByteArray()
Get a copy of this buffer
|
java.lang.String |
toHexString(int offset,
int length) |
static MessageBuffer |
wrap(byte[] array)
Wraps a byte array into a MessageBuffer.
|
static MessageBuffer |
wrap(byte[] array,
int offset,
int length)
Wraps a byte array into a MessageBuffer.
|
static MessageBuffer |
wrap(java.nio.ByteBuffer bb)
Wraps a ByteBuffer into a MessageBuffer.
|
protected final java.lang.Object base
protected final long address
protected final int size
protected final java.nio.ByteBuffer reference
protected MessageBuffer(java.lang.Object base,
long address,
int length)
public static MessageBuffer allocate(int size)
java.lang.IllegalArgumentException - If the capacity is a negative integerpublic static MessageBuffer wrap(byte[] array)
array - the byte array that will gack this MessageBufferpublic static MessageBuffer wrap(byte[] array, int offset, int length)
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.lengthlength - The length of the subarray to be used; must be non-negative and no larger than array.length - offsetpublic static MessageBuffer wrap(java.nio.ByteBuffer bb)
bb - the byte buffer that will gack this MessageBufferjava.lang.IllegalArgumentException - given byte buffer returns false both from hasArray() and isDirect()java.lang.UnsupportedOperationException - given byte buffer is a direct buffer and this platform doesn't support Unsafe APIpublic static void releaseBuffer(MessageBuffer buffer)
public int size()
slice(int, int) to get a
part of the buffer.public MessageBuffer slice(int offset, int length)
public byte getByte(int index)
public boolean getBoolean(int index)
public short getShort(int index)
public int getInt(int index)
index - public float getFloat(int index)
public long getLong(int index)
public double getDouble(int index)
public void getBytes(int index,
byte[] dst,
int dstOffset,
int length)
public void getBytes(int index,
int len,
java.nio.ByteBuffer dst)
public void putByte(int index,
byte v)
public void putBoolean(int index,
boolean v)
public void putShort(int index,
short v)
public void putInt(int index,
int v)
index - v - public void putFloat(int index,
float v)
public void putLong(int index,
long l)
public void putDouble(int index,
double v)
public void putBytes(int index,
byte[] src,
int srcOffset,
int length)
public void putByteBuffer(int index,
java.nio.ByteBuffer src,
int len)
public void putMessageBuffer(int index,
MessageBuffer src,
int srcOffset,
int len)
public java.nio.ByteBuffer sliceAsByteBuffer(int index,
int length)
index - length - public java.nio.ByteBuffer sliceAsByteBuffer()
public boolean hasArray()
public byte[] toByteArray()
public byte[] array()
public int arrayOffset()
public void copyTo(int index,
MessageBuffer dst,
int offset,
int length)
index - dst - offset - length - public java.lang.String toHexString(int offset,
int length)