bcds.tools
Class DataFileReader

java.lang.Object
  extended by bcds.tools.DataFileReader
Direct Known Subclasses:
TrafficReader

public class DataFileReader
extends java.lang.Object

Implements a line oriented file reader. It can read both text files and GZIP files, which are decompressed on the fly, transparently to the caller. The following tranformations are performed on the input:

Internally, the input is always read through an instace of java.io.LineNumberReader.

NOTE: It would have been good for this class to extend java.io.Reader, but doing that would make implementing the method rewind() impossible: there is no guarantee that reset works (especially on a user-provided Reader object) and reopening the file (as it is done now) would imply assigning ourselves a new instance, which is impossible.


Constructor Summary
DataFileReader(java.io.Reader ips)
          Creates an instace for reading from ips with a default buffer size of 32 KB.
DataFileReader(java.io.Reader ips, int buff_sz)
          Creates an instace for reading from ips with the given buffer size.
DataFileReader(java.lang.String fname)
          Creates an instance for reading the file named fname.
 
Method Summary
 java.lang.String getFileName()
          Returns the value passed to the constructor that accepts a filename parameter, or null if anoother constructor was used.
 int getLineNum()
          Returns the line number of the last line read.
 java.io.Reader getReader()
          Returns the Reader instance used by this object.
static java.io.Reader newFileReader(java.lang.String fname)
          Returns a new Reader instance.
 java.lang.String nextLine()
          Returns the next available line, after performing the transformations on the input.
 void rewind()
          Repositions the file pointer to the beginning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataFileReader

public DataFileReader(java.lang.String fname)
Creates an instance for reading the file named fname. The internal reader object is creted through newFileReader(String).

Throws:
AnyException - as indicated in newFileReader(String).

DataFileReader

public DataFileReader(java.io.Reader ips)
Creates an instace for reading from ips with a default buffer size of 32 KB.


DataFileReader

public DataFileReader(java.io.Reader ips,
                      int buff_sz)
Creates an instace for reading from ips with the given buffer size.

Method Detail

newFileReader

public static java.io.Reader newFileReader(java.lang.String fname)
Returns a new Reader instance. If fname is the single charcter "-", an instance of java.io.InputStreamReader bound to System.in is returned.

If fname seems to be a GZIP file (ends in ".gz", ignoring case), it is opened with java.util.zip.GZIPInputReader. If that fails, a message is sent to System.err indicating that an attempt fill be made to open the file as a "plain" file.

If the file ends up being treated as "plain", an instance of java.io.FileReader is returned.

Throws:
AnyException - wrapping an IOException if any IO error occurs (including FileNotFOundException).

rewind

public void rewind()
Repositions the file pointer to the beginning. This only works when reading from a real file. If this object was instantiated for reading from stdin (filename was "-"), an IOException is thrown. If it was instantiated with a real filename, that file is reopened. If it was instantiated with a Reader object, Reader.reset() is tried on it and an IOException is thrown if that fails.

Note that the Reader object used internally is exposed (see getReader()), so that the caller can use Reader.mark(int) to change the target of a reset.


getReader

public java.io.Reader getReader()
Returns the Reader instance used by this object. This is always an instance of java.io.LineNumberReader which wraps the user-provided Reader object if the instantiation was performed without a filename. When reading from a file, the reference returned is valid only until the next rewind, as a new reader instance is created on each call.


getLineNum

public int getLineNum()
Returns the line number of the last line read.


getFileName

public java.lang.String getFileName()
Returns the value passed to the constructor that accepts a filename parameter, or null if anoother constructor was used.


nextLine

public java.lang.String nextLine()
Returns the next available line, after performing the transformations on the input. If no more lines are available, returns null.

The line counter is incremented.

Any IOException is turned into an AnyException. This is deliberate, so that the caller does not need to put try..catch everywhere.