com.gc.iotools.stream.writer.inspection
Class WriterDumper<T extends Writer>

java.lang.Object
  extended by java.io.Writer
      extended by java.io.FilterWriter
          extended by com.gc.iotools.stream.writer.inspection.WriterDumper<T>
All Implemented Interfaces:
Closeable, Flushable, Appendable

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.
 

Since:
1.2.9
Version:
$Id: WriterDumper.java 527 2014-02-24 19:29:50Z gabriele.contini@gmail.com $
Author:
dvd.smnt

Field Summary
static long INDEFINITE_SIZE
          Constant INDEFINITE_SIZE=-1L
 
Fields inherited from class java.io.FilterWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
WriterDumper(T sink)
           Constructor for WriterDumper.
WriterDumper(T sink, long maxDumpSize)
           Constructor for WriterDumper.
 
Method Summary
 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)
          
 
Methods inherited from class java.io.FilterWriter
flush, write
 
Methods inherited from class java.io.Writer
append, append, append, write, 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

WriterDumper

public WriterDumper(T sink)

Constructor for WriterDumper. Completely record the stream for an indefinite size into memory.

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

WriterDumper

public WriterDumper(T sink,
                    long maxDumpSize)

Constructor for WriterDumper.

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 FilterWriter
Throws:
IOException

enableDump

public void enableDump(boolean enable)

Allow to switch off the copy to the internal character buffer. The copy is enabled by default.

Parameters:
enable - a boolean.

getData

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

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

getWrappedStream

public final T getWrappedStream()

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

Returns:
The original Writer passed in the constructor

write

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

Overrides:
write in class FilterWriter
Throws:
IOException

write

public void write(int b)
           throws IOException

Overrides:
write in class FilterWriter
Throws:
IOException


Copyright © 2008–2014. All rights reserved.