public abstract class RingBuffer
extends java.lang.Object
The virtual write position is always "ahead" of the virtual read position. That is because something has to be written first before it could be read.
Thread safety: This functionality is safe to be used by one "reader" thread and one "writer" thread concurrently as long as the reader thread only updates the virtual read position (it's OK to get the virtual write position) and the "writer" thread only updates the virtual write position (it's OK to get the virtual read position.)
Modifier | Constructor and Description |
---|---|
protected |
RingBuffer(int bufferLength)
Constructor to be called from concrete implementations.
|
Modifier and Type | Method and Description |
---|---|
int |
advanceReadPosition(int delta)
This method must be called to indicate a successful read operation.
|
int |
advanceWritePosition(int delta)
This method must be called to indicate a successful write operation.
|
int |
getAvailableToRead()
Gets the count of items that are available to be read.
|
int |
getAvailableToReadStraight()
Gets the count of items that are available to be read at once.
|
int |
getAvailableToWrite()
Gets the count of items that are available to be written.
|
int |
getAvailableToWriteStraight()
Gets the count of items that are available to be written at once.
|
int |
getBufferLength()
Gets the length of the underlying buffer.
|
int |
getReadPosition()
Gets the current virtual read position.
|
long |
getTotalReadCount()
Gets the total count of read items.
|
long |
getTotalWriteCount()
Gets the total count of written items.
|
int |
getWritePosition()
Gets the current virtual write position.
|
boolean |
isEOF()
Gets the EOF state of this ring buffer.
|
void |
setEOF()
Sets the EOF state of this ring buffer.
|
protected RingBuffer(int bufferLength)
bufferLength
- Length of the underlying buffer.public boolean isEOF()
public void setEOF()
public int getBufferLength()
public int getReadPosition()
public int getWritePosition()
public long getTotalReadCount()
public long getTotalWriteCount()
public int getAvailableToRead()
getAvailableToReadStraight()
.public int getAvailableToReadStraight()
getAvailableToRead()
.public int getAvailableToWrite()
getAvailableToWriteStraight()
.public int getAvailableToWriteStraight()
getAvailableToWrite()
.public int advanceReadPosition(int delta)
An attempt to advance the read position beyond the value returned by getAvailableToRead()
limits the attempt to the value of getAvailableToRead()
.
That is not an exceptional case.
delta
- The count of virtual notches by which to increment the virtual read position.getAvailableToRead()
.public int advanceWritePosition(int delta)
An attempt to advance the write position beyond the value returned by getAvailableToWrite()
limits the attempt to the value of getAvailableToWrite()
.
That is not an exceptional case.
delta
- The count of virtual notches by which to increment the virtual write position.getAvailableToWrite()
.