com.gc.iotools.stream.os
Class TeeOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.gc.iotools.stream.os.TeeOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class TeeOutputStream
extends OutputStream

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

Usage:

         InputStream source=... //some data to be read.
   ByteArrayOutputStream destination1= new ByteArrayOutputStream();
   ByteArrayOutputStream destination2= new ByteArrayOutputStream();
   
   TeeOutputStream tee =  new TeeOutputStream(destination1,destination2);
   org.apache.commons.io.IOUtils.copy(source,tee);
   tee.close();
   //at this point both destination1 and destination2 contains the same bytes.
 

Since:
1.2.4
Author:
dvd.smnt

Field Summary
protected  boolean closeCalled
          True when close() is invoked.
protected  OutputStream[] destinations
          The destination OutputStream(s) where data is written.
 
Constructor Summary
TeeOutputStream(OutputStream... destinations)
           Creates a TeeOutputStream and saves its arguments, the destinations for later use.
 
Method Summary
 void close()
          
 void flush()
          
 long getSize()
           This method returns the size in bytes of the data written to this OutputStream.
 long[] getWriteTime()
           Return the time spent writing to the destination OutputStream(s) in milliseconds.
 void write(byte[] b)
          
 void write(byte[] b, int off, int len)
          
 void write(int b)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

destinations

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


closeCalled

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

Constructor Detail

TeeOutputStream

public TeeOutputStream(OutputStream... destinations)

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

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

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

close

public void close()
           throws IOException

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

flush

public void flush()
           throws IOException

Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException

getSize

public final long getSize()

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

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

getWriteTime

public long[] getWriteTime()

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

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

Returns:
time spent writing on the destination OutputStreams.

write

public void write(byte[] b)
           throws IOException

Overrides:
write in class OutputStream
Throws:
IOException

write

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

Overrides:
write in class OutputStream
Throws:
IOException

write

public void write(int b)
           throws IOException

Specified by:
write in class OutputStream
Throws:
IOException


Copyright © 2008-2009. All Rights Reserved.