com.groovemanager.tools
Class RingBuffer

java.lang.Object
  extended by com.groovemanager.tools.RingBuffer

public class RingBuffer
extends java.lang.Object

A RingBuffer represents a buffer that has a read and a write position which are set back to 0 when the end is reached. Buffer underruns or overflows are avoided by only allowing writing or reading aslong as there are enough bytes available for the deired operation. As one can expect that reader and writer will be different threads, this implementation is synchronized where needed.

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  java.nio.ByteBuffer 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  java.nio.ByteBuffer writeBuffer
          A view to the source buffer used for reading
 
Constructor Summary
RingBuffer(byte[] buffer, int offset, int length, int granularity)
          Construct a new RingBuffer out of the specified part of the given byte array
RingBuffer(java.nio.ByteBuffer buffer, int granularity)
          Construct a new RingBuffer 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 readAvailable()
          Get the number of bytes currently available for reading
 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 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

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.


open

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


readBuffer

private final java.nio.ByteBuffer readBuffer
A view to the source buffer used for reading


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)


writeBuffer

private final java.nio.ByteBuffer writeBuffer
A view to the source buffer used for reading

Constructor Detail

RingBuffer

public RingBuffer(byte[] buffer,
                  int offset,
                  int length,
                  int granularity)
Construct a new RingBuffer out of the specified part of the given byte array

Parameters:
buffer - The byte array that should be used as source of this buffer
offset - Start position inside the given array
length - The length of the buffer to create
granularity - The buffer granularity in bytes

RingBuffer

public RingBuffer(java.nio.ByteBuffer buffer,
                  int granularity)
Construct a new RingBuffer 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

close

public void close()
Close this buffer


flush

public void flush()
Empty this buffer


isOpen

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

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

open

public void open()
Open this buffer


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

readAvailable

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

Returns:
The number of bytes avilable for reading

size

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

Returns:
This buffer´s total size in bytes

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

writeAvailable

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

Returns:
The number of bytes available for writing