com.gc.iotools.stream.os.inspection
Class OutputStreamDumper<T extends OutputStream>

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by com.gc.iotools.stream.os.inspection.OutputStreamDumper<T>
All Implemented Interfaces:
Closeable, Flushable

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.
 

Since:
1.2.9
Version:
$Id: OutputStreamDumper.java 463 2011-01-21 23:54:17Z dvd.smnt@gmail.com $
Author:
dvd.smnt

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

INDEFINITE_SIZE

public static final long INDEFINITE_SIZE
Constant INDEFINITE_SIZE=-1L

See Also:
Constant Field Values
Constructor Detail

OutputStreamDumper

public OutputStreamDumper(T sink)

Constructor for OutputStreamDumper.

Parameters:
sink - the underlying stream that must be dumped.

OutputStreamDumper

public OutputStreamDumper(T sink,
                          long maxDumpSize)

Constructor for OutputStreamDumper.

Parameters:
sink - the underlying stream that must be dumped.
maxDumpSize - the maximum size of the dump.
Method Detail

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Overrides:
close in class FilterOutputStream
Throws:
IOException

enableDump

public void enableDump(boolean enable)

Allow to switch off the copy to the internal byte array. The copy is enabled by default.

Parameters:
enable - a boolean.

getData

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)).

Returns:
the data that was written until now to the OutputStream

getWrappedStream

public final T getWrappedStream()

Returns the wrapped (original) OutputStream passed in the constructor.

Returns:
The original OutputStream passed in the constructor

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException

Overrides:
write in class FilterOutputStream
Throws:
IOException

write

public void write(int b)
           throws IOException

Overrides:
write in class FilterOutputStream
Throws:
IOException


Copyright © 2008-2011. All Rights Reserved.