public class RingBuffer<T>
extends java.lang.Object
If the buffer overflows you can only retrieve the latest getLimit() bytes.
Modifier and Type | Field and Description |
---|---|
(package private) static int |
INITIAL_LIMIT
Initial limit: 1 MiB.
|
Constructor and Description |
---|
RingBuffer(int limit)
Create a ring buffer with the given size.
|
RingBuffer(int limit,
int initialLimit)
Create a ring buffer with the given size.
|
Modifier and Type | Method and Description |
---|---|
void |
append(T... items)
Append the given items to the buffer.
|
void |
append(T item)
Append one item.
|
void |
append(T[] items,
int offset,
int length)
Append the given byte array part to this buffer.
|
void |
clear()
Clear all data.
|
int |
getLimit()
Get the limit of this ring buffer.
|
long |
getNumberOfCollectedItems()
Get the total number of items appended to this buffer.
|
boolean |
isEmpty()
Is this ring buffer empty?
|
int |
size()
Get the number of valid bytes cached in this buffer.
|
Indexable<T> |
toIndexable()
Get a stable byte indexable from this buffer.
|
java.util.List<T> |
toList()
Create a list with the items in this buffer.
|
Indexable<T> |
view()
Get an indexable view of this ring buffer.
|
static final int INITIAL_LIMIT
public RingBuffer(int limit)
limit
- size limit of this bufferRingBuffer(int limit, int initialLimit)
limit
- size limit of this bufferinitialLimit
- initial limitpublic int getLimit()
public int size()
public boolean isEmpty()
true
if the buffer is emptyfalse
if notpublic void append(T item)
item
- item to append@SafeVarargs public final void append(@NotNull T... items)
items
- items to appendpublic void append(@NotNull T[] items, int offset, int length)
items
- items to appendoffset
- start offset of the bytes to appendlength
- number of bytes to appendjava.lang.IndexOutOfBoundsException
- if the parameters define a part which is at least partially outside the bytes
@NotNull public java.util.List<T> toList()
@NotNull public Indexable<T> view()
This is more efficient than copying all bytes in toList(), especially if only a few items of a larger content are of interest. But the price is high: any change to this buffer can break the returned view. So this is best used on a finished buffer.
toIndexable()
,
toList()
public void clear()
the total number of bytes handled by this buffer
.public long getNumberOfCollectedItems()