Package org.agrona

Class ExpandableRingBuffer


  • public class ExpandableRingBuffer
    extends java.lang.Object
    Ring-buffer for storing messages which can expand to accommodate the messages written into it. Message are written and read in a FIFO order with capacity up to maxCapacity(). Messages can be iterated via for-each methods without consuming and having the option to begin iteration an offset from the current head() position.

    Note: This class is not thread safe.

    • Field Detail

      • MAX_CAPACITY

        public static final int MAX_CAPACITY
        Maximum capacity to which the ring buffer can grow which is 1 GB.
        See Also:
        Constant Field Values
      • HEADER_ALIGNMENT

        public static final int HEADER_ALIGNMENT
        Alignment in bytes for the beginning of message header.
        See Also:
        Constant Field Values
      • HEADER_LENGTH

        public static final int HEADER_LENGTH
        Length of encapsulating header.
        See Also:
        Constant Field Values
      • maxCapacity

        private final int maxCapacity
      • capacity

        private int capacity
      • mask

        private int mask
      • head

        private long head
      • tail

        private long tail
      • isDirect

        private final boolean isDirect
    • Constructor Detail

      • ExpandableRingBuffer

        public ExpandableRingBuffer()
        Create a new ring buffer which is initially compact and empty, has potential for MAX_CAPACITY, and using a direct ByteBuffer.
      • ExpandableRingBuffer

        public ExpandableRingBuffer​(int initialCapacity,
                                    int maxCapacity,
                                    boolean isDirect)
        Create a new ring buffer providing configuration for initial and max capacity, plus whether it is direct or not.
        Parameters:
        initialCapacity - required in the buffer.
        maxCapacity - the buffer can expand to.
        isDirect - is the ByteBuffer allocated direct or heap based.
    • Method Detail

      • isDirect

        public boolean isDirect()
        Is the ByteBuffer used for backing storage direct, that is off Java heap, or not.
        Returns:
        return true if direct ByteBuffer or false for heap based ByteBuffer.
      • maxCapacity

        public int maxCapacity()
        The maximum capacity to which the buffer can expand.
        Returns:
        maximum capacity to which the buffer can expand.
      • capacity

        public int capacity()
        Current capacity of the ring buffer in bytes.
        Returns:
        the current capacity of the ring buffer in bytes.
      • size

        public int size()
        Size of the ring buffer currently populated in bytes.
        Returns:
        size of the ring buffer currently populated in bytes.
      • isEmpty

        public boolean isEmpty()
        Is the ring buffer currently empty.
        Returns:
        true if the ring buffer is empty otherwise false.
      • head

        public long head()
        Head position in the buffer from which bytes are consumed forward toward the tail().
        Returns:
        head position in the buffer from which bytes are consumed forward towards the tail().
        See Also:
        consume(MessageConsumer, int)
      • tail

        public long tail()
        Tail position in the buffer at which new bytes are appended.
        Returns:
        tail position in the buffer at which new bytes are appended.
        See Also:
        append(DirectBuffer, int, int)
      • reset

        public void reset​(int requiredCapacity)
        Reset the buffer with a new capacity and empty state. Buffer capacity will grow or shrink as necessary.
        Parameters:
        requiredCapacity - for the ring buffer. If the same as exiting capacity then no adjustment is made.
      • append

        public boolean append​(DirectBuffer srcBuffer,
                              int srcOffset,
                              int srcLength)
        Append a message into the ring buffer, expanding the buffer if required.
        Parameters:
        srcBuffer - containing the encoded message.
        srcOffset - within the buffer at which the message begins.
        srcLength - of the encoded message in the buffer.
        Returns:
        true if successful otherwise false if maxCapacity() is reached.
      • resize

        private void resize​(int newMessageLength)
      • writeMessage

        private void writeMessage​(DirectBuffer srcBuffer,
                                  int srcOffset,
                                  int srcLength)