it.unimi.dsi.mg4j.io
Class FastBufferedReader

java.lang.Object
  extended byjava.io.Reader
      extended byit.unimi.dsi.mg4j.io.FastBufferedReader

public class FastBufferedReader
extends Reader

Lightweight, unsynchronised, aligned reader buffering class.

This class provides buffering for readers, but it does so with purposes and an internal logic that are radically different from the ones adopted in BufferedReader.

There is no support for marking. All methods are unsychronised. Moreover, it is guaranteed that all reads performed by this class will be multiples of the given buffer size. If, for instance, you use the default buffer size, reads will be performed on the underlying input stream in multiples of 16384 bytes. This is very important on operating systems that optimise disk reads on disk block boundaries.


Field Summary
protected  int avail
          The number of buffer bytes available starting from pos.
protected  char[] buffer
          The internal buffer.
static int DEFAULT_BUFFER_SIZE
          The default size of the internal buffer in bytes (16Ki).
protected  int pos
          The current position in the buffer.
protected  Reader r
          The underlying reader.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
FastBufferedReader(Reader r)
          Creates a new fast buffered reader by wrapping a given reader with a buffer of DEFAULT_BUFFER_SIZE characters.
FastBufferedReader(Reader r, int bufSize)
          Creates a new fast buffered reder by wrapping a given reder with a given buffer size.
 
Method Summary
 void close()
           
 int read()
           
 int read(char[] b, int offset, int length)
           
 MutableString readLine(MutableString s)
          Reads a line into the given mutable string.
 int readWord(MutableString s)
          Reads a word into the given mutable string.
 long skip(long n)
           
 
Methods inherited from class java.io.Reader
mark, markSupported, read, ready, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default size of the internal buffer in bytes (16Ki).

See Also:
Constant Field Values

buffer

protected char[] buffer
The internal buffer.


pos

protected int pos
The current position in the buffer.


avail

protected int avail
The number of buffer bytes available starting from pos.


r

protected Reader r
The underlying reader.

Constructor Detail

FastBufferedReader

public FastBufferedReader(Reader r,
                          int bufSize)
Creates a new fast buffered reder by wrapping a given reder with a given buffer size.

Parameters:
r - a reader to wrap.
bufSize - the size in bytes of the internal buffer.

FastBufferedReader

public FastBufferedReader(Reader r)
Creates a new fast buffered reader by wrapping a given reader with a buffer of DEFAULT_BUFFER_SIZE characters.

Parameters:
r - a reader to wrap.
Method Detail

read

public int read()
         throws IOException
Throws:
IOException

read

public int read(char[] b,
                int offset,
                int length)
         throws IOException
Throws:
IOException

readLine

public MutableString readLine(MutableString s)
                       throws IOException
Reads a line into the given mutable string.

The next line of input (defined as in BufferedReader.readLine()) will be stored into s. Note that if s is not loose this method will be quite inefficient.

Parameters:
s - a mutable string that will be used to store the next line (which could be empty).
Returns:
s or null if the end of file was found.
Throws:
IOException

readWord

public int readWord(MutableString s)
             throws IOException
Reads a word into the given mutable string.

The next word of input (defined as a maximal sequence of characters satisfying Character.isLetterOrDigit(char)) will be stored into s, and the delimiting character will be returned. Note that if s is not loose this method will be quite inefficient.

Parameters:
s - a mutable string that will be used to store the next word (which will be empty if the next character is not a letter or a digit).
Returns:
the character that delimited the current word, or -1 if the word was delimited by an end of file.
Throws:
IOException

skip

public long skip(long n)
          throws IOException
Throws:
IOException

close

public void close()
           throws IOException
Throws:
IOException