public class ByteRingBuffer
extends java.lang.Object
All methods of this class are non-blocking and not synchronized (not thread-safe).
Constructor and Description |
---|
ByteRingBuffer(int size)
Creates a ring buffer.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the ring buffer.
|
void |
discard(int len)
Discards the oldest
len bytes within the ring buffer. |
int |
getFree()
Returns the number of free bytes within the ring buffer.
|
int |
getSize()
Returns the size of the ring buffer.
|
int |
getUsed()
Returns the number of used bytes within the ring buffer.
|
int |
read(byte[] buf)
Reads data from the ring buffer.
|
int |
read(byte[] buf,
int pos,
int len)
Reads data from the ring buffer.
|
void |
resize(int newSize)
Resizes the ring buffer by preserving it's data.
|
int |
write(byte[] buf)
Writes data to the ring buffer.
|
int |
write(byte[] buf,
int pos,
int len)
Writes data to the ring buffer.
|
public void resize(int newSize)
If the new size is not enough to keep all used data in the ring buffer, the excess old data is discarded.
public int getSize()
public int getFree()
public int getUsed()
public void clear()
public void discard(int len)
len
bytes within the ring buffer.
This has the same effect as calling read(new byte[len], 0, len)
.len
- The number of bytes to be discarded.public int write(byte[] buf, int pos, int len)
min(len, getFree())
.public int write(byte[] buf)
Convenience method for: write(buf, 0, buf.length)
public int read(byte[] buf, int pos, int len)
min(len, getUsed())
.public int read(byte[] buf)
Convenience method for: read(buf, 0, buf.length)