org.sadun.util
Class PushbackReader

java.lang.Object
  extended byjava.io.Reader
      extended byjava.io.FilterReader
          extended byorg.sadun.util.PushbackReader

public class PushbackReader
extends java.io.FilterReader

A reader that allows to push back an arbitrary number of characters.

It also offers some commonly used lookahead comparison operations (based on reading/unreading characters). These should really be in a separate class in the future.

Version:
1.1
Author:
Cristiano Sadun

Field Summary
 
Fields inherited from class java.io.FilterReader
in
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
PushbackReader(java.io.Reader r)
          Create a pushback reader over the given source of characters, allowing to push
 
Method Summary
static void main(java.lang.String[] args)
           
 boolean nextEquals(java.lang.String s)
          Looks ahead to check whether the next characters on the stream equal the given String.
 boolean nextEquals(java.lang.String s, boolean unreadIfTrue)
          Looks ahead to check whether the next characters on the stream equal the given String.
 boolean nextEqualsIgnoreCase(java.lang.String s, boolean unreadIfTrue)
          Looks ahead to check whether the next characters on the stream equal the given String (ignoring letter case).
 int read()
          Read the next character.
 java.lang.String readNext(int n)
          Attempt to read the next n characters in the stream, then unread them.
 void unread(int c)
          Unread the given character.
 
Methods inherited from class java.io.FilterReader
close, mark, markSupported, read, ready, reset, skip
 
Methods inherited from class java.io.Reader
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PushbackReader

public PushbackReader(java.io.Reader r)
Create a pushback reader over the given source of characters, allowing to push

Parameters:
r - the source of characters
Method Detail

read

public int read()
         throws java.io.IOException
Read the next character.

Returns:
the next character on the stream, or -1 at EOF
Throws:
java.io.IOException - if there is a problem reading the stream.

unread

public void unread(int c)
            throws java.io.IOException
Unread the given character. The character will be returned as result of the next read() operation.

Parameters:
c - the charater to unread
Throws:
java.io.IOException - if there is a problem unreading the character.

readNext

public java.lang.String readNext(int n)
                          throws java.io.IOException
Attempt to read the next n characters in the stream, then unread them.

Parameters:
n - the number of character to read.
Returns:
a string with the read characters. Note that the string may contain less than n characters if EOF was reached.
Throws:
java.io.IOException - in case of problems reading the stream

nextEquals

public boolean nextEquals(java.lang.String s)
                   throws java.io.IOException
Looks ahead to check whether the next characters on the stream equal the given String. The reader stream position is unchanged.

Parameters:
s - the string to compare
Returns:
true if the stream contains the string at the current position
Throws:
java.io.IOException - in case of problems reading the stream

nextEquals

public boolean nextEquals(java.lang.String s,
                          boolean unreadIfTrue)
                   throws java.io.IOException
Looks ahead to check whether the next characters on the stream equal the given String. The reader stream position is unchanged if there is no match. If a match exists, it is changed or not depending on the value of the unreadIfTrue parameter.

Parameters:
s - the string to compare
unreadIfTrue - if false and the comparison is successful, the stream position will be after the successfully compared string. Otherwise, it will be unchanged.
Returns:
true if the stream contains the string at the current position
Throws:
java.io.IOException - in case of problems reading the stream

nextEqualsIgnoreCase

public boolean nextEqualsIgnoreCase(java.lang.String s,
                                    boolean unreadIfTrue)
                             throws java.io.IOException
Looks ahead to check whether the next characters on the stream equal the given String (ignoring letter case). The reader stream position is unchanged if there is no match. If a match exists, it is changed or not depending on the value of the unreadIfTrue parameter.

Parameters:
s - the string to compare
unreadIfTrue - if false and the comparison is successful, the stream position will be after the successfully compared string. Otherwise, it will be unchanged.
Returns:
true if the stream contains the string at the current position
Throws:
java.io.IOException - in case of problems reading the stream

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception