|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.Reader
jparse.LineReader
A file reader that can give you a numbered line as a string. This is
useful for printing error messages regarding particular lines in a file.
Note that this implementation is not synchronized.
Concurrent threads should not access a LineReader
object
without synchronizing between themselves.
BUG: You always get a newline at the end of the file, even if
the file doesn't have one. This is a limitation of
BufferedReader
.
BufferedReader
Field Summary | |
private BufferedReader |
buf
The underlying BufferedReader used to fetch lines from the
file |
private int |
cacheLine
The cache line we are currently reading |
private boolean |
closed
An indication of whether this stream has been closed |
private static int |
LINE_CACHE_SIZE
The maximum number of lines of the file to keep in the cache |
private String[] |
lineCache
The line cache |
private int |
lineNumber
The line number (in the file) of the first line in the cache |
private int |
linePos
The position in the first line in the cache to read next |
private int |
markLineNumber
The line number of the current mark |
private int |
markLinePos
The column number of the current mark |
private int |
validLines
The number of valid cache lines |
Fields inherited from class java.io.Reader |
lock |
Constructor Summary | |
(package private) |
LineReader(File file)
Create a new LineReader |
(package private) |
LineReader(FileDescriptor fd)
Create a new LineReader |
private |
LineReader(FileReader reader)
Internal filling of the line cache when a LineReader is
first created |
(package private) |
LineReader(String fileName)
Create a new LineReader |
Method Summary | |
void |
close()
Close the stream. |
(package private) String |
getLine(int lineNum)
Get a particular line from the file |
private void |
lineDone()
Called when a line has been read completely. |
void |
mark(int readAheadLimit)
Mark the present position in the stream. |
boolean |
markSupported()
Tell whether this stream supports the mark() operation,
which it does |
int |
read()
Read a single character. |
int |
read(char[] cbuf,
int off,
int len)
Read characters into a portion of an array. |
boolean |
ready()
Tell whether this stream is ready to be read |
void |
reset()
Reset the stream to the most recent mark. |
Methods inherited from class java.io.Reader |
read, skip |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final int LINE_CACHE_SIZE
private final String[] lineCache
private int validLines
private int lineNumber
private int cacheLine
private int linePos
private final BufferedReader buf
BufferedReader
used to fetch lines from the
file
private boolean closed
private int markLineNumber
private int markLinePos
Constructor Detail |
LineReader(String fileName) throws IOException
LineReader
fileName
- the name of the file to read from
IOException
- if no file with that name can be read or an I/O
error occurs during the initial cache fillLineReader(File file) throws IOException
LineReader
file
- the file to read from
IOException
- if no such file can be read or an I/O error
occurs during the initial cache fillLineReader(FileDescriptor fd) throws IOException
LineReader
fd
- a descriptor for the file to read from
IOException
- if an I/O error occurs during the initial cache
fillprivate LineReader(FileReader reader) throws IOException
LineReader
is
first created
reader
- the FileReader
to use for reading the file
IOException
- if an I/O error occursMethod Detail |
private void lineDone() throws IOException
IOException
- if an I/O error occursString getLine(int lineNum)
lineNum
- the number of the line to fetch
null
if it is not in the
line cachepublic int read() throws IOException
0x00-0xffff
), or -1 if the end of the stream has been
reached
IOException
- if an I/O error occurspublic int read(char[] cbuf, int off, int len) throws IOException
cbuf
- the destination bufferoff
- the offset at which to start storing characterslen
- the maximum number of characters to read
IOException
- if an I/O error occurspublic boolean ready() throws IOException
True
if the next read()
is guaranteed
not to block for input, false
otherwise. Note that
returning false
does not guarantee that the next read will
block.
IOException
- if an I/O error occurspublic boolean markSupported()
mark()
operation,
which it does
true
if mark()
is supported,
false
otherwisepublic void mark(int readAheadLimit) throws IOException
reset()
will attempt to reposition the stream to this
point.
readAheadLimit
- 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.
IllegalArgumentException
- if readAheadLimit is < 0
IOException
- if an I/O error occurspublic void reset() throws IOException
IOException
- if an I/O error occurs, the stream has never
been marked, or the mark has been invalidatedpublic void close() throws IOException
read()
, ready()
, mark()
, or
reset()
invocations will throw an
IOException
. Closing a previously-closed stream, however,
has no effect.
IOException
- if an I/O error occurs
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |