Package 

Class MessageBuffer


  • 
    public class MessageBuffer
    
                        

    MessageBuffer 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 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 MessageBuffer
        offset - 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
      • 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.

      • getInt

         int getInt(int index)

        Read a big-endian int value at the specified index

      • getBytes

         void getBytes(int index, Array<byte> dst, int dstOffset, int length)
      • 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)
      • putBytes

         void putBytes(int index, Array<byte> src, int srcOffset, int length)
      • sliceAsByteBuffer

         ByteBuffer sliceAsByteBuffer(int index, int length)

        Create a ByteBuffer view of the range [index, index+length) of this memory

      • copyTo

         void copyTo(int index, MessageBuffer dst, int offset, int length)

        Copy this buffer contents to another MessageBuffer