Class ByteBufferAccumulator

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ByteBufferAccumulator
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Accumulates data into a list of ByteBuffers which can then be combined into a single buffer or written to an OutputStream. The buffer list automatically grows as data is written to it, the buffers are taken from the supplied ByteBufferPool or freshly allocated if one is not supplied. The method ensureBuffer(int, int) is used to write directly to the last buffer stored in the buffer list, if there is less than a certain amount of space available in that buffer then a new one will be allocated and returned instead.
    See Also:
    ensureBuffer(int, int)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void copyBuffer​(java.nio.ByteBuffer buffer)  
      void copyBytes​(byte[] buf, int offset, int length)  
      java.nio.ByteBuffer ensureBuffer​(int minAllocationSize)
      Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.
      java.nio.ByteBuffer ensureBuffer​(int minSize, int minAllocationSize)
      Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.
      ByteBufferPool getByteBufferPool()  
      int getLength()
      Get the amount of bytes which have been accumulated.
      java.nio.ByteBuffer takeByteBuffer()
      Take the combined buffer containing all content written to the accumulator.
      byte[] toByteArray()  
      java.nio.ByteBuffer toByteBuffer()
      Take the combined buffer containing all content written to the accumulator.
      void writeTo​(java.io.OutputStream out)  
      void writeTo​(java.nio.ByteBuffer buffer)  
      • Methods inherited from class java.lang.Object

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

      • ByteBufferAccumulator

        public ByteBufferAccumulator()
      • ByteBufferAccumulator

        public ByteBufferAccumulator​(ByteBufferPool bufferPool,
                                     boolean direct)
    • Method Detail

      • getLength

        public int getLength()
        Get the amount of bytes which have been accumulated. This will add up the remaining of each buffer in the accumulator.
        Returns:
        the total length of the content in the accumulator.
      • ensureBuffer

        public java.nio.ByteBuffer ensureBuffer​(int minAllocationSize)
        Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.
        Parameters:
        minAllocationSize - new buffers will be allocated to have at least this size.
        Returns:
        a buffer with at least minSize space to write into.
      • ensureBuffer

        public java.nio.ByteBuffer ensureBuffer​(int minSize,
                                                int minAllocationSize)
        Get the last buffer of the accumulator, this can be written to directly to avoid copying into the accumulator.
        Parameters:
        minSize - the smallest amount of remaining space before a new buffer is allocated.
        minAllocationSize - new buffers will be allocated to have at least this size.
        Returns:
        a buffer with at least minSize space to write into.
      • copyBytes

        public void copyBytes​(byte[] buf,
                              int offset,
                              int length)
      • copyBuffer

        public void copyBuffer​(java.nio.ByteBuffer buffer)
      • takeByteBuffer

        public java.nio.ByteBuffer takeByteBuffer()
        Take the combined buffer containing all content written to the accumulator. The caller is responsible for releasing this ByteBuffer back into the ByteBufferPool.
        Returns:
        a buffer containing all content written to the accumulator.
        See Also:
        toByteBuffer()
      • toByteBuffer

        public java.nio.ByteBuffer toByteBuffer()
        Take the combined buffer containing all content written to the accumulator. The returned buffer is still contained within the accumulator and will be released back to the ByteBufferPool when the accumulator is closed.
        Returns:
        a buffer containing all content written to the accumulator.
        See Also:
        takeByteBuffer(), close()
      • toByteArray

        public byte[] toByteArray()
        Returns:
        a newly allocated byte array containing all content written into the accumulator.
      • writeTo

        public void writeTo​(java.nio.ByteBuffer buffer)
      • writeTo

        public void writeTo​(java.io.OutputStream out)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable