Rakesh API

org.rakeshv.io
Class LoggingInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.rakeshv.io.LoggingInputStream
All Implemented Interfaces:
java.io.Closeable

public class LoggingInputStream
extends java.io.InputStream

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

The following code shows a way of using this class.

 import org.rakeshv.io.LoggingInputStream;
 import java.io.BufferedInputStream;
 import java.io.FileInputStream;
 
 public class test
 {
   public static void main( String[] args )
   {
     try
     {
       String logFile = "/tmp/log.txt";
       BufferedInputStream inputStream = new BufferedInputStream(
           new FileInputStream( "/tmp/test.java" ) );
       LoggingInputStream lis = new LoggingInputStream( inputStream,
           logFile );
       byte[] buffer = new byte[ 2048 ];
       int length = 0;
       while ( ( length = lis.read( buffer, 0, buffer.length ) ) != -1 )
       {
       }
       lis.close();
     }
     catch ( Throwable t )
     {
       t.printStackTrace();
     }
   }
 }
 

Version:
$Id: LoggingInputStream.java,v 1.1 2005/09/20 11:59:15 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 outputStream must be flushed prior to closing this input stream.
private  java.io.InputStream inputStream
          The InputStream from which the data that is to be logged will be read.
private  java.io.OutputStream outputStream
          The stream to which the contents read from the stream are to be logged.
 
Constructor Summary
LoggingInputStream(java.io.InputStream inputStream, java.io.File file)
          Create a new instance of the class that decorates the specified InputStream, and the specified file
LoggingInputStream(java.io.InputStream inputStream, java.io.OutputStream outputStream)
          Create a new instance of the class that decorates the specified InputStream, and the specified OutputStream
LoggingInputStream(java.io.InputStream inputStream, java.lang.String fileName)
          Create a new instance of the class that decorates the specified InputStream, and the specified fileName
 
Method Summary
 int available()
          Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
 void close()
          Closes this input stream and releases any system resources associated with the stream.
 void flush()
          Flushes the outputStream.
 boolean getFlush()
          Returns flush.
 java.io.InputStream getInputStream()
          Returns inputStream.
 java.io.OutputStream getOutputStream()
          Returns outputStream.
 void mark(int readlimit)
          Marks the current position in this input stream.
 boolean markSupported()
          Tests if this input stream supports the mark and reset methods.
 int read()
          Reads the next byte of data from the input stream.
 int read(byte[] buffer)
          Reads some number of bytes from the input stream and stores them into the buffer array buffer.
 int read(byte[] buffer, int offset, int length)
          Reads up to length bytes of data from the input stream into an array of bytes.
 void reset()
          Repositions this stream to the position at the time the mark method was last called on this input stream.
protected  void setFlush(boolean flush)
          Set flush.
protected  void setInputStream(java.io.InputStream inputStream)
          Set inputStream.
protected  void setOutputStream(java.io.OutputStream outputStream)
          Set outputStream.
 long skip(long number)
          Skips over and discards number bytes of data from this input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

inputStream

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


outputStream

private java.io.OutputStream outputStream
The stream to which the contents read from the stream are to be logged.


flush

private boolean flush
A flag used to indicate that the outputStream must be flushed prior to closing this input stream.

Constructor Detail

LoggingInputStream

public LoggingInputStream(java.io.InputStream inputStream,
                          java.io.OutputStream outputStream)
Create a new instance of the class that decorates the specified InputStream, and the specified OutputStream

Parameters:
inputStream - The stream from which data will be read. This is the stream that will be decorated.
outputStream - The stream to which the data read is to be logged.

LoggingInputStream

public LoggingInputStream(java.io.InputStream inputStream,
                          java.lang.String fileName)
                   throws java.io.FileNotFoundException
Create a new instance of the class that decorates the specified InputStream, and the specified fileName

Parameters:
inputStream - The stream from which data will be read. This is the stream that will be decorated.
fileName - The fully qualified name of the file to which the data is to be logged.
Throws:
java.io.FileNotFoundException - If the specified file could not be created or accessed.
See Also:
FileUtilities.mkdirs(java.io.File)

