|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.io.OutputStream
java.io.FilterOutputStream
com.gc.iotools.stream.os.inspection.OutputStreamDumper<T>
public class OutputStreamDumper<T extends OutputStream>
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.
| Field Summary | |
|---|---|
static long |
INDEFINITE_SIZE
Constant INDEFINITE_SIZE=-1L |
| Fields inherited from class java.io.FilterOutputStream |
|---|
out |
| Constructor Summary | |
|---|---|
OutputStreamDumper(T sink)
Constructor for OutputStreamDumper. |
|
OutputStreamDumper(T sink,
long maxDumpSize)
Constructor for OutputStreamDumper. |
|
| Method Summary | |
|---|---|
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)
|
| Methods inherited from class java.io.FilterOutputStream |
|---|
flush, write |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final long INDEFINITE_SIZE
INDEFINITE_SIZE=-1L
| Constructor Detail |
|---|
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.| Method Detail |
|---|
public void close()
throws IOException
close in interface Closeableclose in class FilterOutputStreamIOExceptionpublic 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
constructor
public void write(byte[] b,
int off,
int len)
throws IOException
write in class FilterOutputStreamIOException
public void write(int b)
throws IOException
write in class FilterOutputStreamIOException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||