public class ByteBufferSupply
extends java.lang.Object
1) It can be used as a simple supply of ByteBuffer(Item)s. In this mode, only get() and release() are called. A user does a get(), uses that buffer, then calls release() when done with it. If there are multiple users of a single buffer (say 5), then call bufferItem.setUsers(5) before it is used and the buffer is only released when all 5 users have called release().
2) As in the first usage, it can be used as a supply of ByteBuffers, but each buffer can be preset to a specific ByteBuffer object. Thus it can act as a supply of buffers in which each contains specific data. Because of the circular nature of the ring used to implement this code, after all ByteBuffers have been gotten by the user for the first time, it starts back over with the first -- going round and round.
To implement this, use the constructor which takes a list of ByteBuffer objects with which to fill this supply. The user does a getAsIs() which does not clear the buffer's position and limit. When finished reading/writing, user calls release(). It's up to the user to maintain proper values for the buffer's position and limit since it will be used again. If there are multiple users of a single buffer (say 5), then call bufferItem.setUsers(5) before it is used and the buffer is only released when all 5 users have called release().
3) It can be used as a supply of ByteBuffers in which a single producer provides data for a single consumer which is waiting for that data. The producer does a get(), fills the buffer with data, and finally does a publish() to let the consumer know the data is ready. Simultaneously, a consumer does a consumerGet() to access the data once it is ready. The consumer then calls release() when finished which allows the producer to reuse the now unused buffer.
Modifier and Type | Field and Description |
---|---|
java.lang.Object |
auxObject
Object to be associated with each ByteBufferItem.
|
Constructor and Description |
---|
ByteBufferSupply(int ringSize,
int bufferSize)
Constructor.
|
ByteBufferSupply(int ringSize,
int bufferSize,
java.nio.ByteOrder order,
boolean direct)
Constructor.
|
ByteBufferSupply(int ringSize,
int bufferSize,
java.nio.ByteOrder order,
boolean direct,
boolean orderedRelease)
Constructor.
|
ByteBufferSupply(int ringSize,
int bufferSize,
java.nio.ByteOrder order,
boolean direct,
boolean orderedRelease,
org.jlab.coda.jevio.EvioNodePool[] pools)
Constructor.
|
ByteBufferSupply(int ringSize,
java.util.List<java.nio.ByteBuffer> bufList,
boolean orderedRelease)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
ByteBufferItem |
consumerGet()
Get the next available item in ring buffer for getting data already written into.
|
ByteBufferItem |
get()
Get the next available item in ring buffer for writing/reading data.
|
ByteBufferItem |
getAsIs()
Get the next available item in ring buffer for writing/reading data.
|
int |
getFillLevel()
What percentage of the byte buffers are being used?
If this ring is full (of unused buffers), it corresponds to an empty input or output channel (0%).
|
void |
publish(ByteBufferItem byteBufferItem)
Used to tell that the consumer that the ring buffer item is ready for consumption.
|
void |
release(ByteBufferItem item)
Consumer releases claim on the given ring buffer item so it becomes available for reuse.
|
public java.lang.Object auxObject
public ByteBufferSupply(int ringSize, int bufferSize) throws java.lang.IllegalArgumentException
ringSize
- number of ByteBufferItem objects in ring buffer.bufferSize
- initial size (bytes) of ByteBuffer in each ByteBufferItem object.java.lang.IllegalArgumentException
- if args < 1 or ringSize not power of 2.public ByteBufferSupply(int ringSize, int bufferSize, java.nio.ByteOrder order, boolean direct) throws java.lang.IllegalArgumentException
ringSize
- number of ByteBufferItem objects in ring buffer.bufferSize
- initial size (bytes) of ByteBuffer in each ByteBufferItem object.order
- byte order of ByteBuffer in each ByteBufferItem object.direct
- if true, make ByteBuffers direct.java.lang.IllegalArgumentException
- if args < 1 or ringSize not power of 2.public ByteBufferSupply(int ringSize, int bufferSize, java.nio.ByteOrder order, boolean direct, boolean orderedRelease) throws java.lang.IllegalArgumentException
ringSize
- number of ByteBufferItem objects in ring buffer.bufferSize
- initial size (bytes) of ByteBuffer in each ByteBufferItem object.order
- byte order of ByteBuffer in each ByteBufferItem object.direct
- if true, make ByteBuffers direct.orderedRelease
- if true, the user promises to release the ByteBufferItems
in the same order as acquired. This avoids using
synchronized code (no locks).java.lang.IllegalArgumentException
- if args < 1 or ringSize not power of 2.public ByteBufferSupply(int ringSize, int bufferSize, java.nio.ByteOrder order, boolean direct, boolean orderedRelease, org.jlab.coda.jevio.EvioNodePool[] pools) throws java.lang.IllegalArgumentException
In addition, each ByteBufferItem in this supply will have an object associated with it which is passed in through the last argument. Used to store an EvioNodePool with each buffer in the supply.
ringSize
- number of ByteBufferItem objects in ring buffer.bufferSize
- initial size (bytes) of ByteBuffer in each ByteBufferItem object.order
- byte order of ByteBuffer in each ByteBufferItem object.direct
- if true, make ByteBuffers direct.orderedRelease
- if true, the user promises to release the ByteBufferItems
in the same order as acquired. This avoids using
synchronized code (no locks).pools
- one EvioNodePool for each ByteBuffer.java.lang.IllegalArgumentException
- if args < 1 or ringSize not power of 2,
pools array must have not be null or have less than
ringSize number of elements.public ByteBufferSupply(int ringSize, java.util.List<java.nio.ByteBuffer> bufList, boolean orderedRelease) throws java.lang.IllegalArgumentException
ringSize
- number of ByteBufferItem objects in ring buffer.bufList
- list of ByteBuffers used to populate this supply.
List must contain ringSize number of buffers.orderedRelease
- if true, the user promises to release the ByteBufferItems
in the same order as acquired. This avoids using
synchronized code (no locks).java.lang.IllegalArgumentException
- bad arg or ringSize not power of 2.public int getFillLevel()
public ByteBufferItem get() throws java.lang.InterruptedException
java.lang.InterruptedException
- if thread interrupted.public ByteBufferItem getAsIs() throws java.lang.InterruptedException
java.lang.InterruptedException
- if thread interrupted.public ByteBufferItem consumerGet() throws java.lang.InterruptedException
java.lang.InterruptedException
- if thread interrupted.public void release(ByteBufferItem item)
get()
and consumerGet()
.item
- item in ring buffer to release for reuse.public void publish(ByteBufferItem byteBufferItem)
get()
and consumerGet()
.byteBufferItem
- item available for consumer's use.