LoggingInputStream

public LoggingInputStream(java.io.InputStream inputStream,
                          java.io.File file)
                   throws java.io.FileNotFoundException
Create a new instance of the class that decorates the specified InputStream, and the specified file

Parameters:
inputStream - The stream from which data will be read. This is the stream that will be decorated.
file - The file to which the data is to be logged.
Throws:
java.io.FileNotFoundException - If the specified file could not be created or accessed.
See Also:
FileUtilities.mkdirs(java.io.File)
Method Detail

read

public int read()
         throws java.io.IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255 . If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Specified by:
read in class java.io.InputStream
Returns:
int - The next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - If errors are encountered while reading the byte of data.

read

public int read(byte[] buffer)
         throws java.io.IOException,
                java.lang.NullPointerException
Reads some number of bytes from the input stream and stores them into the buffer array buffer. The method behaves similar to the implementation in the decorated underlying inputStream.

Overrides:
read in class java.io.InputStream
Parameters:
buffer - The buffer into which the data will be read.
Returns:
int - The total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - If errors are encountered while reading the data.
java.lang.NullPointerException - If the buffer is null.
See Also:
read( byte[], int, int )

read

public int read(byte[] buffer,
                int offset,
                int length)
         throws java.io.IOException,
                java.lang.NullPointerException
Reads up to length bytes of data from the input stream into an array of bytes. An attempt is made to read as many as length bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer. This method behaves similar to the implementation in the decorated underlying inputStream.

Overrides:
read in class java.io.InputStream
Parameters:
buffer - The buffer into which the data will be read.
offset - The start offset in array buffer at which the data is written.
length - The maximum number of bytes to read.
Returns:
int - The total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - If errors are encountered while reading the data.
java.lang.NullPointerException - If the buffer is null.

skip

public long skip(long number)
          throws java.io.IOException
Skips over and discards number bytes of data from this input stream. Delegates to the decorated inputStream implementation.

Overrides:
skip in class java.io.InputStream
Parameters:
number - The number of bytes to be skipped.
Returns:
long - The actual number of bytes skipped.
Throws:
java.io.IOException - If errors are encountered while skipping.

available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. The next caller might be the same thread or or another thread.

Overrides:
available in class java.io.InputStream
Returns:
int - The number of bytes that can be read from this input stream without blocking.
Throws:
java.io.IOException - If errors are encountered while interacting with the input stream.

close

public void close()
           throws java.io.IOException
Closes this input stream and releases any system resources associated with the stream. Closes the inputStream that this stream decorates. Also closes the outputStream to which the data is logged. If the flush is true, then the outputStream is flushed prior to closing.

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

mark

public void mark(int readlimit)
Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

Overrides:
mark in class java.io.InputStream
Parameters:
readlimit - The maximum limit of bytes that can be read before the mark position becomes invalid.

reset

public void reset()
           throws java.io.IOException
Repositions this stream to the position at the time the mark method was last called on this input stream.

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException - If this stream has not been marked or if the mark has been invalidated.
See Also:
mark(int)

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods. Whether or not mark and reset are supported is an invariant property of a particular inputStream used to initialise this stream.

Overrides:
markSupported in class java.io.InputStream
Returns:
boolean - Returns true if this stream instance supports the mark(int) and reset() methods; false otherwise.

flush

public void flush()
           throws java.io.IOException
Flushes the outputStream. Invoke this method if the OutputStream 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 OutputStream needs to be flushed.

Throws:
java.io.IOException

getInputStream

public final java.io.InputStream getInputStream()
Returns inputStream.

Returns:
InputStream The value/reference of/to inputStream.

setInputStream

protected final void setInputStream(java.io.InputStream inputStream)
Set inputStream.

Parameters:
inputStream - The value to set.

getOutputStream

public final java.io.OutputStream getOutputStream()
Returns outputStream.

Returns:
OutputStream The value/reference of/to outputStream.

setOutputStream

protected final void setOutputStream(java.io.OutputStream outputStream)
Set outputStream.

Parameters:
outputStream - 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