com.gc.iotools.stream.writer
Class TeeWriter

java.lang.Object
  extended by java.io.Writer
      extended by com.gc.iotools.stream.writer.TeeWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class TeeWriter
extends Writer

Copies the data that is written to this class to the Writer(s) passed in the constructor. It also collect statistics on the operations done (time spent writing to the internal writers, amount of data written).

Usage:

         InputStream source=... //some data to be read.
   StringWriter destination1= new StringWriter();
   StringWriter destination2= new StringWriter();

   TeeWriter tee =  new TeeWriter(destination1,destination2);
   org.apache.commons.io.IOUtils.copy(source,tee);
   tee.close();
   //at this point both destination1 and destination2 contains the same characters.
 

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

Field Summary
protected  boolean closeCalled
          True when close() is invoked.
protected  Writer[] destinations
          The destination Writer(s) where data is written.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
TeeWriter(Writer... destinations)
           Creates a TeeWriter and saves its arguments, the destinations for later use.
 
Method Summary
 void close()
          
 void enableCopy(boolean enable)
           Allow to switch off the copy to the underlying streams.
 void enableCopy(boolean[] enable)
           Allow to switch off the copy to the underlying streams, selectively enabling or disabling copy on some specific stream.
 void flush()
          
 long getSize()
           This method returns the size in bytes of the data written to this Writer.
 long[] getWriteTime()
           Return the time spent writing to the destination Writer(s) in milliseconds.
 void write(char[] b)
          
 void write(char[] b, int off, int len)
          
 void write(int b)
          
 
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

closeCalled

protected boolean closeCalled
True when close() is invoked. Prevents data from being written to the destination Writer(s) after close() has been invoked.


destinations

protected final Writer[] destinations
The destination Writer(s) where data is written.

Constructor Detail

TeeWriter

public TeeWriter(Writer... destinations)

Creates a TeeWriter and saves its arguments, the destinations for later use.

This constructor allow to specify multiple Writer to which the data will be copied.

Parameters:
destinations - Data written to thisWriter are copied to all the destinations.
Since:
1.2.4
Method Detail

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Specified by:
close in class Writer
Throws:
IOException

enableCopy

public final void enableCopy(boolean enable)

Allow to switch off the copy to the underlying streams. Setting the parameter to false will disable the copy to all the underlying streams at once.

If you need more fine grained control you should use enableCopy(boolean[]) .

Parameters:
enable - whether to copy or not the bytes to the underlying stream.
Since:
1.2.9

enableCopy

public final void enableCopy(boolean[] enable)

Allow to switch off the copy to the underlying streams, selectively enabling or disabling copy on some specific stream.

The copy is enabled by default. Each element in the array correspond to an OutputStream passed in the constructor. If the correspondent element in the array passed as a parameter is set to true the copy will be enabled.It can be invoked multiple times.

Parameters:
enable - whether to copy or not the bytes to the underlying stream.
Since:
1.2.9

flush

public void flush()
           throws IOException

Specified by:
flush in interface Flushable
Specified by:
flush in class Writer
Throws:
IOException

getSize

public final long getSize()

This method returns the size in bytes of the data written to this Writer. It can be used to collect statistics on the write operations.

Returns:
size in bytes of the data written to the Writers.

getWriteTime

public long[] getWriteTime()

Return the time spent writing to the destination Writer(s) in milliseconds.

The returned array has one element for each Writer passed in the constructor.

Returns:
time spent writing on the destination Writers.

write

public void write(char[] b)
           throws IOException

Overrides:
write in class Writer
Throws:
IOException

write

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

Specified by:
write in class Writer
Throws:
IOException

write

public void write(int b)
           throws IOException

Overrides:
write in class Writer
Throws:
IOException


Copyright © 2008-2011. All Rights Reserved.