Class DirectBufferStreamBroker


  • public class DirectBufferStreamBroker
    extends java.lang.Object
    A helper class to consume the entire contents of a stream into a direct byte buffer. Designed for cases where this is repeated several times, as it supports resetting.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clear the underlying buffer.
      boolean consumeNext​(java.io.InputStream inputStream, int maximumSavedBytes, int maximumReadBytes)
      Consume an entire stream and append it into the buffer (or clear first if clear parameter is true).
      byte[] extractBytes()
      Copies the final state after a consumeNext(InputStream, int, int) operation into a new byte array.
      java.nio.ByteBuffer getBuffer()  
      boolean isTruncated()  
      void resetAndCompact()
      Reset the buffer to its initial size.
      • Methods inherited from class java.lang.Object

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

      • DirectBufferStreamBroker

        public DirectBufferStreamBroker​(int initialSize)
        Parameters:
        initialSize - Initial size of the underlying direct buffer.
    • Method Detail

      • resetAndCompact

        public void resetAndCompact()
        Reset the buffer to its initial size.
      • clear

        public void clear()
        Clear the underlying buffer.
      • getBuffer

        public java.nio.ByteBuffer getBuffer()
        Returns:
        A duplicate of the underlying buffer.
      • isTruncated

        public boolean isTruncated()
      • extractBytes

        public byte[] extractBytes()
        Copies the final state after a consumeNext(InputStream, int, int) operation into a new byte array.
        Returns:
        New byte array containing consumed data.
      • consumeNext

        public boolean consumeNext​(java.io.InputStream inputStream,
                                   int maximumSavedBytes,
                                   int maximumReadBytes)
                            throws java.io.IOException
        Consume an entire stream and append it into the buffer (or clear first if clear parameter is true).
        Parameters:
        inputStream - The input stream to fully consume.
        maximumSavedBytes - Maximum number of bytes to save internally. If this is exceeded, it will continue reading and discarding until maximum read byte count is reached.
        maximumReadBytes - Maximum number of bytes to read.
        Returns:
        If stream was fully read before maximumReadBytes was reached, returns true. Returns false if the number of bytes read is maximumReadBytes, even if no more data is left in the stream.
        Throws:
        java.io.IOException - On read error