gnu.text

Class LineBufferedReader

public class LineBufferedReader extends 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 intBUFFER_SIZE
Default (initial buffer) size.
protected Readerin
intlimit
The length of the valid (data-containing) part of the buffer.
protected intlineNumber
The current line number (at position of lineStartPos).
protected intmarkPos
The position of the mark (assuming {@code readAheadLinit > 0}).
intpos
The current read position, as an index into buffer.
protected intreadAheadLimit
If mark has been called, and not invalidated, the read ahead limit.
charreadState
Constructor Summary
LineBufferedReader(InputStream in)
LineBufferedReader(Reader in)
Method Summary
voidclose()
intfill(int len)
Called by {@code read()} when it needs its buffer filled.
intgetColumnNumber()
Return the current (zero-based) column number.
booleangetConvertCR()
True if CR and CRLF should be converted to LF.
intgetLineNumber()
Get the current line number.
StringgetName()
PathgetPath()
chargetReadState()
Return a character that indicates what we are currently reading.
voidincrLineNumber(int lineDelta, int lineStartPos)
voidlineStart(boolean revisited)
A hook to allow sub-classes to perform some action at start of line.
voidmark(int readAheadLimit)
booleanmarkSupported()
intpeek()
intread()
intread(char[] cbuf, int off, int len)
voidreadLine(StringBuffer sbuf, char mode)
Read a line.
StringreadLine()
booleanready()
voidreset()
voidsetBuffer(char[] buffer)
Specify a buffer to use for the input buffer.
voidsetConvertCR(boolean convertCR)
voidsetKeepFullLines(boolean keep)
Should we preserve the complete current line?
voidsetLineNumber(int lineNumber)
voidsetName(Object name)
voidsetPath(Path path)
intskip(int n)
Skip forwards or backwards a number of characters.
voidskip()
voidskipRestOfLine()
Skips the rest of the current line, including the line terminator.
voidskip_quick()
Same as skip(), but assumes previous command was a non-EOF peek().
voidunread()
voidunread_quick()
Same as unread, but only allowed after non-EOF-returning read().

Field Detail

buffer

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

BUFFER_SIZE

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

in

protected Reader in

limit

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

lineNumber

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

markPos

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

pos

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

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).

readState

public char readState

Constructor Detail

LineBufferedReader

public LineBufferedReader(InputStream in)

LineBufferedReader

public LineBufferedReader(Reader in)

Method Detail

close

public void close()

fill

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

getColumnNumber

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

getConvertCR

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

getLineNumber

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

getName

public String getName()

getPath

public Path getPath()

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.

incrLineNumber

public void incrLineNumber(int lineDelta, int lineStartPos)

lineStart

public void lineStart(boolean revisited)
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)

mark

public void mark(int readAheadLimit)

markSupported

public boolean markSupported()

peek

public int peek()

read

public int read()

read

public int read(char[] cbuf, int off, int len)

readLine

public void readLine(StringBuffer sbuf, char mode)
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.

readLine

public String readLine()

ready

public boolean ready()

reset

public void reset()

setBuffer

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

setConvertCR

public final void setConvertCR(boolean convertCR)

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.

setLineNumber

public void setLineNumber(int lineNumber)

setName

public void setName(Object name)

setPath

public void setPath(Path path)

skip

public int skip(int n)
Skip forwards or backwards a number of characters.

skip

public void skip()

skipRestOfLine

public void skipRestOfLine()
Skips the rest of the current line, including the line terminator.

skip_quick

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

unread

public void unread()

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'.