org.inria.ns.reflex.util
Class ReaderScanner

java.lang.Object
  extended by org.inria.ns.reflex.util.Scanner
      extended by org.inria.ns.reflex.util.ReaderScanner
Direct Known Subclasses:
TrackableReaderScanner

public class ReaderScanner
extends Scanner

A scanner for stream of characters.

Note: if you intend to scan strings, you are not compelled to use StringReader, use the specific scanner instead.

Author:
Philippe Poulard
See Also:
StringScanner

Constructor Summary
ReaderScanner(Reader reader)
          Create a new scanner.
 
Method Summary
 void cancel()
          Cancel the characters read since the last marked position (the next read will start from the last marked position).
 void consume()
          Consume the characters read so far.
 Reader getRemainder()
          Return the remainder to read from the current position.
 String getRemainderString()
          Return the remainder to read from the current position.
 void mark(int limit)
          Mark the present position in the stream.
 void read()
          Read the next character.
 
Methods inherited from class org.inria.ns.reflex.util.Scanner
getPosition, getSourceIndex, hasNext, hasNextChar, hasNextChar, hasNextString, lookAhead, nextChar, nextChar, nextNumber, nextNumber, nextObject, nextString, nextString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReaderScanner

public ReaderScanner(Reader reader)
              throws IOException
Create a new scanner.

Parameters:
reader - The input to read.

*** Must support marks. ***

Throws:
IOException - When an I/O error occur.
IllegalArgumentException - When the reader doesn't support marks.
See Also:
ExtensibleBufferedReader
Method Detail

read

public void read()
          throws IOException
Read the next character.

Specified by:
read in class Scanner
Throws:
IOException - When an I/O error occur.
See Also:
#hasNextChar(char), Scanner.nextChar(), Scanner.lookAhead()

mark

public void mark(int limit)
          throws IOException
Mark the present position in the stream.

This method can be called safely several times.

For each mark set, there should be sooner or later a consume or cancel.

Some convenient methods are available for getting a single character without using a mark : Scanner.lookAhead(), Scanner.nextChar(), Scanner.hasNextChar(char, boolean) and Scanner.hasNextChar(String, boolean).

Specified by:
mark in class Scanner
Parameters:
limit - The maximum number of characters that can be read before loosing the mark.
Throws:
IOException - When an I/O error occur.
See Also:
Reader.mark(int), cancel(), consume()

cancel

public void cancel()
            throws IOException
Cancel the characters read since the last marked position (the next read will start from the last marked position).

Specified by:
cancel in class Scanner
Throws:
IOException - When an I/O error occur.
IllegalStateException - When this method is called whereas no position was marked so far.
See Also:
mark(int)

consume

public void consume()
             throws IOException
Consume the characters read so far.

This implies that the last marked position is removed and the next read goes on from the current position. If there wasn't other marker, it will be impossible to go back. If there was at least another one marker, it can be itself cancelled or consumed independently.

Specified by:
consume in class Scanner
Throws:
IOException - When an I/O error occur.
IllegalStateException - When this method is called whereas no position was marked so far.
See Also:
mark(int)

getRemainder

public Reader getRemainder()
Return the remainder to read from the current position.

Specified by:
getRemainder in class Scanner
Returns:
The remainder to read, or null if the end was reached.
See Also:
Scanner.getRemainderString()

getRemainderString

public String getRemainderString()
                          throws IOException
Return the remainder to read from the current position.

Specified by:
getRemainderString in class Scanner
Returns:
The remainder to read, or null if the end was reached.
Throws:
IOException - When an I/O error occur.
See Also:
Scanner.getRemainder()