simtools.util
Class StreamMemoryBuffer

java.lang.Object
  extended by simtools.util.StreamMemoryBuffer

public class StreamMemoryBuffer
extends java.lang.Object

A memory buffer to be filled by one writer and read by several readers The readers do not empty the buffer, they acces data in it using an index The writer is filling the circular buffer it writes at a given index and increments a minimum index value to prevent access from the readers on overwritten data due to the cyclic buffer. Thus the writer is never constrained by the readers. The readers have to be fast enough to read the buffer. The buffer is allocated into native memory and thus can be quite large. This implementation is based on the assumption that packets size has a maximum value and that the minimum value is not too much different from the maximum value. If not efficient in terms of memory usage, it is assumed that a subclass will manage it.

Author:
cazenave_c

Nested Class Summary
 class StreamMemoryBuffer.StreamReader
          A buffer reader
 
Field Summary
protected  java.nio.ByteBuffer buffer
          The memory circular buffer
protected  long bufferSize
          The size of the circular buffer= number of packets in memory
protected  int counter
          The numbe of times the circular buffer is filled (starting at 1)
protected  long index
          The index of the next packet to be written
protected  int[] lengths
          A circular array with packets length
protected  long minIndex
          The last availble packet index (-1 at the beginning)
protected  int streamMaxSize
          The maximum size for one packet
protected  java.lang.Object synchro
          A syncho to allow readers to synchrosize on the buffer updates
 
Constructor Summary
StreamMemoryBuffer(int bufferSize, int streamMaxSize)
          Create a new memory buffer
 
Method Summary
 void clean()
          Clean the StreamMemoryBuffer
 StreamMemoryBuffer.StreamReader createReader()
          Create a reader on this buffer This method is overwritten to implement specific readers
 long write(byte[] b, int offset, int length)
          Write a packet into the buffer
 long write(java.nio.ByteBuffer b)
          Write a byte buffer into the buffer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

protected java.nio.ByteBuffer buffer
The memory circular buffer


lengths

protected int[] lengths
A circular array with packets length


bufferSize

protected final long bufferSize
The size of the circular buffer= number of packets in memory


streamMaxSize

protected final int streamMaxSize
The maximum size for one packet


index

protected long index
The index of the next packet to be written


minIndex

protected long minIndex
The last availble packet index (-1 at the beginning)


counter

protected int counter
The numbe of times the circular buffer is filled (starting at 1)


synchro

protected java.lang.Object synchro
A syncho to allow readers to synchrosize on the buffer updates

Constructor Detail

StreamMemoryBuffer

public StreamMemoryBuffer(int bufferSize,
                          int streamMaxSize)
Create a new memory buffer

Parameters:
bufferSize - the number of packets to store
streamMaxSize - the max size for one packet
Method Detail

write

public long write(byte[] b,
                  int offset,
                  int length)
Write a packet into the buffer

Parameters:
b - The array from which bytes are to be read
offset - The offset within the array of the first byte to be read
length - The number of bytes to be read from the given array
Returns:
the written packet index

write

public long write(java.nio.ByteBuffer b)
Write a byte buffer into the buffer

Parameters:
b - the byte buffer
Returns:
the written packet index

createReader

public StreamMemoryBuffer.StreamReader createReader()
Create a reader on this buffer This method is overwritten to implement specific readers

Returns:
the new reader

clean

public void clean()
Clean the StreamMemoryBuffer