gnu.text
Class LineBufferedReader

java.lang.Object
  extended by java.io.Reader
      extended by gnu.text.LineBufferedReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable
Direct Known Subclasses:
InPort, LineInputStreamReader

public class LineBufferedReader
extends java.io.Reader

A LineNumberReader with some extra features: You can seek backwards to the start of the line preceding the current position (or the mark, if that has been set). You can use seek with a negative offset, or unread. You can also use peek to look at the next character without moving. The method getColumnNumber gives you the current column. Provides a method that is called at the start of a line. This is especially useful for interactive streams (e.g. prompting). It would be nice if we could inherit from LineNumberReader. That may be possible in theory, but it is difficult and expensive (because we don't get access to BufferedReader's buffer).


Field Summary
 char[] buffer
          The input buffer, containing the current line etc.
static int BUFFER_SIZE
          Default (initial buffer) size.
protected  java.io.Reader in
           
 int limit
          The length of the valid (data-containing) part of the buffer.
protected  int lineNumber
          The current line number (at position of lineStartPos).
protected  int markPos
          The position of the mark (assuming readAheadLinit > 0).
 int pos
          The current read position, as an index into buffer.
protected  int readAheadLimit
          If mark has been called, and not invalidated, the read ahead limit.
 char readState
           
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
LineBufferedReader(java.io.InputStream in)
           
LineBufferedReader(java.io.Reader in)
           
 
Method Summary
 void close()
           
 int fill(int len)
          Called by read() when it needs its buffer filled.
 int getColumnNumber()
          Return the current (zero-based) column number.
 boolean getConvertCR()
          True if CR and CRLF should be converted to LF.
 int getLineNumber()
          Get the current line number.
 java.lang.String getName()
           
 Path getPath()
           
 char getReadState()
          Return a character that indicates what we are currently reading.
 void incrLineNumber(int lineDelta, int lineStartPos)
           
 void lineStart(boolean revisited)
          A hook to allow sub-classes to perform some action at start of line.
 void mark(int readAheadLimit)
           
 boolean markSupported()
           
 int peek()
           
 int read()
           
 int read(char[] cbuf, int off, int len)
           
 java.lang.String readLine()
           
 void readLine(java.lang.StringBuffer sbuf, char mode)
          Read a line.
 boolean ready()
           
 void reset()
           
 void setBuffer(char[] buffer)
          Specify a buffer to use for the input buffer.
 void setConvertCR(boolean convertCR)
           
 void setKeepFullLines(boolean keep)
          Should we preserve the complete current line? The default is true, but in some cases there can be a performance improvement if we don't need to keep a long line when filling the buffer.
 void setLineNumber(int lineNumber)
           
 void setName(java.lang.Object name)
           
 void setPath(Path path)
           
 void skip_quick()
          Same as skip(), but assumes previous command was a non-EOF peek().
 void skip()
           
 int skip(int n)
          Skip forwards or backwards a number of characters.
 void skipRestOfLine()
          Skips the rest of the current line, including the line terminator.
 void unread_quick()
          Same as unread, but only allowed after non-EOF-returning read().
 void unread()
           
 
Methods inherited from class java.io.Reader
read, read, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

protected java.io.Reader in

BUFFER_SIZE

public static final int BUFFER_SIZE
Default (initial buffer) size.

See Also:
Constant Field Values

buffer

public char[] buffer
The input buffer, containing the current line etc.


pos

public int pos
The current read position, as an index into buffer.


limit

public int limit
The length of the valid (data-containing) part of the buffer.


readState

public char readState

lineNumber

protected int lineNumber
The current line number (at position of lineStartPos).


readAheadLimit

protected int readAheadLimit
If mark has been called, and not invalidated, the read ahead limit. Zero if mark has not been called, or had been invalidated (due to either calling reset or excessive reading ahead).


markPos

protected int markPos
The position of the mark (assuming readAheadLinit > 0). (Garbage if readAheadLimit <= 0).

Constructor Detail

LineBufferedReader

public LineBufferedReader(java.io.InputStream in)

LineBufferedReader

public LineBufferedReader(java.io.Reader in)
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

getReadState

public char getReadState()
Return a character that indicates what we are currently reading. Returns '\n' if we are not inside read; '\"' if reading a string; '|' if inside a comment; '(' if inside a list; and ' ' if otherwise inside a read.


setKeepFullLines

public void setKeepFullLines(boolean keep)
Should we preserve the complete current line? The default is true, but in some cases there can be a performance improvement if we don't need to keep a long line when filling the buffer.


getConvertCR

public final boolean getConvertCR()
True if CR and CRLF should be converted to LF.


setConvertCR

public final void setConvertCR(boolean convertCR)

lineStart

public void lineStart(boolean revisited)
               throws java.io.IOException
A hook to allow sub-classes to perform some action at start of line. Called just before the first character of the new line is read.

Parameters:
revisited - true if we have read here before (i.e. we did a reset of unread() to get here)
Throws:
java.io.IOException

fill

public int fill(int len)
         throws java.io.IOException
Called by read() when it needs its buffer filled. Read characters into buffer, starting at off, for len. Can assume that len > 0. Only called if pos>=limit. Return -1 if EOF, otherwise number of read chars. This can be usefully overridden by sub-classes.

Throws:
java.io.IOException

setBuffer

public void setBuffer(char[] buffer)
               throws java.io.IOException
Specify a buffer to use for the input buffer.

Throws:
java.io.IOException

read

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

read

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

getPath

public Path getPath()

setPath

public void setPath(Path path)

getName

public java.lang.String getName()

setName

public void setName(java.lang.Object name)

getLineNumber

public int getLineNumber()
Get the current line number. The "first" line is number number 0.


setLineNumber

public void setLineNumber(int lineNumber)

incrLineNumber

public void incrLineNumber(int lineDelta,
                           int lineStartPos)

getColumnNumber

public int getColumnNumber()
Return the current (zero-based) column number.


markSupported

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

mark

public void mark(int readAheadLimit)
Overrides:
mark in class java.io.Reader

reset

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

readLine

public void readLine(java.lang.StringBuffer sbuf,
                     char mode)
              throws java.io.IOException
Read a line. If mode is 'I' ("ignore") ignore delimiters. If mode is 'P' ("peek") leave delimiter in input stream. If mode is 'A' ("append") append delimiter to result.

Throws:
java.io.IOException

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Throws:
java.io.IOException

skip

public int skip(int n)
         throws java.io.IOException
Skip forwards or backwards a number of characters.

Throws:
java.io.IOException

ready

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

skip_quick

public final void skip_quick()
                      throws java.io.IOException
Same as skip(), but assumes previous command was a non-EOF peek().

Throws:
java.io.IOException

skip

public void skip()
          throws java.io.IOException
Throws:
java.io.IOException

skipRestOfLine

public void skipRestOfLine()
                    throws java.io.IOException
Skips the rest of the current line, including the line terminator.

Throws:
java.io.IOException

unread

public void unread()
            throws java.io.IOException
Throws:
java.io.IOException

unread_quick

public void unread_quick()
Same as unread, but only allowed after non-EOF-returning read(). Also allowed after an intervening peek(), but only if the read() did not return '\r' or '\n'.


peek

public int peek()
         throws java.io.IOException
Throws:
java.io.IOException