jparse
Class LineReader

java.lang.Object
  extended byjava.io.Reader
      extended byjparse.LineReader

final class LineReader
extends Reader

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.

Author:
Jerry James
See Also:
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

LINE_CACHE_SIZE

private static final int LINE_CACHE_SIZE
The maximum number of lines of the file to keep in the cache

See Also:
Constant Field Values

lineCache

private final String[] lineCache
The line cache


validLines

private int validLines
The number of valid cache lines


lineNumber

private int lineNumber
The line number (in the file) of the first line in the cache


cacheLine

private int cacheLine
The cache line we are currently reading


linePos

private int linePos
The position in the first line in the cache to read next


buf

private final BufferedReader buf
The underlying BufferedReader used to fetch lines from the file


closed

private boolean closed
An indication of whether this stream has been closed


markLineNumber

private int markLineNumber
The line number of the current mark


markLinePos

private int markLinePos
The column number of the current mark

Constructor Detail

LineReader

LineReader(String fileName)
     throws IOException
Create a new LineReader

Parameters:
fileName - the name of the file to read from
Throws:
IOException - if no file with that name can be read or an I/O error occurs during the initial cache fill

LineReader

LineReader(File file)
     throws IOException
Create a new LineReader

Parameters:
file - the file to read from
Throws:
IOException - if no such file can be read or an I/O error occurs during the initial cache fill

LineReader

LineReader(FileDescriptor fd)
     throws IOException
Create a new LineReader

Parameters:
fd - a descriptor for the file to read from
Throws:
IOException - if an I/O error occurs during the initial cache fill

LineReader

private LineReader(FileReader reader)
            throws IOException
Internal filling of the line cache when a LineReader is first created

Parameters:
reader - the FileReader to use for reading the file
Throws:
IOException - if an I/O error occurs
Method Detail

lineDone

private void lineDone()
               throws IOException
Called when a line has been read completely. Either advance the pointer in the cache, or read in a new line if we have exhausted the cache.

Throws:
IOException - if an I/O error occurs

getLine

String getLine(int lineNum)
Get a particular line from the file

Parameters:
lineNum - the number of the line to fetch
Returns:
the line, as a string, or null if it is not in the line cache

read

public int read()
         throws IOException
Read a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.

Returns:
the character read, as an integer in the range 0 to 16383 (0x00-0xffff), or -1 if the end of the stream has been reached
Throws:
IOException - if an I/O error occurs

read

public int read(char[] cbuf,
                int off,
                int len)
         throws IOException
Read 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.

Parameters:
cbuf - the destination buffer
off - the offset at which to start storing characters
len - the maximum number of characters to read
Returns:
the number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException - if an I/O error occurs

ready

public boolean ready()
              throws IOException
Tell whether this stream is ready to be read

Returns:
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.
Throws:
IOException - if an I/O error occurs

markSupported

public boolean markSupported()
Tell whether this stream supports the mark() operation, which it does

Returns:
true if mark() is supported, false otherwise

mark

public void mark(int readAheadLimit)
          throws IOException
Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.

Parameters:
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.
Throws:
IllegalArgumentException - if readAheadLimit is < 0
IOException - if an I/O error occurs

reset

public void reset()
           throws IOException
Reset the stream to the most recent mark.

Throws:
IOException - if an I/O error occurs, the stream has never been marked, or the mark has been invalidated

close

public void close()
           throws IOException
Close the stream. Once a stream has been closed, further read(), ready(), mark(), or reset() invocations will throw an IOException. Closing a previously-closed stream, however, has no effect.

Throws:
IOException - if an I/O error occurs