edu.toronto.cs.util
Class TeeBufferedReader

java.lang.Object
  extended by java.io.Reader
      extended by java.io.BufferedReader
          extended by edu.toronto.cs.util.TeeBufferedReader
All Implemented Interfaces:
java.io.Closeable, java.lang.Readable

public class TeeBufferedReader
extends java.io.BufferedReader

Reads text from a Reader, copying characters read to a Writer.

Inspired by the Unix tee command.


Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
TeeBufferedReader(java.io.Reader source)
          Creates a new TeeBufferedReader instance specifying a source only
TeeBufferedReader(java.io.Reader source, int sz)
          Creates a new TeeBufferedReader instance.
TeeBufferedReader(java.io.Reader source, java.io.Writer copy)
          Creates a new TeeBufferedReader instance specifying source and logFile.
TeeBufferedReader(java.io.Reader source, java.io.Writer copy, java.lang.String prefix)
          Constructs a TeeBufferedReader with the specified Reader, Writer and prefix.
 
Method Summary
 void close()
          Writes the content of outputBuffer to the logFile and closes the underlying reader and writer.
static void main(java.lang.String[] args)
           
 void prefixOn()
          Makes it so that the next character read is copied out with the prefix prepended.
 int read()
          reads a character from the Reader and sends it to be written to the logFile
 java.lang.String readLine()
          reads a line from reader and writes it to the logFile.
 
Methods inherited from class java.io.BufferedReader
mark, markSupported, read, ready, reset, skip
 
Methods inherited from class java.io.Reader
read, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TeeBufferedReader

public TeeBufferedReader(java.io.Reader source,
                         java.io.Writer copy)
Creates a new TeeBufferedReader instance specifying source and logFile.

Parameters:
source - a Reader with the information to be logged
copy - a Writer which creates a log file

TeeBufferedReader

public TeeBufferedReader(java.io.Reader source)
Creates a new TeeBufferedReader instance specifying a source only

Parameters:
source - a Reader with the information to be logged

TeeBufferedReader

public TeeBufferedReader(java.io.Reader source,
                         int sz)
Creates a new TeeBufferedReader instance.

Parameters:
source - a Reader with the information to be logged
x - an int

TeeBufferedReader

public TeeBufferedReader(java.io.Reader source,
                         java.io.Writer copy,
                         java.lang.String prefix)
Constructs a TeeBufferedReader with the specified Reader, Writer and prefix.

Parameters:
source - a Reader with the information to be logged
copy - a Writer which creates a log of all information.
prefix - a String that should be added before each line of CVC output
Method Detail

prefixOn

public void prefixOn()
Makes it so that the next character read is copied out with the prefix prepended.


read

public int read()
         throws java.io.IOException
reads a character from the Reader and sends it to be written to the logFile

Overrides:
read in class java.io.BufferedReader
Returns:
an int a character that was read
Throws:
java.io.IOException - if an error occurs

readLine

public java.lang.String readLine()
                          throws java.io.IOException
reads a line from reader and writes it to the logFile.

Overrides:
readLine in class java.io.BufferedReader
Returns:
a String line that was read
Throws:
java.io.IOException - if an error occurs

close

public void close()
           throws java.io.IOException
Writes the content of outputBuffer to the logFile and closes the underlying reader and writer.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.BufferedReader
Throws:
java.io.IOException - if an I/O error occurs

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception