org.millscript.tools
Class PushbackLinenumberReader

java.lang.Object
  extended by java.io.Reader
      extended by org.millscript.tools.PushbackLinenumberReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable

public class PushbackLinenumberReader
extends java.io.Reader

A buffered character stream reader that allows characters to be pushed back and keeps track of line numbers. By default we start line numbers at 1, rather than 0 as is Java.

Due to the implementation, there may be more available push back space than the value specified. However, you are guaranteed to have at least the requested amount of push back space.

This is heavily based on Java's LineNumberReader, PushbackReader and BufferedReader. However PushbackReader fails when you push back an end of file character(-1) as it simply pushes -1 into a char array! Also, although you could construct a PushbackReader on a LineNumberReader, or vice versa, if you push back an end of line you would have a hard time obtaining the correct line number.


Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
PushbackLinenumberReader(java.io.Reader in)
           
PushbackLinenumberReader(java.io.Reader in, int sz, int pb)
          Constructs a new push back line number reader, which will buffer the specified number of characters and have a push back buffer of the given minimum size.
 
Method Summary
 void close()
           
 int getLineNumber()
          Returns the line number for the current position in the file.
 void mark(int readAheadLimit)
           
 boolean markSupported()
           
 int read()
          Reads a single character, returning it as an int.
 int read(char[] cbuf, int off, int len)
          Read characters into a portion of an array.
 java.lang.String readLine()
          Reads a line of text.
 boolean ready()
           
 void reset()
           
 void setLineNumber(int n)
          Sets the current line number
 long skip(long n)
          Skips the requested number of characters.
 void unread(char[] cbuf)
          Pushes back all the characters in specified buffer.
 void unread(char[] cbuf, int off, int len)
          Pushes back a specified number of characters from the specified buffer, starting from the specified offset.
 void unread(int ch)
          Pushes back the specified character.
 
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
 

Constructor Detail

PushbackLinenumberReader

public PushbackLinenumberReader(java.io.Reader in)
Parameters:
in - a Reader to fetch characters from

PushbackLinenumberReader

public PushbackLinenumberReader(java.io.Reader in,
                                int sz,
                                int pb)
Constructs a new push back line number reader, which will buffer the specified number of characters and have a push back buffer of the given minimum size.

Parameters:
in - a Reader to fetch characters from
sz - the number of characters to buffer
pb - the minimum number of push back characters
Method Detail

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Reader
Throws:
java.io.IOException
See Also:
Reader.close()

getLineNumber

public int getLineNumber()
Returns the line number for the current position in the file. i.e. if you've just read a end of line character, the line number will have been incremented. If you've just read a block of characters the line number may have been incremented multiple times.

Returns:
the current line number

mark

public void mark(int readAheadLimit)
          throws java.io.IOException
Overrides:
mark in class java.io.Reader
Throws:
java.io.IOException
See Also:
Reader.mark(int)

markSupported

public boolean markSupported()
Overrides:
markSupported in class java.io.Reader
See Also:
Reader.markSupported()

read

public int read()
         throws java.io.IOException
Reads a single character, returning it as an int. Line terminators are compressed into a single line feed('\n') character. A line is terminated by a line feed('\n'), a carriage return('\r') or a carriage return immediately followed by a line feed('\r\n').

Overrides:
read in class java.io.Reader
Returns:
an int containing the next character code
Throws:
java.io.IOException - if there is a problem reading from the source reader

read

public int read(char[] cbuf,
                int off,
                int len)
         throws java.io.IOException
Read characters into a portion of an array. This method compresses line endings in the same way as the read method.

Specified by:
read in class java.io.Reader
Parameters:
cbuf - the character buffer to read characters into
off - the position in the character buffer to start placing characters at
len - the maximum number of characters to read
Returns:
the number of characters read
Throws:
java.io.IOException - thrown if there is a problem reading a character from the parent reader

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Reads a line of text. A line is terminated by a line feed('\n'), a carriage return('\r') or a carriage return immediately followed by a line feed.

Returns:
a String holding the line of text read or null if the end of file has been reached.
Throws:
java.io.IOException - if an I/O exception occurs

ready

public boolean ready()
              throws java.io.IOException
Overrides:
ready in class java.io.Reader
Throws:
java.io.IOException
See Also:
Reader.ready()

reset

public void reset()
           throws java.io.IOException
Overrides:
reset in class java.io.Reader
Throws:
java.io.IOException
See Also:
Reader.reset()

setLineNumber

public void setLineNumber(int n)
Sets the current line number

Parameters:
n - an int specifying the new current line number

skip

public long skip(long n)
          throws java.io.IOException
Skips the requested number of characters. This method does not compress line endings like the read method.

Overrides:
skip in class java.io.Reader
Parameters:
n - the number of characters to skip
Returns:
the number of characters skipped
Throws:
java.io.IOException - thrown if there is a problem reading from the parent reader

unread

public void unread(int ch)
            throws java.io.IOException
Pushes back the specified character.

Parameters:
ch - the character to push back. This can include any value returned by read, including the end of line.
Throws:
java.io.IOException - thrown if there is a problem reading from the parent reader

unread

public void unread(char[] cbuf)
            throws java.io.IOException
Pushes back all the characters in specified buffer.

Parameters:
cbuf - the array of characters to unread
Throws:
java.io.IOException - thrown if there is a problem reading from the parent reader

unread

public void unread(char[] cbuf,
                   int off,
                   int len)
            throws java.io.IOException
Pushes back a specified number of characters from the specified buffer, starting from the specified offset.

Parameters:
cbuf - the array of characters to unread
off - the position in the buffer to unread from
len - the number of characters to unread
Throws:
java.io.IOException - thrown if there is a problem reading from the parent reader


Copyright © 2001-2007 Open World Ltd. All Rights Reserved.