Class MessagePacker

    • Method Detail

      • reset

        public MessageBufferOutput reset​(MessageBufferOutput out)
                                  throws java.io.IOException
        Replaces underlying output.

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

        This method doesn't close the old output.

        Parameters:
        out - new output
        Returns:
        the old output
        Throws:
        java.io.IOException - when underlying output throws IOException
        java.lang.NullPointerException - the given output is null
      • getTotalWrittenBytes

        public long getTotalWrittenBytes()
        Returns total number of written bytes.

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

        Calling reset(MessageBufferOutput) resets this number to 0.

      • clear

        public void clear()
        Clears the written data.
      • flush

        public void flush()
                   throws java.io.IOException
        Flushes internal buffer to the underlying output.

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

        Specified by:
        flush in interface java.io.Flushable
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Closes underlying output.

        This method flushes internal buffer before closing.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • packNil

        public MessagePacker packNil()
                              throws java.io.IOException
        Writes a Nil value. This method writes a nil byte.
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packBoolean

        public MessagePacker packBoolean​(boolean b)
                                  throws java.io.IOException
        Writes a Boolean value. This method writes a true byte or a false byte.
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packByte

        public MessagePacker packByte​(byte b)
                               throws java.io.IOException
        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
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packShort

        public MessagePacker packShort​(short v)
                                throws java.io.IOException
        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
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packInt

        public MessagePacker packInt​(int r)
                              throws java.io.IOException
        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
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packLong

        public MessagePacker packLong​(long v)
                               throws java.io.IOException
        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
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packBigInteger

        public MessagePacker packBigInteger​(java.math.BigInteger bi)
                                     throws java.io.IOException
        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
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packFloat

        public MessagePacker packFloat​(float v)
                                throws java.io.IOException
        Writes a Float value.

        This method writes a float value using float format family.

        Parameters:
        v - the value to be written
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packDouble

        public MessagePacker packDouble​(double v)
                                 throws java.io.IOException
        Writes a Float value.

        This method writes a float value using float format family.

        Parameters:
        v - the value to be written
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packString

        public MessagePacker packString​(java.lang.String s)
                                 throws java.io.IOException
        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 MessagePack.PackerConfig.withStr8FormatSupport(boolean) is set to false, smallest format from the str format family excepting str8 format.

        Parameters:
        s - the string to be written
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packArrayHeader

        public MessagePacker packArrayHeader​(int arraySize)
                                      throws java.io.IOException
        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
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packMapHeader

        public MessagePacker packMapHeader​(int mapSize)
                                    throws java.io.IOException
        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
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packValue

        public MessagePacker packValue​(Value v)
                                throws java.io.IOException
        Writes a dynamically typed value.
        Parameters:
        v - the value to be written
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packExtensionTypeHeader

        public MessagePacker packExtensionTypeHeader​(byte extType,
                                                     int payloadLen)
                                              throws java.io.IOException
        Writes header of an Extension value.

        You MUST call writePayload(byte[]) or addPayload(byte[]) 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
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packBinaryHeader

        public MessagePacker packBinaryHeader​(int len)
                                       throws java.io.IOException
        Writes header of a Binary value.

        You MUST call writePayload(byte[]) or addPayload(byte[]) method to write body binary.

        Parameters:
        len - number of bytes of a binary to be written
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • packRawStringHeader

        public MessagePacker packRawStringHeader​(int len)
                                          throws java.io.IOException
        Writes header of a String value.

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

        You MUST call writePayload(byte[]) or addPayload(byte[]) method to write body of the UTF-8 encoded string.

        Parameters:
        len - number of bytes of a UTF-8 string to be written
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • writePayload

        public MessagePacker writePayload​(byte[] src)
                                   throws java.io.IOException
        Writes a byte array to the output.

        This method is used with packRawStringHeader(int) or packBinaryHeader(int) methods.

        Parameters:
        src - the data to add
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException
      • writePayload

        public MessagePacker writePayload​(byte[] src,
                                          int off,
                                          int len)
                                   throws java.io.IOException
        Writes a byte array to the output.

        This method is used with packRawStringHeader(int) or packBinaryHeader(int) methods.

        Parameters:
        src - the data to add
        off - the start offset in the data
        len - the number of bytes to add
        Returns:
        this
        Throws:
        java.io.IOException - when underlying output throws IOException