|
Rakesh API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.io.InputStream
org.rakeshv.io.LoggingInputStream
public class LoggingInputStream
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();
}
}
}
© 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 |
|---|
private java.io.InputStream inputStream
InputStream from which the data that is to be
logged will be read.
private java.io.OutputStream outputStream
private boolean flush
outputStream must be
flushed prior to closing this input stream.
| Constructor Detail |
|---|
public LoggingInputStream(java.io.InputStream inputStream,
java.io.OutputStream outputStream)
InputStream, and the specified OutputStream
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.
public LoggingInputStream(java.io.InputStream inputStream,
java.lang.String fileName)
throws java.io.FileNotFoundException
InputStream, and the specified fileName
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.
java.io.FileNotFoundException - If the specified file could not be
created or accessed.FileUtilities.mkdirs(java.io.File)
public LoggingInputStream(java.io.InputStream inputStream,
java.io.File file)
throws java.io.FileNotFoundException
InputStream, and the specified file
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.
java.io.FileNotFoundException - If the specified file could not be
created or accessed.FileUtilities.mkdirs(java.io.File)| Method Detail |
|---|
public int read()
throws java.io.IOException
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.
read in class java.io.InputStream-1 if the
end of the stream is reached.
java.io.IOException - If errors are encountered while reading the
byte of data.
public int read(byte[] buffer)
throws java.io.IOException,
java.lang.NullPointerException
buffer. The method behaves
similar to the implementation in the decorated underlying inputStream.
read in class java.io.InputStreambuffer - The buffer into which the data will be read.
-1 is there is no more data because the end of the
stream has been reached.
java.io.IOException - If errors are encountered while reading the
data.
java.lang.NullPointerException - If the buffer is
null.read( byte[], int, int )
public int read(byte[] buffer,
int offset,
int length)
throws java.io.IOException,
java.lang.NullPointerException
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.
read in class java.io.InputStreambuffer - 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.
-1 is there is no more data because the end of the
stream has been reached.
java.io.IOException - If errors are encountered while reading the
data.
java.lang.NullPointerException - If the buffer is
null.
public long skip(long number)
throws java.io.IOException
number bytes of data from
this input stream. Delegates to the decorated inputStream implementation.
skip in class java.io.InputStreamnumber - The number of bytes to be skipped.
java.io.IOException - If errors are encountered while skipping.
public int available()
throws java.io.IOException
available in class java.io.InputStreamjava.io.IOException - If errors are encountered while interacting
with the input stream.
public void close()
throws java.io.IOException
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.
close in interface java.io.Closeableclose in class java.io.InputStreamjava.io.IOException - If errors are encountered while interacting
with the input stream.public void mark(int readlimit)
mark in class java.io.InputStreamreadlimit - The maximum limit of bytes that can be read
before the mark position becomes invalid.
public void reset()
throws java.io.IOException
reset in class java.io.InputStreamjava.io.IOException - If this stream has not been marked or if the
mark has been invalidated.mark(int)public boolean markSupported()
inputStream used to initialise
this stream.
markSupported in class java.io.InputStreamtrue if this stream
instance supports the mark(int) and reset() methods;
false otherwise.
public void flush()
throws java.io.IOException
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.
java.io.IOExceptionpublic final java.io.InputStream getInputStream()
inputStream.
protected final void setInputStream(java.io.InputStream inputStream)
inputStream.
inputStream - The value to set.public final java.io.OutputStream getOutputStream()
outputStream.
protected final void setOutputStream(java.io.OutputStream outputStream)
outputStream.
outputStream - The value to set.public final boolean getFlush()
flush.
protected final void setFlush(boolean flush)
flush.
flush - The value to set.
|
Rakesh API | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||