com.groovemanager.spi.asio
Class DoubleHalfRingBuffer

java.lang.Object
  extended by com.groovemanager.spi.asio.DoubleHalfRingBuffer

 class DoubleHalfRingBuffer
extends Object

A RingBuffer to be used with DoubleHalfBuffers.

Author:
Manu Robledo

Field Summary
private  int granularity
          The granularity in which data must be read or written.
private  boolean open
          Indicates whether this buffer is in open state or not
private  DoubleHalfBuffer readBuffer
          A view to the source buffer used for reading
private  boolean writeAhead
          When read and write position are the same, this value indicates, whether the buffer is filled completely (false) or emptied completely (true)
private  DoubleHalfBuffer writeBuffer
          A view to the source buffer used for reading
 
Constructor Summary
DoubleHalfRingBuffer(DoubleHalfBuffer buffer, int granularity)
          Construct a new DoubleHalfRingBuffer out of the given buffer
 
Method Summary
 void close()
          Close this buffer
 void flush()
          Empty this buffer
 boolean isOpen()
          Tells whether this buffer is currently open or not
 void open()
          Open this buffer
 int read(byte[] b, int off, int len)
          Try to read len bytes from this buffer starting at the current position into b starting at position off.
 int read(byte[] b, int off, int len, boolean overwrite)
          Try to read len bytes from this buffer starting at the current position into b starting at position off.
 int readAvailable()
          Get the number of bytes currently available for reading
 void rewind()
          Rewind this buffer
 void setPositions(int readPos, int writePos, boolean writeAhead)
          Set the read and write position of this Buffer
 int size()
          Get this buffer´s total size
 int write(byte[] b, int off, int len)
          Try to write len bytes from b starting at index off into this buffer starting at the current position.
 int write(byte[] b, int off, int len, boolean overwrite)
          Try to write len bytes from b starting at index off into this buffer starting at the current position.
 int writeAvailable()
          Get the number of bytes currently available for writing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

writeAhead

private boolean writeAhead
When read and write position are the same, this value indicates, whether the buffer is filled completely (false) or emptied completely (true)


open

private boolean open
Indicates whether this buffer is in open state or not


granularity

private final int granularity
The granularity in which data must be read or written. Data will only be available in blocks of this value.


readBuffer

private final DoubleHalfBuffer readBuffer
A view to the source buffer used for reading


writeBuffer

private final DoubleHalfBuffer writeBuffer
A view to the source buffer used for reading

Constructor Detail

DoubleHalfRingBuffer

public DoubleHalfRingBuffer(DoubleHalfBuffer buffer,
                            int granularity)
Construct a new DoubleHalfRingBuffer out of the given buffer

Parameters:
buffer - The buffer to be used as source for this ring buffer
granularity - The buffer granularity in bytes
Method Detail

readAvailable

public int readAvailable()
Get the number of bytes currently available for reading

Returns:
The number of bytes avilable for reading

writeAvailable

public int writeAvailable()
Get the number of bytes currently available for writing

Returns:
The number of bytes available for writing

read

public int read(byte[] b,
                int off,
                int len)
Try to read len bytes from this buffer starting at the current position into b starting at position off. The currently avilable bytes will be read the number of bytes read will be returned.

Parameters:
b - The byte array to transfer the data to
off - The offset inside the target array
len - The number of bytes to read
Returns:
The number of bytes read

read

public int read(byte[] b,
                int off,
                int len,
                boolean overwrite)
Try to read len bytes from this buffer starting at the current position into b starting at position off. If overwrite is true, the bytes will be read independent of the number of available bytes for reading. If this read operation exceeds the current write position (buffer underrun), the write position will be discarded and set to the end of the read data.

Parameters:
b - The byte array to transfer the data to
off - The offset inside the target array
len - The number of bytes to read
overwrite - true, if the given number of bytes should be read even if this causes a buffer underrun
Returns:
The number of bytes read

write

public int write(byte[] b,
                 int off,
                 int len)
Try to write len bytes from b starting at index off into this buffer starting at the current position. This method will not return before the given number of bytes has been written or the buffer has been closed. This means that if the given number of bytes can not be written at once, this methods waits until another thread empties the needed part of this buffer by reading from it.

Parameters:
b - The array containing the data to write
off - Start position inside the given array
len - The number of bytes to write
Returns:
The number of bytes written

write

public int write(byte[] b,
                 int off,
                 int len,
                 boolean overwrite)
Try to write len bytes from b starting at index off into this buffer starting at the current position. This method will not return before the given number of bytes has been written or the buffer has been closed. If overwrite is set to true, this method will write the data immediately independent of the number of bytes available for writing. If the read position is exceeded by this operation (buffer overflow), the read position will be set after the last written data.

Parameters:
b - The array containing the data to write
off - Start position inside the given array
len - The number of bytes to write
overwrite - true, if the given number of bytes should be written even if this causes a buffer overflow
Returns:
The number of bytes written

open

public void open()
Open this buffer


isOpen

public boolean isOpen()
Tells whether this buffer is currently open or not

Returns:
true, if this buffer is open, false otherwise

close

public void close()
Close this buffer


size

public int size()
Get this buffer´s total size

Returns:
This buffer´s total size in bytes

flush

public void flush()
Empty this buffer


rewind

public void rewind()
Rewind this buffer


setPositions

public void setPositions(int readPos,
                         int writePos,
                         boolean writeAhead)
Set the read and write position of this Buffer

Parameters:
readPos - The new read position
writePos - The new write position
writeAhead - Only needed, if readPos == writePos:
true, if the writePos is ahead of the readPos and the buffer is therefore filled, false if the buffer is empty.