Class MessageBufferPacker

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

    public class MessageBufferPacker
    extends MessagePacker
    MessagePacker that is useful to produce byte array output.

    This class allocates a new buffer instead of resizing the buffer when data doesn't fit in the initial capacity. This is faster than ByteArrayOutputStream especially when size of written bytes is large because resizing a buffer usually needs to copy contents of the buffer.

    • Method Detail

      • reset

        public MessageBufferOutput reset​(MessageBufferOutput out)
                                  throws java.io.IOException
        Description copied from class: MessagePacker
        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.

        Overrides:
        reset in class MessagePacker
        Parameters:
        out - new output
        Returns:
        the old output
        Throws:
        java.io.IOException - when underlying output throws IOException
      • toByteArray

        public byte[] toByteArray()
        Gets copy of the written data as a byte array.

        If your application needs better performance and smaller memory consumption, you may prefer toMessageBuffer() or toBufferList() to avoid copying.

        Returns:
        the byte array
      • toMessageBuffer

        public MessageBuffer toMessageBuffer()
        Gets the written data as a MessageBuffer.

        Unlike toByteArray(), this method omits copy of the contents if size of the written data is smaller than a single buffer capacity.

        Returns:
        the MessageBuffer instance
      • toBufferList

        public java.util.List<MessageBuffer> toBufferList()
        Returns the written data as a list of MessageBuffer.

        Unlike toByteArray() or toMessageBuffer(), this is the fastest method that doesn't copy contents in any cases.

        Returns:
        the list of MessageBuffer instances
      • getBufferSize

        public int getBufferSize()
        Returns:
        the size of the buffer in use