org.cdlib.xtf.util
Class LineReader

Object
  extended by LineReader

public class LineReader
extends Object

Provides a line-based interface for reading a file. Has the unusual ability to read forward or backward.

Author:
Martin Haye

Field Summary
private  byte[] block
          Internal buffer of file data
private  int blockEnd
          Ending position of the block buffer within the file
private static int blockSize
          Size of our internal buffer
private  int blockStart
          Starting position of the block buffer within the file
private  StringBuffer buf
          Used to accumulate lines
private  RandomAccessFile file
          Actual disk file we're reading
private  int length
          Overall length of the file we're reading
private  int linePos
          File position of the start of the last line read
private  int pos
          Current position within the file
 
Constructor Summary
LineReader(String filePath)
          Default constructor
 
Method Summary
 int length()
          Tells how long the file is, in bytes.
 int linePos()
          Retrieves the file position of the last line fetched by nextLine() or prevLine().
private  char nextChar()
          Get the next character in the input file, and increment the position.
 String nextLine()
          Retrieves the next line of text from the file.
private  char prevChar()
          Get the previous character in the input file, and decrement the position.
 String prevLine()
          Retrieves the previous line of text from the file.
private  void readBlock(int startPos)
          Read a block of data starting a the given position.
 void seek(int toPos)
          Reposition the file pointer at the beginning of the line containing the specified byte position.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

blockSize

private static final int blockSize
Size of our internal buffer

See Also:
Constant Field Values

block

private byte[] block
Internal buffer of file data


length

private int length
Overall length of the file we're reading


pos

private int pos
Current position within the file


blockStart

private int blockStart
Starting position of the block buffer within the file


blockEnd

private int blockEnd
Ending position of the block buffer within the file


linePos

private int linePos
File position of the start of the last line read


file

private RandomAccessFile file
Actual disk file we're reading


buf

private StringBuffer buf
Used to accumulate lines

Constructor Detail

LineReader

public LineReader(String filePath)
           throws IOException
Default constructor

Parameters:
filePath - Path of the file to read
Throws:
IOException
Method Detail

readBlock

private void readBlock(int startPos)
                throws IOException
Read a block of data starting a the given position.

Throws:
IOException

length

public final int length()
                 throws IOException
Tells how long the file is, in bytes.

Throws:
IOException

seek

public void seek(int toPos)
          throws IOException
Reposition the file pointer at the beginning of the line containing the specified byte position.

Throws:
IOException

nextChar

private char nextChar()
               throws IOException
Get the next character in the input file, and increment the position.

Throws:
IOException

prevChar

private char prevChar()
               throws IOException
Get the previous character in the input file, and decrement the position.

Throws:
IOException

nextLine

public String nextLine()
                throws IOException
Retrieves the next line of text from the file.

Returns:
The text line, or null if the end of the file has been reached.
Throws:
IOException

prevLine

public String prevLine()
                throws IOException
Retrieves the previous line of text from the file.

Returns:
The text line, or null if the start of the file has been reached.
Throws:
IOException

linePos

public int linePos()
Retrieves the file position of the last line fetched by nextLine() or prevLine().