Class ByteBufferQueue


  • @NotThreadSafe
    public class ByteBufferQueue
    extends Object
    A helper class to make queuing up of data easier.
    Since:
    3.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBufferQueue​(int bufferSize)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte get()
      Reads the next byte from this queue.
      int get​(byte[] dst, int offset, int len)
      This method transfers bytes from the head of this buffer queue into the given destination byte.
      void get​(ByteBuffer dst)
      This method transfers bytes from the head of this buffer queue into the given destination buffer.
      boolean hasRemaining()
      Tells whether there are any bytes between the current read index and the write index.
      boolean hasRemaining​(int len)
      Tells whether there are any bytes between the current read index and the write index.
      ByteBuffer newByteBuffer()
      Creates a new byte buffer matched to the queue's current buffer size.
      void peek​(ByteBuffer dst)  
      void put​(byte b)
      This method appends a single byte onto this buffer queue.
      void put​(byte[] src, int offset, int len)
      This method appends bytes from the byte array onto this buffer queue.
      void put​(ByteBuffer src)
      This method transfers the bytes remaining in the given source buffer appended onto this buffer queue.
      long remaining()
      Returns how much data is remaining between the current read index and the write index.
      int remaining​(int limit)
      Returns how much data is remaining between the current read index and the write index.
      long skip​(long bytes)
      Discards up to the specified number of bytes from the read index.
      byte[] toByteArray()
      Transfers all the bytes in the ByteBufferQueue from the read position to the write position into a new byte[].
      String toString()
      void unget​(ByteBuffer src)
      This method transfers the bytes remaining in the given source buffer inserted at the head of this buffer queue.
    • Constructor Detail

      • ByteBufferQueue

        public ByteBufferQueue​(int bufferSize)
        Constructor.
        Parameters:
        bufferSize - the size of buffers to use for queuing.
    • Method Detail

      • newByteBuffer

        public ByteBuffer newByteBuffer()
        Creates a new byte buffer matched to the queue's current buffer size.
        Returns:
        a new byte buffer matched to the queue's current buffer size.
      • put

        public void put​(ByteBuffer src)
        This method transfers the bytes remaining in the given source buffer appended onto this buffer queue.
        Parameters:
        src - The source buffer from which bytes are to be read.
      • put

        public void put​(byte[] src,
                        int offset,
                        int len)
        This method appends bytes from the byte array onto this buffer queue.
        Parameters:
        src - the source byte array.
        offset - the offset from which to start taking bytes.
        len - the number of bytes to transfer.
      • put

        public void put​(byte b)
        This method appends a single byte onto this buffer queue.
        Parameters:
        b - the byte.
      • hasRemaining

        public boolean hasRemaining()
        Tells whether there are any bytes between the current read index and the write index.
        Returns:
        true if, and only if, there is at least one byte remaining in this buffer queue.
      • hasRemaining

        public boolean hasRemaining​(int len)
        Tells whether there are any bytes between the current read index and the write index.
        Parameters:
        len - the number of bytes that we want at least remaining
        Returns:
        true if, and only if, there is at least len bytes remaining in this buffer queue.
      • remaining

        public long remaining()
        Returns how much data is remaining between the current read index and the write index.
        Returns:
        the total number of bytes remaining in this buffer queue.
      • remaining

        public int remaining​(int limit)
        Returns how much data is remaining between the current read index and the write index.
        Parameters:
        limit - the maximum number of remaining bytes at which to short-circuit.
        Returns:
        the total number of bytes remaining in this buffer queue or the supplied maximum if there is at least the supplied limit remaining.
      • skip

        public long skip​(long bytes)
        Discards up to the specified number of bytes from the read index.
        Parameters:
        bytes - the total number of bytes to discard.
        Returns:
        the number of bytes actually discarded.
      • get

        public void get​(ByteBuffer dst)
        This method transfers bytes from the head of this buffer queue into the given destination buffer. The number of bytes transferred will be the smaller of the number of bytes available and the remaining capacity of the destination buffer.
        Parameters:
        dst - the destination buffer into which bytes are to be written.
      • get

        public int get​(byte[] dst,
                       int offset,
                       int len)
        This method transfers bytes from the head of this buffer queue into the given destination byte. The number of bytes transferred will be the smaller of the number of requested bytes and the remaining capacity of the destination buffer.
        Parameters:
        dst - the destination byte array into which the bytes are to be written.
        offset - the offset in the byte array at which to write the bytes.
        len - the number of bytes to transfer.
        Returns:
        the actual number of bytes transferred.
      • get

        public byte get()
        Reads the next byte from this queue.
        Returns:
        The byte at the read index.
        Throws:
        BufferUnderflowException - If there are no remaining bytes to be read.
      • unget

        public void unget​(ByteBuffer src)
        This method transfers the bytes remaining in the given source buffer inserted at the head of this buffer queue.
        Parameters:
        src - The source buffer from which bytes are to be read.
      • toByteArray

        public byte[] toByteArray()
        Transfers all the bytes in the ByteBufferQueue from the read position to the write position into a new byte[].
        Returns:
        the byte[]