public class LoggingInputStream extends InputStream
InputStream to a specified OutputStream.
The following code shows a way of using this class.
import com.sptci.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, Sans Pareil Technologies, Inc.
| Constructor and Description |
|---|
LoggingInputStream(InputStream inputStream,
File file)
Create a new instance of the class that decorates the specified
InputStream, and the specified file |
LoggingInputStream(InputStream inputStream,
OutputStream outputStream)
Create a new instance of the class that decorates the specified
InputStream, and the specified OutputStream
|
LoggingInputStream(InputStream inputStream,
String fileName)
Create a new instance of the class that decorates the specified
InputStream, and the specified fileName
|
| Modifier and Type | Method and Description |
|---|---|
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. |
InputStream |
getInputStream()
Returns
inputStream. |
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(InputStream inputStream)
Set
inputStream. |
protected void |
setOutputStream(OutputStream outputStream)
Set
outputStream. |
long |
skip(long number)
Skips over and discards
number bytes of data from
this input stream. |
public LoggingInputStream(InputStream inputStream, 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(InputStream inputStream, String fileName) throws 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.FileNotFoundException - If the specified file could not be
created or accessed.FileUtilities.mkdirs(java.io.File)public LoggingInputStream(InputStream inputStream, File file) throws FileNotFoundException
InputStream, and the specified fileinputStream - 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.FileNotFoundException - If the specified file could not be
created or accessed.FileUtilities.mkdirs(java.io.File)public int read()
throws 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 InputStream-1 if the
end of the stream is reached.IOException - If errors are encountered while reading the
byte of data.public int read(byte[] buffer)
throws IOException,
NullPointerException
buffer. The method behaves
similar to the implementation in the decorated underlying inputStream.read in class 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.IOException - If errors are encountered while reading the
data.NullPointerException - If the buffer is
null.read( byte[], int, int )public int read(byte[] buffer,
int offset,
int length)
throws IOException,
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 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.IOException - If errors are encountered while reading the
data.NullPointerException - If the buffer is
null.public long skip(long number)
throws IOException
number bytes of data from
this input stream. Delegates to the decorated inputStream implementation.skip in class InputStreamnumber - The number of bytes to be skipped.IOException - If errors are encountered while skipping.public int available()
throws IOException
available in class InputStreamIOException - If errors are encountered while interacting
with the input stream.public void close()
throws 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 Closeableclose in interface AutoCloseableclose in class InputStreamIOException - If errors are encountered while interacting
with the input stream.public void mark(int readlimit)
mark in class InputStreamreadlimit - The maximum limit of bytes that can be read
before the mark position becomes invalid.public void reset()
throws IOException
reset in class InputStreamIOException - 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 InputStreamtrue if this stream
instance supports the mark(int) and reset() methods;
false otherwise.public void flush()
throws 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.IOException - If exceptions are encountered.public final InputStream getInputStream()
inputStream.protected final void setInputStream(InputStream inputStream)
inputStream.inputStream - The value to set.public final OutputStream getOutputStream()
outputStream.protected final void setOutputStream(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.