ikrs.io
Class CircularFIFO

java.lang.Object
  extended by ikrs.io.CircularFIFO

public class CircularFIFO
extends java.lang.Object

The CircularFIFO implements an ... ehm ... yes, a circular FIFO buffer.


Constructor Summary
CircularFIFO(int capacity)
          Creates a new circular FIFO buffer.
 
Method Summary
 int capacity()
          Get the whole capacity for this buffer.
 void clear()
          This method clears the buffer's data.
 boolean isEmpty()
          Checks whether this buffer is empty.
 boolean isFull()
          Checks whether this buffer is full.
 int length()
          Get the number of bytes currently stored inside this buffer.
static void main(java.lang.String[] argv)
           
 byte peek(int offset)
          Get the byte at the given buffer offset (relative).
 byte read()
          Read a single byte from this buffer (if available).
 int read(byte[] buf, int offset, int readLength)
          This method reads up to 'readLength' bytes from this buffer and stores them inside the given 'buf' array.
 java.lang.String toString()
           
 java.lang.StringBuffer toString(java.lang.StringBuffer b)
           
 void write(byte b)
          This method writes the given byte to this buffer.
 int write(byte[] buf, int offset, int writeLength)
          This method writes up tp 'writeLength' bytes bytes to this buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CircularFIFO

public CircularFIFO(int capacity)
             throws java.lang.IllegalArgumentException
Creates a new circular FIFO buffer.

Parameters:
capacity - The buffer's capacity (means: up to 'size' bytes can be stored in this buffer).
Throws:
java.lang.IllegalArgumentException
Method Detail

capacity

public int capacity()
Get the whole capacity for this buffer. The capacity is the max. amount of bytes that can be stored inside this buffer.

Returns:
The max. number of bytes that can be stored inside this buffer.

length

public int length()
Get the number of bytes currently stored inside this buffer.

Returns:
The number of bytes currently stored inside tis buffer.

isEmpty

public boolean isEmpty()
Checks whether this buffer is empty. A buffer is empty if its length equals 0 (zero). No more read operations are allowed in this stats until at least one byte was written. Otherwise the read() methods will raise BufferUnderflowExceptions.

Returns:
A boolean value indicating if the buffer is empty.

isFull

public boolean isFull()
Checks whether this buffer is full. A buffer is full if its length equals its capacity. No more write operations are allowed in this stats until at least one byte was read. Otherwise the write() methods will raise BufferOverflowExceptions.

Returns:
A boolean value indicating if the buffer is full.

read

public byte read()
          throws java.nio.BufferUnderflowException
Read a single byte from this buffer (if available).

Returns:
The next byte from the FIFO queue.
Throws:
java.nio.BufferUnderflowException - If this buffer is empty [length()==0].

read

public int read(byte[] buf,
                int offset,
                int readLength)
This method reads up to 'readLength' bytes from this buffer and stores them inside the given 'buf' array. If 'readLength' exceeds the addressable space inside the 'buf' array (beginning at 'offset'). The method stops reading and returns the actual number of bytes that were read.

Parameters:
buf - The byte buffer to read from.
offset - The read offset for the input array.
readLength - The max. number of bytes to read from the input array.
Returns:
The actual number of bytes that were read.

write

public void write(byte b)
           throws java.nio.BufferOverflowException
This method writes the given byte to this buffer. If the buffer is full the method will raise a BufferOverflowException.

Parameters:
b - The byte to write.
Throws:
java.nio.BufferOverflowException - If the buffer is already full and the byte cannot be written.

write

public int write(byte[] buf,
                 int offset,
                 int writeLength)
This method writes up tp 'writeLength' bytes bytes to this buffer. If 'writeLength' exceeds the addressable space inside 'buf' (beginning at 'offset') the method stops writing and return the actual number of bytes written.

Returns:
The actual number of bytes written to this buffer.

clear

public void clear()
This method clears the buffer's data. After calling this method getLength() will return 0.


peek

public byte peek(int offset)
          throws java.lang.IndexOutOfBoundsException
Get the byte at the given buffer offset (relative). This is not really FIFO style, but might be helpful.

Parameters:
offset - The byte offset (0 <= offset < this.length()).
Returns:
The byte value at the given offset.
Throws:
java.lang.IndexOutOfBoundsException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public java.lang.StringBuffer toString(java.lang.StringBuffer b)

main

public static void main(java.lang.String[] argv)