Class CircularByteBuffer


  • public class CircularByteBuffer
    extends java.lang.Object
    A circular byte buffer (also called ring buffer) allows putting and and getting bytes in a FIFO way. Typical use cases are (usually concurrent/asynchronous) producers and consumers operating on bytes. This enables building a multi-threaded processing pipeline.

    All put&get methods are non-blocking.

    This class is thread-safe.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Returns the number of bytes available and can be get without additional puts.
      int capacity()
      The capacity (size) is the maximum of bytes that can be stored inside this buffer.
      void clear()
      Clears all data from the buffer.
      int free()
      Returns the number of free bytes available that can still be put without additional gets.
      int get()
      Gets a single byte return or -1 if no data is available.
      int get​(byte[] dst)
      Gets as many of the requested bytes as available from this buffer.
      int get​(byte[] dst, int off, int len)
      Gets as many of the requested bytes as available from this buffer.
      int peek()
      Return the first byte a get would return or -1 if no data is available.
      boolean put​(byte value)
      Puts a single byte if the buffer is not yet full.
      int put​(byte[] src)
      Puts as many of the given bytes as possible into this buffer.
      int put​(byte[] src, int off, int len)
      Puts as many of the given bytes as possible into this buffer.
      byte[] rawBuffer()
      Usually you should prefer one of the get() methods.
      int rawIndexGet()
      The index to get (read) from.
      int rawIndexPut()
      The index to put (write) to.
      int skip​(int count)
      Skips the given count of bytes, but at most the currently available count.
      • Methods inherited from class java.lang.Object

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

      • buffer

        private final byte[] buffer
      • capacity

        private final int capacity
      • available

        private int available
      • idxGet

        private int idxGet
      • idxPut

        private int idxPut
    • Constructor Detail

      • CircularByteBuffer

        public CircularByteBuffer()
      • CircularByteBuffer

        public CircularByteBuffer​(int capacity)
    • Method Detail

      • clear

        public void clear()
        Clears all data from the buffer.
      • get

        public int get()
        Gets a single byte return or -1 if no data is available.
      • get

        public int get​(byte[] dst)
        Gets as many of the requested bytes as available from this buffer.
        Returns:
        number of bytes actually got from this buffer (0 if no bytes are available)
      • get

        public int get​(byte[] dst,
                       int off,
                       int len)
        Gets as many of the requested bytes as available from this buffer.
        Returns:
        number of bytes actually got from this buffer (0 if no bytes are available)
      • put

        public boolean put​(byte value)
        Puts a single byte if the buffer is not yet full.
        Returns:
        true if the byte was put, or false if the buffer is full
      • put

        public int put​(byte[] src)
        Puts as many of the given bytes as possible into this buffer.
        Returns:
        number of bytes actually put into this buffer (0 if the buffer is full)
      • put

        public int put​(byte[] src,
                       int off,
                       int len)
        Puts as many of the given bytes as possible into this buffer.
        Returns:
        number of bytes actually put into this buffer (0 if the buffer is full)
      • peek

        public int peek()
        Return the first byte a get would return or -1 if no data is available.
      • skip

        public int skip​(int count)
        Skips the given count of bytes, but at most the currently available count.
        Returns:
        number of bytes actually skipped from this buffer (0 if no bytes are available)
      • capacity

        public int capacity()
        The capacity (size) is the maximum of bytes that can be stored inside this buffer.
      • available

        public int available()
        Returns the number of bytes available and can be get without additional puts.
      • free

        public int free()
        Returns the number of free bytes available that can still be put without additional gets.
      • rawBuffer

        public byte[] rawBuffer()
        Usually you should prefer one of the get() methods. If you still want direct access (less safe) you can use this method in combination with rawIndexGet() and rawIndexPut().
      • rawIndexGet

        public int rawIndexGet()
        The index to get (read) from.
      • rawIndexPut

        public int rawIndexPut()
        The index to put (write) to.