|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.jack4j.RingBuffer
public class RingBuffer
Thread-safe, lock-free ringbuffer.
The ringbuffer can be accessed by two threads simultaneously. One thread can read from the buffer, while the other can write to it. The access to the ringbuffer need not to be synchronized, However, there can be only one reader and one writer thread, and their roles can't be interchanged.
Note on synchronization: the various read/write methods in this class are not synchronized (lack of synchronization is the point of lock-free buffers), but it's developer's responsibility to ensure that there's always only one thread reading, and only one thread writing.
Constructor Summary | |
---|---|
RingBuffer(int size)
Creates new ringbuffer that can hold specified number of bytes. |
|
RingBuffer(int size,
boolean freeWhenFinalized)
Creates new ringbuffer that can hold specified number of bytes. |
|
RingBuffer(long ringbufferHandle,
int capacity,
boolean freeWhenFinalized)
Creates new Java object, using pre-allocated ringbuffer. |
Method Summary | |
---|---|
java.nio.channels.ReadableByteChannel |
createReadableByteChannel()
Creates and returns ReadableByteChannel associated with this
RingBuffer. |
RingBufferVector |
createReadVector(java.nio.ByteOrder byteOrder)
Creates and returns RingBufferVector that covers readable part of the buffer. |
java.nio.channels.WritableByteChannel |
createWritableByteChannel()
Creates and returns WritableByteChannel associated with this
RingBuffer. |
RingBufferVector |
createWriteVector(java.nio.ByteOrder byteOrder)
Creates and returns RingBufferVector that covers writable part of the buffer. |
protected void |
finalize()
Frees the ringbuffer, if it didn't happen before. |
void |
free()
Frees the ringbuffer. |
int |
getCapacity()
Returns the total capacity of the ringbuffer (in bytes). |
boolean |
getFreeWhenFinalized()
Returns true if the native ringbuffer will be freed when this Java object is finalized. |
int |
getReadSpaceBytes()
Returns the number of bytes available for reading. |
int |
getReadSpaceSamples()
Returns the number of samples available for reading. |
long |
getRingbufferHandle()
Returns the native pointer to jack_ringbuffer_t structure. |
int |
getWriteSpaceBytes()
Returns the number of bytes available for writing. |
int |
getWriteSpaceSamples()
Returns the number of samples available for writing. |
void |
mlock()
Attempts to lock the underlying native buffer in memory. |
void |
readAdvanceBytes(int size)
Advances read position in the ringbuffer, skipping some readable data. |
void |
readAdvanceSamples(int size)
Advances read position in the ringbuffer, skipping some readable data. |
int |
readBytesToArray(byte[] array,
int start,
int size)
Reads data from the ringbuffer and stores them into specified direct buffer, then advances read pointer. |
int |
readBytesToArray(byte[] array,
int start,
int size,
boolean advance)
Reads data from the ringbuffer and stores them into specified direct buffer, then optionally advances read pointer. |
int |
readBytesToBuffer(java.nio.ByteBuffer buffer,
int size)
Reads data from the ringbuffer and stores them into specified direct buffer, then optionally advances read pointer. |
int |
readBytesToBuffer(java.nio.ByteBuffer buffer,
int size,
boolean advance)
Reads data from the ringbuffer and stores them into specified direct buffer, then optionally advances read pointer. |
int |
readBytesToDirectBuffer(java.nio.ByteBuffer buffer,
int size)
Reads data from the ringbuffer and stores them into specified direct buffer, then advances pointer. |
int |
readBytesToDirectBuffer(java.nio.ByteBuffer buffer,
int size,
boolean advance)
Reads data from the ringbuffer and stores them into specified direct buffer, then optionally advances read pointer. |
int |
readSamplesToDirectBuffer(java.nio.FloatBuffer buffer,
int size)
Reads data from the ringbuffer and stores them into specified direct buffer, then advances pointer. |
int |
readSamplesToDirectBuffer(java.nio.FloatBuffer buffer,
int size,
boolean advance)
Reads data from the ringbuffer and stores them into specified direct buffer, then optionally advances read pointer. |
void |
reset()
Resets (clears) the buffer. |
void |
setFreeWhenFinalized(boolean freeWhenFinalized)
Sets the flag that says if the native ringbuffer will be freed when this Java object is finalized. |
void |
writeAdvanceBytes(int size)
Advances write position in the ringbuffer, skipping some writable space. |
void |
writeAdvanceSamples(int size)
Advances write position in the ringbuffer, skipping some writable space. |
int |
writeBytesFromArray(byte[] array,
int start,
int size)
Writes from the specified array into ringbuffer. |
int |
writeBytesFromBuffer(java.nio.ByteBuffer buffer,
int size)
Writes from the specified direct buffer into ringbuffer. |
int |
writeBytesFromDirectBuffer(java.nio.ByteBuffer buffer,
int size)
Writes from the specified direct buffer into ringbuffer. |
int |
writeSamplesFromDirectBuffer(java.nio.FloatBuffer buffer,
int size)
Writes from the specified direct buffer into ringbuffer. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public RingBuffer(int size, boolean freeWhenFinalized) throws JackException
The size is in bytes, not samples!
size
- size in bytesfreeWhenFinalized
- specifies whether free()
should be called during
finalization
JackException
public RingBuffer(int size) throws JackException
Equivalent to RingBuffer(size, true)
JackException
public RingBuffer(long ringbufferHandle, int capacity, boolean freeWhenFinalized)
This method is useful when you obtain pointer to
jack_ringbuffer_t
from another JNI call and want to build
the Java object around that native ringbuffer.
ringbufferHandle
- pointer to jack_ringbuffer_t
, packed as
long
capacity
- total capacity of the ringbufferfreeWhenFinalized
- specifies whether free()
should be called during
finalizationMethod Detail |
---|
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
Object.finalize()
public void free() throws JackException
This object will be unusable after the call.
This method may be called multiple calls; second and subsequent calls have no effect.
JackException
public long getRingbufferHandle()
jack_ringbuffer_t
structure.
The pointer is packed as long
value, and can be used in
JNI calls.
public int getCapacity()
public java.nio.channels.ReadableByteChannel createReadableByteChannel()
ReadableByteChannel
associated with this
RingBuffer.
It's recommended (faster) to use only direct buffers with the returned channel.
The channel behaves as a non-blocking channel, that is, read method doesn't block and reads only as many bytes as is available for reading in the associated RingBuffer.
The channel must not be used by multiple threads simultaneously.
This also means that the read
method of the returned
channel doesn't fully conform to ReadableByteChannel specification - if
another thread starts reading from the channel during read operation, it
won't block and the results are unpredictable.
Closing the channel doesn't affect underlying buffer. If the underlying
buffer is freed, the channel's read
method will throw
exception.
public java.nio.channels.WritableByteChannel createWritableByteChannel()
WritableByteChannel
associated with this
RingBuffer.
It's recommended (faster) to use only direct buffers with the returned channel.
The channel behaves as a non-blocking channel, that is, write method doesn't block and writes only as many bytes as is available for writing in the associated RingBuffer.
The channel must not be used by multiple threads simultaneously.
This also means that the write
method of the returned
channel doesn't fully conform to WritableByteChannel specification - if
another thread starts writing to the channel during write operation, it
won't block and the results are unpredictable.
Closing the channel doesn't affect underlying buffer. If the underlying
buffer is freed, the channel's write
method will throw
exception.
public int getReadSpaceBytes()
public int getReadSpaceSamples()
public int getWriteSpaceBytes()
public int getWriteSpaceSamples()
public int readBytesToDirectBuffer(java.nio.ByteBuffer buffer, int size, boolean advance)
This method behaves exactly as
readBytesToBuffer(ByteBuffer, int)
, but the specified
buffer
must be a direct buffer, otherwise an exception is
thrown.
public int readBytesToDirectBuffer(java.nio.ByteBuffer buffer, int size)
This is an equivalent to
readBytesToDirectBuffer(buffer, size, true)
public int readSamplesToDirectBuffer(java.nio.FloatBuffer buffer, int size, boolean advance)
If the buffer
is not direct, the method throws
IllegalArgumentException.
The number of samples read is a minimum of: size
parameter, return value of buffer.remaining()
, and return
value of getReadSpaceSamples()
.
public int readSamplesToDirectBuffer(java.nio.FloatBuffer buffer, int size)
This is an equivalent to
readSamplesToDirectBuffer(buffer, size, true)
public int readBytesToBuffer(java.nio.ByteBuffer buffer, int size, boolean advance)
The number of bytes read is a minimum of: specified size
parameter, available space in target buffer
, and size of
available data in the ringbuffer, as returned by
getReadSpaceBytes()
.
public int readBytesToBuffer(java.nio.ByteBuffer buffer, int size)
This is equivalent to readBytesToBuffer(buffer, size, true)
public int readBytesToArray(byte[] array, int start, int size, boolean advance)
The number of bytes read is a minimum of specified size
parameter and size of available data in the ringbuffer, as returned by
getReadSpaceBytes()
.
start
must be non-negative and not greater than
array.length
; size
must be non-negative
and not greater than array.length - start
.
array
- the array into which will the data be copiedstart
- index of first element to overwritesize
- number of requested bytesadvance
- if set to true, read pointer is advanced after read
JackException
- if internal error occurs
java.lang.IndexOutOfBoundsException
- if start
or size
are out of
boundspublic int readBytesToArray(byte[] array, int start, int size)
This is equivalent to
readBytesToArray(array, start, size, true)
public void readAdvanceBytes(int size)
size
- number of bytes to skippublic void readAdvanceSamples(int size)
size
- number of samples to skippublic RingBufferVector createReadVector(java.nio.ByteOrder byteOrder)
See RingBufferVector
description for details.
The use of these vectors in Java is not very efficient.
byteOrder
- requested byte order of the ByteBuffers in the returned vectorpublic int writeBytesFromDirectBuffer(java.nio.ByteBuffer buffer, int size)
This method behaves exactly as
writeBytesFromBuffer(ByteBuffer, int)
, but the specified
buffer
must be a direct buffer, otherwise an exception is
thrown.
public int writeSamplesFromDirectBuffer(java.nio.FloatBuffer buffer, int size)
If the buffer
is not direct, the method throws
IllegalArgumentException.
The number of bytes written is a minimum of: size
parameter, return value of buffer.remaining()
, and return
value of getWriteSpaceBytes()
.
public int writeBytesFromBuffer(java.nio.ByteBuffer buffer, int size)
The number of bytes written is a minimum of: specified size
parameter, number of available bytes in target buffer
,
and size of available data in the ringbuffer, as returned by
getWriteSpaceBytes()
.
public int writeBytesFromArray(byte[] array, int start, int size)
The number of bytes read is a minimum of specified size
parameter and size of available data in the ringbuffer, as returned by
getWriteSpaceBytes()
.
start
must be non-negative and not greater than
array.length
; size
must be non-negative
and not greater than array.length - start
.
array
- the array from which will the data be copiedstart
- index of first element that holds the datasize
- requested number of bytes to copy
java.lang.IndexOutOfBoundsException
- if start
or size
are out of
boundspublic void writeAdvanceBytes(int size)
size
- number of bytes to skippublic void writeAdvanceSamples(int size)
size
- number of samples to skippublic RingBufferVector createWriteVector(java.nio.ByteOrder byteOrder)
See RingBufferVector
description for details.
The use of these vectors in Java is not very efficient.
byteOrder
- requested byte order of the ByteBuffers in the returned vectorpublic void reset()
This method is not thread-safe.
public void mlock() throws JackException
You can call this if you want to use the ringbuffer during real-time callbacks and want to avoid swapping.
JackException
public boolean getFreeWhenFinalized()
public void setFreeWhenFinalized(boolean freeWhenFinalized)
Normally, the flag is set to true; setting it to false may be useful if you want to share the ringbuffer with native code that will survive the destruction of this Java object.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |