de.unibi.techfak.jpredictor.sequences
Class ResetableReader

java.lang.Object
  extended by java.io.Reader
      extended by de.unibi.techfak.jpredictor.sequences.ResetableReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable

public class ResetableReader
extends java.io.Reader

This class is designed for only one purpose: to make sure, mark and reset function on a stream. To fulfill this task, you cannot construct this class other than give a filename, which is used to reopen the file if necessary. All attempts to reset and mark are redirected to the underlying BufferedReader and only if it fails, the class' own handling takes over.


Field Summary
protected  long countReadBytes
          The number of actual read bytes from the underlying reader, set by read(char[], int, int), skip(int) and read().
private  java.lang.String filename
          The name of the file this reader is working with.
protected  long mark
          The mark on the underlying Reader, set by mark(int) and used by reset() to read ahead to this position.
private  java.io.Reader reader
          The file reader all reads are done through.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
ResetableReader(java.lang.String filename)
          Constructs a new ResetableReader object by opening the file with new BufferedReader(new FileReader( filename ))
 
Method Summary
 void close()
          Closes this Reader.
 void mark(int readAheadLimit)
          Sets a mark on this stream.
 boolean markSupported()
          This class supports mark and reset on the stream by reopening the underlying BufferedReader if necessary.
 int read()
          Reads a character from the file through the underlying BufferedReader.
 int read(char[] buffer, int offset, int length)
          Reads characters from the file through the underlying BufferedReader and writes them into the buffer.
 boolean ready()
          Tells whether this stream is ready to be read.
 void reset()
           Resets this reader.
 long skip(long n)
          Skip characters.
 
Methods inherited from class java.io.Reader
read, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

filename

private java.lang.String filename
The name of the file this reader is working with.


reader

private java.io.Reader reader
The file reader all reads are done through.


countReadBytes

protected long countReadBytes
The number of actual read bytes from the underlying reader, set by read(char[], int, int), skip(int) and read().


mark

protected long mark
The mark on the underlying Reader, set by mark(int) and used by reset() to read ahead to this position.

Constructor Detail

ResetableReader

public ResetableReader(java.lang.String filename)
                throws java.io.FileNotFoundException,
                       java.lang.NullPointerException
Constructs a new ResetableReader object by opening the file with new BufferedReader(new FileReader( filename ))

Parameters:
filename - The filename of the file to open.
Throws:
java.io.FileNotFoundException - If the file does not exists.
java.lang.NullPointerException - If filename is null.
Method Detail

read

public int read()
         throws java.io.IOException
Reads a character from the file through the underlying BufferedReader.

Overrides:
read in class java.io.Reader
Returns:
The next valid character, -1 in case of EOF
Throws:
java.io.IOException - From the underlying BufferedReader.
See Also:
BufferedReader.read()

read

public int read(char[] buffer,
                int offset,
                int length)
         throws java.io.IOException
Reads characters from the file through the underlying BufferedReader and writes them into the buffer.

Specified by:
read in class java.io.Reader
Parameters:
buffer - The buffer to write to.
offset - The start of the buffer.
length - Number of characters to read maximal.
Returns:
The number of valid characters written into the buffer. -1 is returned if no character could be read due to a EOF.
Throws:
java.io.IOException - From the underlying BufferedReader.

close

public void close()
           throws java.io.IOException
Closes this Reader.

Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Reader
Throws:
java.io.IOException - From the underlying BufferedReader.

markSupported

public boolean markSupported()
This class supports mark and reset on the stream by reopening the underlying BufferedReader if necessary.

Overrides:
markSupported in class java.io.Reader
Returns:
true.

mark

public void mark(int readAheadLimit)
          throws java.io.IOException
Sets a mark on this stream. After performing a reset() it is garantied, that the next readable character is the one a read would return now.
The parameter is forwarded to the underlying BufferedReader , but if it is less than 64k, 64k is taken.

Overrides:
mark in class java.io.Reader
Parameters:
readAheadLimit - The number of characters to read until the mark on the stream expires.
Throws:
java.io.IOException - Never.

reset

public void reset()
           throws java.io.IOException

Resets this reader. The attempt to reset this reader is redirected to the underlying BufferedReader. But, since BufferedReader will always reset to the same mark and is unable to reset to the beginning of the file, the redirection policy holds only in case the mark on the reader is not zero. Thus, if the reset redirection fails or if the mark is zero the file is reopened and, if neccessary, read to the mark.

Afterwards, the mark on this Reader is set back to zero (BufferedReader would not do so and this is exactly the reason why it will always reset to the same mark once set). An attempt to reset this reader without a previously mark will reset it to the beginning, which means that the file is reopened. With all this in mind it is clear, that two subsequent calls to reset() will always set the reader to the beginning.

Overrides:
reset in class java.io.Reader
Throws:
java.io.IOException - If the file could not be reopened, or if the reopened stream could not be read to the mark.

skip

public long skip(long n)
          throws java.io.IOException
Skip characters. This call is redirected to the underlying BufferReader.

Overrides:
skip in class java.io.Reader
Parameters:
n - The number of characters to skip
Returns:
The number of characters actually skipped
Throws:
java.lang.IllegalArgumentException - If n is negative.
java.io.IOException - If an I/O error occurs.

ready

public boolean ready()
              throws java.io.IOException
Tells whether this stream is ready to be read. This call is simple forwarded to the underlying BufferedReader

Overrides:
ready in class java.io.Reader
Returns:
true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Throws:
java.io.IOException - If an I/O error occurs