Package 

Class MessagePacker

  • All Implemented Interfaces:
    java.io.Closeable , java.io.Flushable , java.lang.AutoCloseable

    
    public class MessagePacker
     implements Closeable, Flushable
                        

    MessagePack serializer that converts objects into binary. You can use factory methods of MessagePack class or MessagePack.PackerConfig class to create an instance.

    This class provides following primitive methods to write MessagePack values. These primitive methods write short bytes (1 to 7 bytes) to the internal buffer at once. There are also some utility methods for convenience.

    Primitive methods:

    Utility methods:

    To write a byte array, first you call packBinaryHeader method with length of the byte array. Then, you call writePayload or addPayload method to write the contents.

    To write a List, Collection or array, first you call packArrayHeader method with the number of elements. Then, you call packer methods for each element. iteration.

    To write a Map, first you call packMapHeader method with size of the map. Then, for each pair, you call packer methods for key first, and then value. You will call packer methods twice as many time as the size of the map.

    Note that packXxxHeader methods don't validate number of elements. You must call packer methods for correct number of times to produce valid MessagePack data.

    When IOException is thrown, primitive methods guarantee that all data is written to the internal buffer or no data is written. This is convenient behavior when you use a non-blocking output channel that may not be writable immediately.

    • Method Detail

      • reset

         MessageBufferOutput reset(MessageBufferOutput out)

        Replaces underlying output.

        This method flushes current internal buffer to the output, swaps it with the new given output, then returnsthe old output.

        This method doesn't close the old output.

        Parameters:
        out - new output
      • getTotalWrittenBytes

         long getTotalWrittenBytes()

        Returns total number of written bytes.

        This method returns total of amount of data flushed to the underlying output plus size of currentinternal buffer.

        Calling reset resets this number to 0.

      • clear

         void clear()

        Clears the written data.

      • flush

         void flush()

        Flushes internal buffer to the underlying output.

        This method also calls flush method of the underlying output after writing internal buffer.

      • close

         void close()

        Closes underlying output.

        This method flushes internal buffer before closing.

      • packBoolean

         MessagePacker packBoolean(boolean b)

        Writes a Boolean value.

        This method writes a true byte or a false byte.

      • packByte

         MessagePacker packByte(byte b)

        Writes an Integer value.

        This method writes an integer using the smallest format from the int format family.

        Parameters:
        b - the integer to be written
      • packShort

         MessagePacker packShort(short v)

        Writes an Integer value.

        This method writes an integer using the smallest format from the int format family.

        Parameters:
        v - the integer to be written
      • packInt

         MessagePacker packInt(int r)

        Writes an Integer value.

        This method writes an integer using the smallest format from the int format family.

        Parameters:
        r - the integer to be written
      • packLong

         MessagePacker packLong(long v)

        Writes an Integer value.

        This method writes an integer using the smallest format from the int format family.

        Parameters:
        v - the integer to be written
      • packBigInteger

         MessagePacker packBigInteger(BigInteger bi)

        Writes an Integer value.

        This method writes an integer using the smallest format from the int format family.

        Parameters:
        bi - the integer to be written
      • packFloat

         MessagePacker packFloat(float v)

        Writes a Float value.

        This method writes a float value using float format family.

        Parameters:
        v - the value to be written
      • packDouble

         MessagePacker packDouble(double v)

        Writes a Float value.

        This method writes a float value using float format family.

        Parameters:
        v - the value to be written
      • packString

         MessagePacker packString(String s)

        Writes a String vlaue in UTF-8 encoding.

        This method writes a UTF-8 string using the smallest format from the str format family by default. If withStr8FormatSupport is set to false, smallest format from the str format family excepting str8 format.

        Parameters:
        s - the string to be written
      • packArrayHeader

         MessagePacker packArrayHeader(int arraySize)

        Writes header of an Array value.

        You will call other packer methods for each element after this method call.

        You don't have to call anything at the end of iteration.

        Parameters:
        arraySize - number of elements to be written
      • packMapHeader

         MessagePacker packMapHeader(int mapSize)

        Writes header of a Map value.

        After this method call, for each key-value pair, you will call packer methods for key first, and then value.You will call packer methods twice as many time as the size of the map.

        You don't have to call anything at the end of iteration.

        Parameters:
        mapSize - number of pairs to be written
      • packExtensionTypeHeader

         MessagePacker packExtensionTypeHeader(byte extType, int payloadLen)

        Writes header of an Extension value.

        You MUST call writePayload or addPayload method to write body binary.

        Parameters:
        extType - the extension type tag to be written
        payloadLen - number of bytes of a payload binary to be written
      • packRawStringHeader

         MessagePacker packRawStringHeader(int len)

        Writes header of a String value.

        Length must be number of bytes of a string in UTF-8 encoding.

        You MUST call writePayload or addPayload method to write body of theUTF-8 encoded string.

        Parameters:
        len - number of bytes of a UTF-8 string to be written