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
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 -
Constructor Summary
ConstructorsConstructorDescriptionRingBufferBase(A buffer) RingBufferBase(A buffer, int start, int end, boolean isEmpty) -
Method Summary
Modifier and TypeMethodDescriptionintReturns the maxmimum number of bytes that can be appended in a single operation before the end marker flips over or reaches the start marker.intThe number of used bytes - distance between start and end.intcapacity()The number of unused bytes - distance between end and start.intfill(ReadableSource<A> source) intfill(ReadableSource<A> source, int targetAmount) intgetEnd()intgetStart()The internal array is not exposed because the implementation may change to e.g.protected voidincEnd(int n) protected voidincStart(int amount) booleanisEmpty()intlength()protected intpreRead(int length) Returns a number of 'n' bytes that can be read from the current start marker up to the requested length.intTake items from the start of the buffer.voidresize(int newSize) Set the buffer to a new size.skip(int length) Increment the start pointer by the given amount.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface HasArrayOps
getArrayOpsMethods inherited from interface ReadableSource
readRaw
-
Field Details
-
buffer
-
bufferLen
protected int bufferLen -
start
protected int start -
end
protected int end -
isEmpty
protected boolean isEmpty
-
-
Constructor Details
-
RingBufferBase
-
RingBufferBase
-
-
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
- 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
- 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
Increment the start pointer by the given amount. Raises an invalid argument exception if the length is greater thanavailable(). -
read
Take items from the start of the buffer.- Specified by:
readin interfaceReadableSource<A>- Parameters:
tgt- The array into which to put the read datatgtOffset- Offset into array where to start writinglength- 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.
-