Rakesh API

org.rakeshv.io
Class LoggingReader

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

public class LoggingReader
extends java.io.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 org.rakeshv.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,v 1.2 2005/09/20 13:15:11 rakesh Exp $
Author:
Rakesh Vidyadharan 19th September 2005
See Also:

© Copyright 2005, Rakesh Vidyadharan


Field Summary
private  boolean flush
          A flag used to indicate that the writer must be flushed prior to closing this reader.
private  java.io.Reader reader
          The Reader from which the data that is to be logged will be read.
private  java.io.Writer writer
          The writer to which the contents read from the reader are to be logged.
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
LoggingReader(java.io.Reader reader, java.io.File file)
          Create a new instance of the class that decorates the specified Reader, and the specified file
LoggingReader(java.io.Reader reader, java.lang.String fileName)
          Create a new instance of the class that decorates the specified Reader, and the specified fileName
LoggingReader(java.io.Reader reader, java.io.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.
 java.io.Reader getReader()
          Returns reader.
 java.io.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(java.io.Reader reader)
          Set reader.
protected  void setWriter(java.io.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
 

Field Detail

reader

private java.io.Reader reader
The Reader from which the data that is to be logged will be read.


writer

private java.io.Writer writer
The writer to which the contents read from the reader are to be logged.


flush

private boolean flush
A flag used to indicate that the writer must be flushed prior to closing this reader.

Constructor Detail

LoggingReader

public LoggingReader(java.io.Reader reader,
                     java.io.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(java.io.Reader reader,
                     java.lang.String fileName)
              throws java.io.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:
java.io.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(java.io.Reader reader,
                     java.io.File file)
              throws java.io.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:
java.io.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 java.io.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 java.io.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:
java.io.IOException - If errors are encountered while reading the byte of data.

read

public int read(char[] buffer)
         throws java.io.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 java.io.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:
java.io.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 java.io.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 java.io.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:
java.io.IOException - If errors are encountered while reading the data.

skip

public long skip(long number)
          throws java.lang.IllegalArgumentException,
                 java.io.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 java.io.Reader
Parameters:
number - The number of characters to be skipped.
Returns:
long - The actual number of characters skipped.
Throws:
java.lang.IllegalArgumentException - If number is negative.
java.io.IOException - If errors are encountered while skipping.

ready

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

Overrides:
ready in class java.io.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:
java.io.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 java.io.Reader
Returns:
boolean - Returns true if and only if this stream supports the mark operation.

mark

public void mark(int readlimit)
          throws java.io.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 java.io.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:
java.io.IOException - If the stream does not support mark() , or if some other I/O error occurs

reset

public void reset()
           throws java.io.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 java.io.Reader
Throws:
java.io.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 java.io.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 java.io.Closeable
Specified by:
close in class java.io.Reader
Throws:
java.io.IOException - If errors are encountered while interacting with the input stream.

flush

public void flush()
           throws java.io.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:
java.io.IOException

getReader

public final java.io.Reader getReader()
Returns reader.

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

setReader

protected final void setReader(java.io.Reader reader)
Set reader.

Parameters:
reader - The value to set.

getWriter

public final java.io.Writer getWriter()
Returns writer.

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

setWriter

protected final void setWriter(java.io.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.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan