SPT Core API

com.sptci.io
Class LoggingReader

java.lang.Object
  extended by java.io.Reader
      extended by com.sptci.io.LoggingReader
All Implemented Interfaces:
Closeable, Readable

public class LoggingReader
extends Reader

A decorator class that logs all the data it reads from the underlying Reader to a specified Writer.

The following code shows a way of using this class.

 import com.sptci.io.LoggingReader;
 import java.io.BufferedReader;
 import java.io.FileReader;

 public class test
 {
   public static void main( String[] args )
   {
     try
     {
       String logFile = "/tmp/log.txt";
       BufferedReader reader = new BufferedReader(
           new LoggingReader( new FileReader( "/tmp/test.java" ),
             logFile ) );
       String line = "";
       while ( ( line = reader.readLine() ) != null )
       {
       }
       reader.close();
     }
     catch ( Throwable t )
     {
       t.printStackTrace();
     }
   }
 }
 

Version:
$Id: LoggingReader.java 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2005-09-19
See Also:

© Copyright 2005, Sans Pareil Technologies, Inc.


Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
LoggingReader(Reader reader, File file)
          Create a new instance of the class that decorates the specified Reader, and the specified file
LoggingReader(Reader reader, String fileName)
          Create a new instance of the class that decorates the specified Reader, and the specified fileName
LoggingReader(Reader reader, Writer writer)
          Create a new instance of the class that decorates the specified Reader, and the specified Writer
 
Method Summary
 void close()
          Close the stream.
 void flush()
          Flushes the writer.
 boolean getFlush()
          Returns flush.
 Reader getReader()
          Returns reader.
 Writer getWriter()
          Returns writer.
 void mark(int readlimit)
          Mark the present position in the stream.
 boolean markSupported()
          Tell whether this stream supports the mark(int) operation.
 int read()
          Read a single character.
 int read(char[] buffer)
          Read characters into an array.
 int read(char[] buffer, int offset, int length)
          Read characters into a portion of an array.
 boolean ready()
          Tell whether this stream is ready to be read.
 void reset()
          Reset the stream.
protected  void setFlush(boolean flush)
          Set flush.
protected  void setReader(Reader reader)
          Set reader.
protected  void setWriter(Writer writer)
          Set writer.
 long skip(long number)
          Skip characters.
 
Methods inherited from class java.io.Reader
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LoggingReader

public LoggingReader(Reader reader,
                     Writer writer)
Create a new instance of the class that decorates the specified Reader, and the specified Writer

Parameters:
reader - The reader from which data will be read. This is the reader that will be decorated.
writer - The writer to which the data read is to be logged.

LoggingReader

public LoggingReader(Reader reader,
                     String fileName)
              throws IOException
Create a new instance of the class that decorates the specified Reader, and the specified fileName

Parameters:
reader - The reader from which data will be read. This is the reader that will be decorated.
fileName - The fully qualified name of the file to which the data is to be logged.
Throws:
IOException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
See Also:
FileUtilities.mkdirs(java.io.File)

LoggingReader

public LoggingReader(Reader reader,
                     File file)
              throws IOException
Create a new instance of the class that decorates the specified Reader, and the specified file

Parameters:
reader - The reader from which data will be read. This is the reader that will be decorated.
file - The file to which the data is to be logged.
Throws:
IOException - If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
See Also:
FileUtilities.mkdirs(java.io.File)
Method Detail

read

public int read()
         throws IOException
Read a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.

Overrides:
read in class Reader
Returns:
int - The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
Throws:
IOException - If errors are encountered while reading the byte of data.

read

public int read(char[] buffer)
         throws IOException
Read characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

Overrides:
read in class Reader
Parameters:
buffer - The buffer into which the characters will be read.
Returns:
int - The number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException - If errors are encountered while reading the data.
See Also:
read( char[], int, int )

read

public int read(char[] buffer,
                int offset,
                int length)
         throws IOException
Read characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

Specified by:
read in class Reader
Parameters:
buffer - The buffer into which the characters will be read.
offset - The start offset in array buffer at which the characters are written.
length - The maximum number of characters to read.
Returns:
int - The number of characters read, or -1 if the end of the stream has been reached
Throws:
IOException - If errors are encountered while reading the data.

skip

public long skip(long number)
          throws IllegalArgumentException,
                 IOException
Skip characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.

Overrides:
skip in class Reader
Parameters:
number - The number of characters to be skipped.
Returns:
long - The actual number of characters skipped.
Throws:
IllegalArgumentException - If number is negative.
IOException - If errors are encountered while skipping.

ready

public boolean ready()
              throws IOException
Tell whether this stream is ready to be read.

Overrides:
ready in class Reader
Returns:
boolean - true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Throws:
IOException - If errors are encountered while interacting with the input stream.

markSupported

public boolean markSupported()
Tell whether this stream supports the mark(int) operation.

Overrides:
markSupported in class Reader
Returns:
boolean - Returns true if and only if this stream supports the mark operation.

mark

public void mark(int readlimit)
          throws IOException
Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point. Not all character-input streams support the mark(int) operation.

Note: Logging will remain true to the read sequence. In other words, marking and resetting the reader will cause duplicate characters to be written to the writer.

Overrides:
mark in class Reader
Parameters:
readlimit - Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.
Throws:
IOException - If the stream does not support mark() , or if some other I/O error occurs

reset

public void reset()
           throws IOException
Reset the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().

Overrides:
reset in class Reader
Throws:
IOException - If the stream has not been marked, or if the mark has been invalidated, or if the stream does not support reset(), or if some other I/O error occurs
See Also:
mark(int)

close

public void close()
           throws IOException
Close the stream. Once a stream has been closed, further read(), ready(), mark(int), or reset() invocations will throw an IOException. Closing a previously-closed stream, however, has no effect. Closes the reader that this stream decorates. Also closes the writer to which the data is logged. If the flush is true, then the writer is flushed prior to closing.

Specified by:
close in interface Closeable
Specified by:
close in class Reader
Throws:
IOException - If errors are encountered while interacting with the input stream.

flush

public void flush()
           throws IOException
Flushes the writer. Invoke this method if the Writer you used to initialise this stream requires to be flushed prior to a close invocation. You must invoke this method prior to invoking the close() method if the Writer needs to be flushed.

Throws:
IOException - If errors are encountered.

getReader

public final Reader getReader()
Returns reader.

Returns:
Reader The value/reference of/to reader.

setReader

protected final void setReader(Reader reader)
Set reader.

Parameters:
reader - The value to set.

getWriter

public final Writer getWriter()
Returns writer.

Returns:
Writer The value/reference of/to writer.

setWriter

protected final void setWriter(Writer writer)
Set writer.

Parameters:
writer - The value to set.

getFlush

public final boolean getFlush()
Returns flush.

Returns:
boolean The value/reference of/to flush.

setFlush

protected final void setFlush(boolean flush)
Set flush.

Parameters:
flush - The value to set.

SPT Core API