public class WriterDumper<T extends Writer> extends FilterWriter
This class act as a filter, simply forwarding the calls to the
Writer
passed in the constructor. Doing so it also keeps track
of the data written to the underlying stream. This is useful for logging
purposes.
WARNING: data written to this Writer
are kept in
memory, so this class should be used when the maximum size of the character
data is known in advance, and it is "small" compared to memory size. In
case this is not possible this class should be instantiated limiting the
data that can be dumped #WriterDumper(sink, maxDumpSize)
.
Usage:
Reader source=... //some data to be read. Writer destination1= new StringWriter(); WriterDumper dumper = new WriterDumper(destination1); org.apache.commons.io.IOUtils.copy(source, dumper); dumper.close(); String data= dumper.getData(); //at this point "data" and destination1.toString() contains the same string.
Modifier and Type | Field and Description |
---|---|
static long |
INDEFINITE_SIZE
Constant
INDEFINITE_SIZE=-1L |
out
Constructor and Description |
---|
WriterDumper(T sink)
Constructor for WriterDumper.
|
WriterDumper(T sink,
long maxDumpSize)
Constructor for WriterDumper.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
enableDump(boolean enable)
Allow to switch off the copy to the internal character buffer.
|
String |
getData()
Returns the data that was written until now to the internal character
buffer.
|
T |
getWrappedStream()
Returns the wrapped (original)
Writer passed in the
constructor. |
void |
write(char[] b,
int off,
int len) |
void |
write(int b) |
flush, write
public static final long INDEFINITE_SIZE
INDEFINITE_SIZE=-1L
public WriterDumper(T sink)
Constructor for WriterDumper. Completely record the stream for an indefinite size into memory.
sink
- the underlying stream that must be dumped.public WriterDumper(T sink, long maxDumpSize)
Constructor for WriterDumper.
sink
- the underlying stream that must be dumped.maxDumpSize
- the maximum size of the dump.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class FilterWriter
IOException
public void enableDump(boolean enable)
Allow to switch off the copy to the internal character buffer. The copy is enabled by default.
enable
- a boolean.public final String getData()
Returns the data that was written until now to the internal character
buffer. This corresponds to the data written to the internal
Writer
passed in the constructor if
maxDumpSize
was not reach and data dump was not disabled
(calling enableDump(false)
).
public final T getWrappedStream()
Returns the wrapped (original) Writer
passed in the
constructor.
Writer
passed in the constructorpublic void write(char[] b, int off, int len) throws IOException
write
in class FilterWriter
IOException
public void write(int b) throws IOException
write
in class FilterWriter
IOException
Copyright © 2008–2016. All rights reserved.