dd.util
Class DelimReader

java.lang.Object
  extended byjava.io.Reader
      extended bydd.util.DelimReader

public class DelimReader
extends java.io.Reader

Reader that stops at user-defined delimeters. This reader serves as a wrapper for an existing character stream. It simulates the underlying reader, but when the simulated end-of-input marker is encountered, the reader returns end of input without closing the underlying Reader. This allows an input file to be read in chunks. After this reader reaches the marker, the underlying stream will be positioned immediately after that marker.

Example input file:

 This is a text file to be used with DelimReader, it will keep
 reading until the synthetic EOF is reached
 __EOF__
 This text will never be encountered by the DelimReader, but will be
 the next thing read from the underlying Reader.
 

Reader r = new DelimReader(input, "__EOF__");

Author:
Eric Scharff

Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
DelimReader(java.io.Reader in, java.lang.String eofMarker)
          Creates a new reader using the underlying stream and synthetic EOF marker.
 
Method Summary
 void close()
          Close does nothing on this stream.
static void main(java.lang.String[] args)
          Tests the DelimReader.
 int read()
          Reads the next character of input.
 int read(char[] cbuf, int off, int len)
          Reads characters into the buffer provided.
 
Methods inherited from class java.io.Reader
mark, markSupported, read, ready, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DelimReader

public DelimReader(java.io.Reader in,
                   java.lang.String eofMarker)
Creates a new reader using the underlying stream and synthetic EOF marker. The characters will be read from the stream until the String eofMarker is encountered. Once this is encountered, the DelimReader will pretend to be at the end of input.

Parameters:
in - character stream from which to read input
eofMarker - text to trigger the end of input chunk
Method Detail

close

public void close()
Close does nothing on this stream. The underlying stream stays open because it is legitimate to read input from it eventually.


read

public int read()
         throws java.io.IOException
Reads the next character of input. If the stream is pointing to the synthetic end of input marker, this method will return -1 and the underlying stream will be positioned past the eofMarker. Otherwise, this method returns the next character of input.

Returns:
the next character of input, or -1 if the real or synthetic end of input has been reached.
Throws:
java.io.IOException - if an error occurs reading from the underlying stream.

read

public int read(char[] cbuf,
                int off,
                int len)
         throws java.io.IOException
Reads characters into the buffer provided. This is the core method that should be implemented by a reader. This method attempts to fill the buffer provided, but will stop if the real or synthetic end of input marker is reached.

Parameters:
cbuf - buffer into which input should be written
off - offset into the buffer to write data
len - maximum number of bytes to write
Returns:
the number of bytes read, or -1 if the real or synthetic end of input has been reached.
Throws:
java.io.IOException

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Tests the DelimReader. This testing utility takes a file and a delimieter and prints the contents of the file to standard output until the delimeter is reached. It then prints the underlying stream after the delimeter.

Parameters:
args - command line arguments, the first being the file to read, the second being the delimeter
Throws:
java.lang.Exception