info.aduna.io
Class LookAheadReader

java.lang.Object
  extended by java.io.Reader
      extended by info.aduna.io.LookAheadReader
All Implemented Interfaces:
Closeable, Readable

public class LookAheadReader
extends Reader

A character-stream reader that allows look-aheads to be performed. The reader does not look ahead any more than necesarry, so performing a look ahead of 5 characters means that max. 5 characters will be read into the look ahead buffer. The look ahead buffer grows as needed.

Note that this implementation is not synchronized.

FIXME: implementation does too many array copies. Using a 'sliding window' in the buffer is cheaper.


Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
LookAheadReader(Reader r)
          Creates a new LookAheadReader with a initial buffer size of 8 characters.
LookAheadReader(Reader r, int bufSize)
          Creates a new LookAheadReader.
 
Method Summary
 int charAt(int lookAhead)
          Get the character at the supplied lookAhead.
 void close()
           
 int fillLABuf(int charsNeeded)
          Tries to fill the look ahead buffer so that it contains 'charsNeeded' characters.
 boolean moreInput()
          Checks whether there's more input to read.
 boolean nextInputIgnoreCaseIs(String s)
          Check whether the next input is equal to the supplied string (case-insensitive).
 boolean nextInputIs(String s)
          Check whether the next input is equal to the supplied string (case-sensitive).
 int read()
           
 int read(char[] cbuf, int off, int len)
           
 long skip(long n)
           
 
Methods inherited from class java.io.Reader
mark, markSupported, read, read, ready, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LookAheadReader

public LookAheadReader(Reader r,
                       int bufSize)
Creates a new LookAheadReader.

Parameters:
r - The reader to do the look ahead on
bufSize - The initial size of the look ahead buffer. Minimum size is 1 (needed for 'moreInput()').

LookAheadReader

public LookAheadReader(Reader r)
Creates a new LookAheadReader with a initial buffer size of 8 characters.

Parameters:
r - The reader to do the look ahead on
Method Detail

nextInputIs

public boolean nextInputIs(String s)
                    throws IOException
Check whether the next input is equal to the supplied string (case-sensitive).

Parameters:
s - The string to compare to the input
Returns:
true if the input is equal to the supplied string, false otherwise
Throws:
IOException - If an I/O error occurred in the current Reader or if the string is longer than the maximum look ahead.

nextInputIgnoreCaseIs

public boolean nextInputIgnoreCaseIs(String s)
                              throws IOException
Check whether the next input is equal to the supplied string (case-insensitive).

Parameters:
s - The string to compare to the input
Returns:
true if the input is equal to the supplied string, false otherwise
Throws:
IOException - If an I/O error occurred in the current Reader or if the string is longer than the maximum look ahead.

charAt

public int charAt(int lookAhead)
           throws IOException
Get the character at the supplied lookAhead. charAt(0) returns the first character to be read.

Returns:
the character at the supplied lookAhead, or -1 if the lookAhead is after the last character in the stream.
Throws:
IOException

moreInput

public boolean moreInput()
                  throws IOException
Checks whether there's more input to read.

Throws:
IOException - If an I/O error occurred in the current Reader

fillLABuf

public int fillLABuf(int charsNeeded)
              throws IOException
Tries to fill the look ahead buffer so that it contains 'charsNeeded' characters. This will succeed, except when the end of the stream has been reached.

Returns:
The number of characters in the buffer
Throws:
IOException

read

public int read()
         throws IOException
Overrides:
read in class Reader
Throws:
IOException

read

public int read(char[] cbuf,
                int off,
                int len)
         throws IOException
Specified by:
read in class Reader
Throws:
IOException

skip

public long skip(long n)
          throws IOException
Overrides:
skip in class Reader
Throws:
IOException

close

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


Copyright © 2011 Aduna. All Rights Reserved.