simtools.util
Class StreamMemoryBuffer.StreamReader

java.lang.Object
  extended by simtools.util.StreamMemoryBuffer.StreamReader
Enclosing class:
StreamMemoryBuffer

public class StreamMemoryBuffer.StreamReader
extends java.lang.Object

A buffer reader


Field Summary
protected  long mark
          The mark used to perform the writing of the buffer into a channel or into packet buffers
protected  java.nio.ByteBuffer readBuffer
          The read buffer : an independent view on the write buffer
 
Constructor Summary
protected StreamMemoryBuffer.StreamReader()
          Create a reader
 
Method Summary
 long bufferize(PacketBufferFactory factory, long timeout)
          Bufferize the buffer content from the mark to packet buffer
 int getLength(int readIndex)
          Get the packet length
 long getMinIndex()
          Get the oldest packet index
 long getNextReadIndex(long timeout, long lastReadIndex)
          Wait for the next writing on the buffer and return the index of this packet.
 long getReadIndex()
          Get the last packet index
 int read(long readIndex, int readOffset, int readLength, byte[] b, int offset)
          Read a packet
 void resetMark()
          Set the mark location equal to the last received packet
 void resetMark(int margin)
          Set the mark location wrt the oldest packet to read The mark is initialized with last available index at reader creation time.
 long save(java.nio.channels.ByteChannel channel, long timeout)
          Save the buffer content from the mark to the last written packet
protected  void validationErrorControl(PacketBufferValidationError e)
          Default managment of a packet validation error is to send the exception at upper level
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

readBuffer

protected final java.nio.ByteBuffer readBuffer
The read buffer : an independent view on the write buffer


mark

protected long mark
The mark used to perform the writing of the buffer into a channel or into packet buffers

Constructor Detail

StreamMemoryBuffer.StreamReader

protected StreamMemoryBuffer.StreamReader()
Create a reader

Method Detail

getNextReadIndex

public long getNextReadIndex(long timeout,
                             long lastReadIndex)
Wait for the next writing on the buffer and return the index of this packet. The index is equal to previous index if timeout ellapses. The retuned index is negative if nothing to read or in case of thread interruption.

Parameters:
timeout - the wait timeout
Returns:
the index of the last packet to read

resetMark

public void resetMark()
Set the mark location equal to the last received packet


resetMark

public void resetMark(int margin)
Set the mark location wrt the oldest packet to read The mark is initialized with last available index at reader creation time. To start saving while a unknown number of packets is already written the mark can be set with a margin relative to the oldest packet to read index. For instance margin can be equal to bufferSize/2

Parameters:
margin - the mark location relative to the oldest available packet

bufferize

public long bufferize(PacketBufferFactory factory,
                      long timeout)
               throws java.io.IOException,
                      PacketBufferValidationError
Bufferize the buffer content from the mark to packet buffer

Parameters:
a - packet factory to address the right buffers according to the key read from the packet
timeout - the timeout to wait
Returns:
the number of written bytes
Throws:
java.io.IOException
PacketBufferValidationError

validationErrorControl

protected void validationErrorControl(PacketBufferValidationError e)
                               throws PacketBufferValidationError
Default managment of a packet validation error is to send the exception at upper level

Parameters:
e - the validation error
Throws:
PacketBufferValidationError

save

public long save(java.nio.channels.ByteChannel channel,
                 long timeout)
          throws java.io.IOException
Save the buffer content from the mark to the last written packet

Parameters:
channel - the channel to write into
timeout - the timeout to wait
Returns:
the number of written bytes
Throws:
java.io.IOException

read

public int read(long readIndex,
                int readOffset,
                int readLength,
                byte[] b,
                int offset)
Read a packet

Parameters:
readIndex - the index of the packet to read
readOffset - the offset inside this packet
readLength - the number of bytes to read or -1 if it is up to the packet end
b - the array to which bytes are to be written
offset - the offset within the array of the first byte to be written
Returns:
the number of bytes read

getLength

public int getLength(int readIndex)
Get the packet length

Parameters:
readIndex - the index of the packet
Returns:
the number of bytes in this packet

getReadIndex

public long getReadIndex()
Get the last packet index

Returns:
the index

getMinIndex

public long getMinIndex()
Get the oldest packet index

Returns:
the index