public class LoggingReader extends Reader
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();
}
}
}
© Copyright 2005, Sans Pareil Technologies, Inc.
| Constructor and Description |
|---|
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
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
public LoggingReader(Reader reader, Writer writer)
Reader, and the specified Writer
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.public LoggingReader(Reader reader, String fileName) throws IOException
Reader, and the specified fileName
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.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 reasonFileUtilities.mkdirs(java.io.File)public LoggingReader(Reader reader, File file) throws IOException
Reader, and the specified filereader - 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.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 reasonFileUtilities.mkdirs(java.io.File)public int read()
throws IOException
read in class Reader0 to 65535 (0x00-0xffff), or
-1 if the end of the stream has been reachedIOException - If errors are encountered while reading the
byte of data.public int read(char[] buffer)
throws IOException
read in class Readerbuffer - The buffer into which the characters will be read.-1
if the end of the stream has been reachedIOException - If errors are encountered while reading the
data.read( char[], int, int )public int read(char[] buffer,
int offset,
int length)
throws IOException
read in class Readerbuffer - 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.-1
if the end of the stream has been reachedIOException - If errors are encountered while reading the
data.public long skip(long number)
throws IllegalArgumentException,
IOException
skip in class Readernumber - The number of characters to be skipped.IllegalArgumentException - If number is
negative.IOException - If errors are encountered while skipping.public boolean ready()
throws IOException
ready in class Readertrue 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.IOException - If errors are encountered while interacting
with the input stream.public boolean markSupported()
mark(int) operation.markSupported in class Readertrue if and only if this
stream supports the mark operation.public void mark(int readlimit)
throws IOException
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.
mark in class Readerreadlimit - 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.IOException - If the stream does not support mark()
, or if some other I/O error occurspublic void reset()
throws IOException
reset()
operation, and some support reset() without
supporting mark().reset in class ReaderIOException - 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 occursmark(int)public void close()
throws IOException
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.close in interface Closeableclose in interface AutoCloseableclose in class ReaderIOException - If errors are encountered while interacting
with the input stream.public void flush()
throws IOException
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.IOException - If errors are encountered.public final Reader getReader()
reader.protected final void setReader(Reader reader)
reader.reader - The value to set.public final Writer getWriter()
writer.protected final void setWriter(Writer writer)
writer.writer - The value to set.public final boolean getFlush()
flush.protected final void setFlush(boolean flush)
flush.flush - The value to set.