Class MessageBuffer
- java.lang.Object
-
- com.batch.android.msgpack.core.buffer.MessageBuffer
-
- Direct Known Subclasses:
MessageBufferBE,MessageBufferU
public class MessageBuffer extends java.lang.ObjectMessageBuffer 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(int)method, or wrap an byte array or ByteBuffer usingwrap(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.
-
-
Field Summary
Fields Modifier and Type Field Description protected longaddressHead address of the underlying memory.protected java.lang.ObjectbaseBase object for resolving the relative address of the raw byte array.protected java.nio.ByteBufferreferenceReference 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 intsizeSize of the underlying memory
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessageBuffer(java.lang.Object base, long address, int length)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MessageBufferallocate(int size)Allocates a new MessageBuffer backed by a byte array.byte[]array()intarrayOffset()voidcopyTo(int index, MessageBuffer dst, int offset, int length)Copy this buffer contents to another MessageBufferbooleangetBoolean(int index)bytegetByte(int index)voidgetBytes(int index, byte[] dst, int dstOffset, int length)voidgetBytes(int index, int len, java.nio.ByteBuffer dst)doublegetDouble(int index)floatgetFloat(int index)intgetInt(int index)Read a big-endian int value at the specified indexlonggetLong(int index)shortgetShort(int index)booleanhasArray()voidputBoolean(int index, boolean v)voidputByte(int index, byte v)voidputByteBuffer(int index, java.nio.ByteBuffer src, int len)voidputBytes(int index, byte[] src, int srcOffset, int length)voidputDouble(int index, double v)voidputFloat(int index, float v)voidputInt(int index, int v)Write a big-endian integer value to the memoryvoidputLong(int index, long l)voidputMessageBuffer(int index, MessageBuffer src, int srcOffset, int len)voidputShort(int index, short v)static voidreleaseBuffer(MessageBuffer buffer)intsize()Gets the size of the buffer.MessageBufferslice(int offset, int length)java.nio.ByteBuffersliceAsByteBuffer()Get a ByteBuffer view of this bufferjava.nio.ByteBuffersliceAsByteBuffer(int index, int length)Create a ByteBuffer view of the range [index, index+length) of this memorybyte[]toByteArray()Get a copy of this bufferjava.lang.StringtoHexString(int offset, int length)static MessageBufferwrap(byte[] array)Wraps a byte array into a MessageBuffer.static MessageBufferwrap(byte[] array, int offset, int length)Wraps a byte array into a MessageBuffer.static MessageBufferwrap(java.nio.ByteBuffer bb)Wraps a ByteBuffer into a MessageBuffer.
-
-
-
Field Detail
-
base
protected final java.lang.Object base
Base object for resolving the relative address of the raw byte array. If base == null, the address value is a raw memory address
-
address
protected final long address
Head address of the underlying memory. If base is null, the address is a direct memory address, and if not, it is the relative address within an array object (base)
-
size
protected final int size
Size of the underlying memory
-
reference
protected final 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.
-
-
Method Detail
-
allocate
public static MessageBuffer allocate(int size)
Allocates a new MessageBuffer backed by a byte array.- Throws:
java.lang.IllegalArgumentException- If the capacity is a negative integer
-
wrap
public static MessageBuffer wrap(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- Returns:
- a new MessageBuffer that wraps the given byte array
-
wrap
public static MessageBuffer wrap(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.lengthlength- The length of the subarray to be used; must be non-negative and no larger than array.length - offset- Returns:
- a new MessageBuffer that wraps the given byte array
-
wrap
public static MessageBuffer wrap(java.nio.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- Returns:
- a new MessageBuffer that wraps the given byte array
- Throws:
java.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 API
-
releaseBuffer
public static void releaseBuffer(MessageBuffer buffer)
-
size
public int size()
Gets the size of the buffer.MessageBuffer doesn't have limit unlike ByteBuffer. Instead, you can use
slice(int, int)to get a part of the buffer.- Returns:
- number of bytes
-
slice
public MessageBuffer slice(int offset, int length)
-
getByte
public byte getByte(int index)
-
getBoolean
public boolean getBoolean(int index)
-
getShort
public short getShort(int index)
-
getInt
public int getInt(int index)
Read a big-endian int value at the specified index- Parameters:
index-- Returns:
-
getFloat
public float getFloat(int index)
-
getLong
public long getLong(int index)
-
getDouble
public double getDouble(int index)
-
getBytes
public void getBytes(int index, byte[] dst, int dstOffset, int length)
-
getBytes
public void getBytes(int index, int len, java.nio.ByteBuffer dst)
-
putByte
public void putByte(int index, byte v)
-
putBoolean
public void putBoolean(int index, boolean v)
-
putShort
public void putShort(int index, short v)
-
putInt
public void putInt(int index, int v)Write a big-endian integer value to the memory- Parameters:
index-v-
-
putFloat
public void putFloat(int index, float v)
-
putLong
public void putLong(int index, long l)
-
putDouble
public void putDouble(int index, double v)
-
putBytes
public void putBytes(int index, byte[] src, int srcOffset, int length)
-
putByteBuffer
public void putByteBuffer(int index, java.nio.ByteBuffer src, int len)
-
putMessageBuffer
public void putMessageBuffer(int index, MessageBuffer src, int srcOffset, int len)
-
sliceAsByteBuffer
public java.nio.ByteBuffer sliceAsByteBuffer(int index, int length)Create a ByteBuffer view of the range [index, index+length) of this memory- Parameters:
index-length-- Returns:
-
sliceAsByteBuffer
public java.nio.ByteBuffer sliceAsByteBuffer()
Get a ByteBuffer view of this buffer- Returns:
-
hasArray
public boolean hasArray()
-
toByteArray
public byte[] toByteArray()
Get a copy of this buffer- Returns:
-
array
public byte[] array()
-
arrayOffset
public int arrayOffset()
-
copyTo
public void copyTo(int index, MessageBuffer dst, int offset, int length)Copy this buffer contents to another MessageBuffer- Parameters:
index-dst-offset-length-
-
toHexString
public java.lang.String toHexString(int offset, int length)
-
-