Class ArrayBufferOutput

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

    public class ArrayBufferOutput
    extends java.lang.Object
    implements MessageBufferOutput
    MessageBufferOutput adapter that writes data into a list of byte arrays.

    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 Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(byte[] buffer, int offset, int length)
      Writes an external payload data.
      void clear()
      Clears the written data.
      void close()  
      void flush()  
      int getSize()
      Gets the size of the written data.
      MessageBuffer next​(int minimumSize)
      Allocates the next buffer to write.
      java.util.List<MessageBuffer> toBufferList()
      Returns the written data as a list of MessageBuffer.
      byte[] toByteArray()
      Gets a copy of the written data as a byte array.
      MessageBuffer toMessageBuffer()
      Gets the written data as a MessageBuffer.
      void write​(byte[] buffer, int offset, int length)
      Writes an external payload data.
      void writeBuffer​(int length)
      Writes the previously allocated buffer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ArrayBufferOutput

        public ArrayBufferOutput()
      • ArrayBufferOutput

        public ArrayBufferOutput​(int bufferSize)
    • Method Detail

      • getSize

        public int getSize()
        Gets the size of the written data.
        Returns:
        number of bytes
      • toByteArray

        public byte[] toByteArray()
        Gets a 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
      • clear

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

        public MessageBuffer next​(int minimumSize)
        Description copied from interface: MessageBufferOutput
        Allocates the next buffer to write.

        This method should return a MessageBuffer instance that has specified size of capacity at least.

        When this method is called twice, the previously returned buffer is no longer used. This method may be called twice without call of MessageBufferOutput.writeBuffer(int) in between. In this case, the buffer should be discarded without flushing it to the output.

        Specified by:
        next in interface MessageBufferOutput
        Parameters:
        minimumSize - the mimium required buffer size to allocate
        Returns:
        the MessageBuffer instance with at least minimumSize bytes of capacity
      • write

        public void write​(byte[] buffer,
                          int offset,
                          int length)
        Description copied from interface: MessageBufferOutput
        Writes an external payload data. This method should follow semantics of OutputStream.
        Specified by:
        write in interface MessageBufferOutput
        Parameters:
        buffer - the data to write
        offset - the start offset in the data
        length - the number of bytes to write
      • add

        public void add​(byte[] buffer,
                        int offset,
                        int length)
        Description copied from interface: MessageBufferOutput
        Writes an external payload data.

        Unlike MessageBufferOutput.write(byte[], int, int) method, the buffer is given - this MessageBufferOutput implementation gets ownership of the buffer and may modify contents of the buffer. Contents of this buffer won't be modified by the caller.

        Specified by:
        add in interface MessageBufferOutput
        Parameters:
        buffer - the data to add
        offset - the start offset in the data
        length - the number of bytes to add
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • flush

        public void flush()
        Specified by:
        flush in interface java.io.Flushable