net.sf.japaki.io
Class RingBuffer

java.lang.Object
  extended by net.sf.japaki.io.RingBuffer
All Implemented Interfaces:
Closeable, ReaderBuffer

public class RingBuffer
extends Object
implements ReaderBuffer

A ring buffer stores the characters read from the input stream in a buffer. The last read characters overwrite the oldest data in the buffer.

Implementation

The buffer stores the last n characters read from the input stream, where n is the buffer's capacity. Example: Capacity is 100, offset is 300 and position 60. The buffer holds characters 260 to 299 in the right side and from 300 to 359 in the left side of the buffer.


Constructor Summary
RingBuffer(Reader in)
          Constructs a new RingBuffer with a default buffer size.
RingBuffer(Reader in, int sz)
          Constructs a new RingBuffer.
 
Method Summary
 void close()
           
 void free(int pos)
          Frees the buffer before the specified position.
 int read(char[] cbuf, int off, int len, int pos)
          Retrieves a sequence of characters as described in read.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RingBuffer

public RingBuffer(Reader in,
                  int sz)
Constructs a new RingBuffer.

Parameters:
in - base reader
sz - size to be allocated for the underlying buffer
Throws:
NullPointerException - if in is null
IllegalArgumentException - if the size is a negative integer

RingBuffer

public RingBuffer(Reader in)
Constructs a new RingBuffer with a default buffer size.

Parameters:
in - base reader
Throws:
NullPointerException - if in is null
Method Detail

read

public int read(char[] cbuf,
                int off,
                int len,
                int pos)
         throws IOException
Description copied from interface: ReaderBuffer
Retrieves a sequence of characters as described in read.

Specified by:
read in interface ReaderBuffer
Parameters:
cbuf - Destination buffer
off - Offset at which to start storing characters
len - Maximum number of characters to read
pos - Position, where to start reading in the stream
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException - If the specified position is not covered by the underlying buffer or if an I/O error occurs

free

public void free(int pos)
Description copied from interface: ReaderBuffer
Frees the buffer before the specified position.

Specified by:
free in interface ReaderBuffer
Parameters:
pos - All characters before this position may no longer be stored in the underlying buffer. Actual behaviour depends on the implementation.

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Throws:
IOException

toString

public String toString()
Overrides:
toString in class Object