public class OutputStreamDumper<T extends OutputStream> extends FilterOutputStream
Copies the data that is written to this class to the
OutputStream
passed in the constructor. It also keep track of
the data written to the underlying stream in a byte array that can be read
at any time. This is useful for logging purposes.
WARNING: data written to this OutputStream
are kept in
memory, so this class should be used when the maximum size of the 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 #OutputStreamStreamDumper(sink, maxDumpSize)
.
Usage:
InputStream source=... //some data to be read. OutputStream destination1= new ByteArrayOutputStream(); OutputStreamStreamDumper dumper = new OutputStreamStreamDumper(destination1); org.apache.commons.io.IOUtils.copy(source, dumper); dumper.close(); byte[] data= dumper.getData(); //at this point both destination1 and destination2 contains the same bytes.
Modifier and Type | Field and Description |
---|---|
static long |
INDEFINITE_SIZE
Constant
INDEFINITE_SIZE=-1L |
out
Constructor and Description |
---|
OutputStreamDumper(T sink)
Constructor for OutputStreamDumper.
|
OutputStreamDumper(T sink,
long maxDumpSize)
Constructor for OutputStreamDumper.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
enableDump(boolean enable)
Allow to switch off the copy to the internal byte array.
|
byte[] |
getData()
Returns the data that was written until now to the internal byte array.
|
T |
getWrappedStream()
Returns the wrapped (original)
OutputStream passed in the
constructor. |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
flush, write
public static final long INDEFINITE_SIZE
INDEFINITE_SIZE=-1L
public OutputStreamDumper(T sink)
Constructor for OutputStreamDumper.
sink
- the underlying stream that must be dumped.public OutputStreamDumper(T sink, long maxDumpSize)
Constructor for OutputStreamDumper.
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 FilterOutputStream
IOException
public void enableDump(boolean enable)
Allow to switch off the copy to the internal byte array. The copy is enabled by default.
enable
- a boolean.public final byte[] getData()
Returns the data that was written until now to the internal byte array.
This corresponds to the data written to the internal
OutputStream
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) OutputStream
passed in the
constructor.
OutputStream
passed in the
constructorpublic void write(byte[] b, int off, int len) throws IOException
write
in class FilterOutputStream
IOException
public void write(int b) throws IOException
write
in class FilterOutputStream
IOException
Copyright © 2008–2016. All rights reserved.