Class RingBufferBase<A>

java.lang.Object
org.aksw.commons.io.buffer.ring.RingBufferBase<A>
Type Parameters:
A -
All Implemented Interfaces:
HasArrayOps<A>, ReadableSource<A>
Direct Known Subclasses:
RingBufferForBytes

public abstract class RingBufferBase<A> extends Object implements ReadableSource<A>
A fixed size buffer with start and end pointers. The read operations increment the start pointer. The fill operation reads from a source and increments the end pointer. Both pointers 'overflow' when reaching the end of the array and start from offset 0 again. If a read operation causes the start and end pointers to meet then the buffer is considered empty. If a fill operation causes the start and end pointers to meet then the buffer is considered full. A flag is used to discriminate those cases.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected A
     
    protected int
     
    protected int
     
    protected boolean
     
    protected int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    RingBufferBase(A buffer)
     
    RingBufferBase(A buffer, int start, int end, boolean isEmpty)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the maxmimum number of bytes that can be appended in a single operation before the end marker flips over or reaches the start marker.
    int
    The number of used bytes - distance between start and end.
    int
    The number of unused bytes - distance between end and start.
    int
     
    int
    fill(ReadableSource<A> source, int targetAmount)
     
    int
     
    int
    The internal array is not exposed because the implementation may change to e.g.
    protected void
    incEnd(int n)
     
    protected void
    incStart(int amount)
     
    boolean
     
    int
     
    protected int
    preRead(int length)
    Returns a number of 'n' bytes that can be read from the current start marker up to the requested length.
    int
    read(A tgt, int tgtOffset, int length)
    Take items from the start of the buffer.
    void
    resize(int newSize)
    Set the buffer to a new size.
    skip(int length)
    Increment the start pointer by the given amount.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.aksw.commons.io.buffer.array.HasArrayOps

    getArrayOps

    Methods inherited from interface org.aksw.commons.io.input.ReadableSource

    readRaw
  • Field Details

    • buffer

      protected A buffer
    • bufferLen

      protected int bufferLen
    • start

      protected int start
    • end

      protected int end
    • isEmpty

      protected boolean isEmpty
  • Constructor Details

    • RingBufferBase

      public RingBufferBase(A buffer)
    • RingBufferBase

      public RingBufferBase(A buffer, int start, int end, boolean isEmpty)
  • Method Details

    • getStart

      public int getStart()
      The internal array is not exposed because the implementation may change to e.g. use an array of arrays to store the data.
    • getEnd

      public int getEnd()
    • isEmpty

      public boolean isEmpty()
    • fill

      public int fill(ReadableSource<A> source, int targetAmount) throws IOException
      Parameters:
      source -
      targetAmount - Fill the buffer to this amount (NOT the amount to read from the source)
      Returns:
      Throws:
      IOException
    • appendCapacity

      public int appendCapacity()
      Returns the maxmimum number of bytes that can be appended in a single operation before the end marker flips over or reaches the start marker.
    • fill

      public int fill(ReadableSource<A> source) throws IOException
      Throws:
      IOException
    • incEnd

      protected void incEnd(int n)
    • available

      public int available()
      The number of used bytes - distance between start and end.
    • capacity

      public int capacity()
      The number of unused bytes - distance between end and start.
    • length

      public int length()
    • skip

      public RingBufferBase<A> skip(int length)
      Increment the start pointer by the given amount. Raises an invalid argument exception if the length is greater than available().
    • read

      public int read(A tgt, int tgtOffset, int length) throws IOException
      Take items from the start of the buffer.
      Specified by:
      read in interface ReadableSource<A>
      Parameters:
      tgt - The array into which to put the read data
      tgtOffset - Offset into array where to start writing
      length - Maximum number of items to read.
      Returns:
      The number of items read. Return -1 if end of data was reached, and 0 iff length was 0.
      Throws:
      IOException
    • preRead

      protected int preRead(int length)
      Returns a number of 'n' bytes that can be read from the current start marker up to the requested length. When this method returns, then the fields of this buffer have been updated to the state after the read. However, the actual data can be accessed by reading 'n' bytes from the original start position. (See implementation of read.)
    • incStart

      protected void incStart(int amount)
    • resize

      public void resize(int newSize)
      Set the buffer to a new size.