APIdia Launch Partner
Top Description Fields Constructors Methods
java.io

public Class LineNumberReader

extends BufferedReader
Class Inheritance

A buffered character-input stream that keeps track of line numbers. This class defines methods setLineNumber(int) and getLineNumber() for setting and getting the current line number respectively.

By default, line numbering begins at 0. This number increments at every line terminator as the data is read, and at the end of the stream if the last character in the stream is not a line terminator. This number can be changed with a call to setLineNumber(int). Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().

A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed, or any of the previous terminators followed by end of stream, or end of stream not preceded by another terminator.

Author
Mark Reinhold
Since
1.1

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
private static final int
private int
lineNumber

The current line number

private int
markedLineNumber

The line number of the mark, if any

private boolean
markedSkipLF

Hides java.io.BufferedReader.markedSkipLF.

The skipLF flag when the mark was set
private static final int
maxSkipBufferSize

Hides java.io.Reader.maxSkipBufferSize.

Maximum skip-buffer size
private static final int
NONE

Previous character types

private int
prevChar

The previous character type

private char[]
skipBuffer

Hides java.io.Reader.skipBuffer.

Skip buffer, null until allocated
private boolean
skipLF

Hides java.io.BufferedReader.skipLF.

If the next character is a line feed, skip it

Constructor Summary

AccessConstructor and Description
public
LineNumberReader(Reader
A Reader object to provide the underlying stream
in
)

Create a new line-numbering reader, using the default input-buffer size.

public
LineNumberReader(Reader
A Reader object to provide the underlying stream
in
,
int
An int specifying the size of the buffer
sz
)

Create a new line-numbering reader, reading characters into a buffer of the given size.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

The current line number
getLineNumber
()

Get the current line number.

public void
mark(int
Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.
readAheadLimit
)

Overrides java.io.BufferedReader.mark.

Mark the present position in the stream.

public int

Returns:

The character read, or -1 if the end of the stream has been reached
read
()

Overrides java.io.BufferedReader.read.

Read a single character.

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

Overrides java.io.BufferedReader.read.

Reads characters into a portion of an array.

public String

Returns:

A String containing the contents of the line, not including any line termination characters, or null if the end of the stream has been reached
readLine
()

Overrides java.io.BufferedReader.readLine.

Read a line of text.

public void
reset()

Overrides java.io.BufferedReader.reset.

Reset the stream to the most recent mark.

public void
setLineNumber(int
An int specifying the line number
lineNumber
)

Set the current line number.

public long
skip(long n)

Overrides java.io.BufferedReader.skip.

Skips characters.

Inherited from java.io.BufferedReader:
closelinesmarkSupportedreadLineready

Field Detail

CHARback to summary
private static final int CHAR
EOFback to summary
private static final int EOF
EOLback to summary
private static final int EOL
lineNumberback to summary
private int lineNumber

The current line number

markedLineNumberback to summary
private int markedLineNumber

The line number of the mark, if any

markedSkipLFback to summary
private boolean markedSkipLF

Hides java.io.BufferedReader.markedSkipLF.

The skipLF flag when the mark was set

maxSkipBufferSizeback to summary
private static final int maxSkipBufferSize

Hides java.io.Reader.maxSkipBufferSize.

Maximum skip-buffer size

NONEback to summary
private static final int NONE

Previous character types

prevCharback to summary
private int prevChar

The previous character type

skipBufferback to summary
private char[] skipBuffer

Hides java.io.Reader.skipBuffer.

Skip buffer, null until allocated

skipLFback to summary
private boolean skipLF

Hides java.io.BufferedReader.skipLF.

If the next character is a line feed, skip it

Constructor Detail

LineNumberReaderback to summary
public LineNumberReader(Reader in)

Create a new line-numbering reader, using the default input-buffer size.

Parameters
in:Reader

A Reader object to provide the underlying stream

LineNumberReaderback to summary
public LineNumberReader(Reader in, int sz)

Create a new line-numbering reader, reading characters into a buffer of the given size.

Parameters
in:Reader

A Reader object to provide the underlying stream

sz:int

An int specifying the size of the buffer

Method Detail

getLineNumberback to summary
public int getLineNumber()

Get the current line number.

Returns:int

The current line number

See Also
setLineNumber
markback to summary
public void mark(int readAheadLimit) throws IOException

Overrides java.io.BufferedReader.mark.

Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point, and will also reset the line number appropriately.

Parameters
readAheadLimit:int

Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.

Exceptions
IOException:
If an I/O error occurs
readback to summary
public int read() throws IOException

Overrides java.io.BufferedReader.read.

Read a single character. Line terminators are compressed into single newline ('\n') characters. The current line number is incremented whenever a line terminator is read, or when the end of the stream is reached and the last character in the stream is not a line terminator.

Returns:int

The character read, or -1 if the end of the stream has been reached

Annotations
@SuppressWarnings:fallthrough
Exceptions
IOException:
If an I/O error occurs
readback to summary
public int read(char[] cbuf, int off, int len) throws IOException

Overrides java.io.BufferedReader.read.

Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

If len is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.

Line terminators are compressed into single newline ('\n') characters. The current line number is incremented whenever a line terminator is read, or when the end of the stream is reached and the last character in the stream is not a line terminator.

Annotations
@SuppressWarnings:fallthrough
Exceptions
IOException:
If an I/O error occurs
IndexOutOfBoundsException:
If off is negative, or len is negative, or len is greater than cbuf.length - off
readLineback to summary
public String readLine() throws IOException

Overrides java.io.BufferedReader.readLine.

Read a line of text. Line terminators are compressed into single newline ('\n') characters. The current line number is incremented whenever a line terminator is read, or when the end of the stream is reached and the last character in the stream is not a line terminator.

Returns:String

A String containing the contents of the line, not including any line termination characters, or null if the end of the stream has been reached

Exceptions
IOException:
If an I/O error occurs
resetback to summary
public void reset() throws IOException

Overrides java.io.BufferedReader.reset.

Reset the stream to the most recent mark.

Exceptions
IOException:
If the stream has not been marked, or if the mark has been invalidated
setLineNumberback to summary
public void setLineNumber(int lineNumber)

Set the current line number.

Parameters
lineNumber:int

An int specifying the line number

See Also
getLineNumber
skipback to summary
public long skip(long n) throws IOException

Overrides java.io.BufferedReader.skip.

Doc from java.io.BufferedReader.skip.

Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached. If the stream is already at its end before this method is invoked, then no characters are skipped and zero is returned.

Exceptions
IOException:

Doc from java.io.Reader.skip.

If an I/O error occurs