AsciiDatabase
Class AsciiReader

java.lang.Object
  |
  +--AsciiDatabase.AsciiReader

public final class AsciiReader
extends Object

A fully buffered, random access reader capable of reading both fixed sized blocks and carrage return terminated lines. Also, can read from either normal uncompressed files, or GZIP compressed files.

Author:
chris.studholme@utoronto.ca

Field Summary
private  byte[] buffer
          Buffer used to improve performance of small reads.
static int buffersize
          Default buffer size for uncompressed files.
private  File file
          Open file.
private  RandomAccessFile stream
          Stream used for uncompressed file.
private  long streampos
          Current stream position.
private  int validlength
          Length of valid data in buffer.
private  int validoffset
          Offset of next valid data in buffer.
private  GZIPInputStream zstream
          Stream used for compressed file.
static int zstreambuffer
          Default buffer size for compressed files.
 
Constructor Summary
AsciiReader(String filename)
          Open an uncompressed file by name.
AsciiReader(String filename, boolean gzip)
          Open an uncompressed or compressed file by name.
 
Method Summary
 void close()
          Close the data file.
protected  void finalize()
          Close the data file.
 long getFilePointer()
          Return the current file pointer position.
 long length()
          Return the length of the file.
 void pushBack()
          Push back the last byte read so it can be read again.
 int read()
          Read a byte from the file.
 int read(byte[] b)
          Read data from the file.
 int read(byte[] b, int off, int len)
          Read data from the file.
 String readLine()
          Read a single line of an ascii file.
 int readToEOL(byte[] b)
          Read until the end of line is reached.
 int readToEOL(byte[] b, int off, int len)
          Read until the end of line is reached.
 void seek(long off)
          Seek to an arbitrary offset within the file.
 
Methods inherited from class java.lang.Object
, clone, equals, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

buffersize

public static final int buffersize
Default buffer size for uncompressed files.

zstreambuffer

public static final int zstreambuffer
Default buffer size for compressed files.

file

private File file
Open file.

stream

private RandomAccessFile stream
Stream used for uncompressed file.

zstream

private GZIPInputStream zstream
Stream used for compressed file.

buffer

private byte[] buffer
Buffer used to improve performance of small reads.

validoffset

private int validoffset
Offset of next valid data in buffer.

validlength

private int validlength
Length of valid data in buffer.

streampos

private long streampos
Current stream position.
Constructor Detail

AsciiReader

public AsciiReader(String filename)
            throws IOException
Open an uncompressed file by name.
Parameters:
filename - name of file to open
Throws:
IOException - if an error occurs openning the file

AsciiReader

public AsciiReader(String filename,
                   boolean gzip)
            throws IOException
Open an uncompressed or compressed file by name.
Parameters:
filename - name of file to open
gzip - flag to indicate if the file is GZIP compressed
Throws:
IOException - if an error occurs openning the file
Method Detail

close

public void close()
Close the data file.

finalize

protected void finalize()
Close the data file. Just calls close().
Overrides:
finalize in class Object

pushBack

public void pushBack()
              throws IOException
Push back the last byte read so it can be read again. Pushing back more that one byte is possible but is unreliable (may result in an exception).
Throws:
IOException - if the byte cannot be pushed back

read

public final int read()
               throws IOException
Read a byte from the file.
Returns:
byte read
Throws:
IOException - if an error occurs reading the file

readToEOL

public int readToEOL(byte[] b,
                     int off,
                     int len)
              throws IOException
Read until the end of line is reached. End of line is denoted by one of: '\r', '\n', or '\r\n'. If the end of line is reached, the buffer will contain one of '\r' or '\n' but not '\r\n'.
Parameters:
b - buffer where data should be placed
off - offset within buffer where data should go
len - maximum number of to be placed in buffer
Returns:
number of bytes actually read
Throws:
IOException - if an error occurs reading the file

readToEOL

public final int readToEOL(byte[] b)
                    throws IOException
Read until the end of line is reached. End of line is denoted by one of: '\r', '\n', or '\r\n'. If the end of line is reached, the buffer will contain one of '\r' or '\n' but not '\r\n'. The maximum number of characters read is b.length.
Parameters:
b - buffer where data should be placed
Returns:
number of bytes actually read
Throws:
IOException - if an error occurs reading the file

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Read data from the file. If the size of the read is equal to or greater than buffersize, the buffer is not used and the data is read directly into b.
Parameters:
b - buffer where data should be placed
off - offset within buffer where data should go
len - maximum number of to be placed in buffer
Returns:
number of bytes read
Throws:
IOException - if an error occurs reading the file

read

public final int read(byte[] b)
               throws IOException
Read data from the file. If the size of the read is equal to or greater than buffersize, the buffer is not used and the data is read directly into b.
Parameters:
b - buffer where data should be placed
Returns:
number of bytes read
Throws:
IOException - if an error occurs reading the file

getFilePointer

public long getFilePointer()
                    throws IOException
Return the current file pointer position.
Returns:
current file position
Throws:
IOException - if an error occurs

length

public long length()
Return the length of the file. For compressed files, this method returns the compressed length. There is no way to discover the uncompressed length of a compressed file without reading (and uncompressing) the whole file.
Returns:
length of the file

seek

public void seek(long off)
          throws IOException
Seek to an arbitrary offset within the file. This method works correctly even for compressed files; however, it may be quite slow when seeking within a compressed file. For example, when seeking backwards within a compressed file, the file must be closed, reopened, and data read until the desired location is reached.
Parameters:
off - offset to seek to
Throws:
IOException - if an error occurs

readLine

public String readLine()
                throws IOException
Read a single line of an ascii file. A line is terminated with one of: '/r', '/n', or '/r/n'. The terminating character(s) are not appended to the returned string. This method is not as efficient as readToEOL().
Returns:
single ascii line as a String without the end of line characters
Throws:
IOException - if an error occurs openning the file