Class CircularByteBuffer

java.lang.Object
org.greenrobot.essentials.io.CircularByteBuffer

public class CircularByteBuffer extends 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 putinvalid input: '&get' methods are non-blocking.

This class is thread-safe.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
     
    private final byte[]
     
    private final int
     
    private int
     
    private int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    CircularByteBuffer(int capacity)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of bytes available and can be get without additional puts.
    int
    The capacity (size) is the maximum of bytes that can be stored inside this buffer.
    void
    Clears all data from the buffer.
    int
    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
    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[]
    Usually you should prefer one of the get() methods.
    int
    The index to get (read) from.
    int
    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 Details

    • buffer

      private final byte[] buffer
    • capacity

      private final int capacity
    • available

      private int available
    • idxGet

      private int idxGet
    • idxPut

      private int idxPut
  • Constructor Details

    • CircularByteBuffer

      public CircularByteBuffer()
    • CircularByteBuffer

      public CircularByteBuffer(int capacity)
  • Method Details

    • 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.