com.gc.iotools.stream.is
Class TeeInputStreamOutputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.gc.iotools.stream.base.AbstractInputStreamWrapper
          extended by com.gc.iotools.stream.is.TeeInputStreamOutputStream
All Implemented Interfaces:
Closeable

public class TeeInputStreamOutputStream
extends AbstractInputStreamWrapper

Copies the data from the underlying InputStream to the OutputStream(s) passed in the constructor. The data copied are similar to the underlying InputStream.

When the method AbstractInputStreamWrapper.close() is invoked all the bytes remaining in the underlying InputStream are copied to the OutputStream(s). This behavior is different from this class and TeeInputStream in Apache commons-io.

Bytes skipped are in any case copied to the OutputStream. Mark and reset of the outer InputStream doesn't affect the data copied to the OutputStream(s), that remain similar to the InputStream passed in the constructor.

It also calculate some statistics on the read/write operations. getWriteTime() returns the time spent writing to the OutputStreams, getReadTime() returns the time spent reading from the InputStream and getWriteSize() returns the amount of data written to a single OutputStream until now.

Sample usage:

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

   TeeInputStreamOutputStream tee=
                          new TeeInputStreamOutputStream(source,destination1);
   org.apache.commons.io.IOUtils.copy(tee,destination2);
   tee.close();
   //at this point both destination1 and destination2 contains the same bytes
   //in destination1 were put by TeeInputStreamOutputStream while in
   //destination2 they were copied by IOUtils.
   byte[] bytes=destination1.getBytes();
 

Since:
1.0.6
Version:
$Id: TeeInputStreamOutputStream.java 527 2014-02-24 19:29:50Z gabriele.contini@gmail.com $
Author:
dvd.smnt
See Also:
TeeInputStream

Field Summary
protected  boolean closeStreams
          If true source and destination streams are closed when AbstractInputStreamWrapper.close() is invoked.
protected  boolean[] copyEnabled
           
protected  OutputStream[] destinations
          The destination OutputStream where data is written.
 
Fields inherited from class com.gc.iotools.stream.base.AbstractInputStreamWrapper
closeCalled, source
 
Constructor Summary
TeeInputStreamOutputStream(InputStream source, boolean closeStreams, OutputStream... destinations)
           Creates a TeeInputStreamOutputStream and saves its argument, the input stream source and the output stream destination for later use.
TeeInputStreamOutputStream(InputStream source, OutputStream destination)
           Creates a TeeOutputStream and saves its argument, the input stream source and the OutputStream destination for later use.
TeeInputStreamOutputStream(InputStream source, OutputStream destination, boolean closeStreams)
          Creates a TeeOutputStream and saves its argument, the input stream source and the output stream destination for later use.
 
Method Summary
 int available()
          
 void closeOnce()
          closeOnce
 void enableCopy(boolean enable)
           Allow to switch off the copy to the underlying OutputStreams.
 void enableCopy(boolean[] enable)
           Allow to switch off the copy to the underlying OutputStreams, selectively enabling or disabling copy to some specific stream.
 OutputStream[] getDestinationStreams()
           Returns the OutputStream(s) passed in the constructor.
 long getReadTime()
           Returns the number of milliseconds spent reading from the source InputStream.
 long getWriteSize()
           Returns the number of bytes written until now to a single destination OutputStream.
 long[] getWriteTime()
           Return the time spent writing on the destination OutputStream(s) in milliseconds.
 int innerRead(byte[] b, int off, int len)
          innerRead
 void mark(int readLimit)
          
 boolean markSupported()
          
 int read()
          
 void reset()
          
 
Methods inherited from class com.gc.iotools.stream.base.AbstractInputStreamWrapper
close, read, skip
 
Methods inherited from class java.io.InputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

closeStreams

protected final boolean closeStreams
If true source and destination streams are closed when AbstractInputStreamWrapper.close() is invoked.


copyEnabled

protected final boolean[] copyEnabled

destinations

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

Constructor Detail

TeeInputStreamOutputStream

public TeeInputStreamOutputStream(InputStream source,
                                  boolean closeStreams,
                                  OutputStream... destinations)

Creates a TeeInputStreamOutputStream and saves its argument, the input stream source and the output stream destination for later use.

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

Parameters:
source - The underlying InputStream
closeStreams - if true the destination will be closed when the AbstractInputStreamWrapper.close() method is invoked. If false the close method on the underlying streams will not be called (it must be invoked externally).
destinations - Data read from source are also written to this OutputStream.
Since:
1.2.3

TeeInputStreamOutputStream

public TeeInputStreamOutputStream(InputStream source,
                                  OutputStream destination)

Creates a TeeOutputStream and saves its argument, the input stream source and the OutputStream destination for later use.

When the method AbstractInputStreamWrapper.close() it is invoked the remaining content of the source stream is copied to the destination and the source and destination streams are closed.

Parameters:
source - The underlying InputStream
destination - Data read from source are also written to this OutputStream.

TeeInputStreamOutputStream

public TeeInputStreamOutputStream(InputStream source,
                                  OutputStream destination,
                                  boolean closeStreams)
Creates a TeeOutputStream and saves its argument, the input stream source and the output stream destination for later use.

Parameters:
source - The underlying InputStream
destination - Data read from source are also written to this OutputStream.
closeStreams - if true the destination will be closed when the AbstractInputStreamWrapper.close() method is invoked. If false the close method on the underlying streams will not be called (it must be invoked externally).
Since:
1.2.0
Method Detail

available

public int available()
              throws IOException

Overrides:
available in class InputStream
Throws:
IOException

closeOnce

public void closeOnce()
               throws IOException

closeOnce

This method is called when the method AbstractInputStreamWrapper.close() is invoked. It copies all the data eventually remaining in the source InputStream passed in the constructor to the destination OutputStream.

The standard behavior is to close both the underlying InputStream and OutputStream(s). When the class was constructed with the parameter closeCalled set to false the underlying streams must be closed externally.

Specified by:
closeOnce in class AbstractInputStreamWrapper
Throws:
IOException - if any error occurs reading the bytes.
See Also:
AbstractInputStreamWrapper.close()

enableCopy

public final void enableCopy(boolean enable)

Allow to switch off the copy to the underlying OutputStreams. 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 OutputStreams, selectively enabling or disabling copy to 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 OutputStreams.
Since:
1.2.9

getDestinationStreams

public final OutputStream[] getDestinationStreams()

Returns the OutputStream(s) passed in the constructor.

Returns:
Array of OutputStream passed in the constructor.
Since:
1.2.9

getReadTime

public long getReadTime()

Returns the number of milliseconds spent reading from the source InputStream.

Returns:
number of milliseconds spent reading from the source .
Since:
1.2.5

getWriteSize

public long getWriteSize()

Returns the number of bytes written until now to a single destination OutputStream.

This number is not affected by any of the mark and reset that are made on this TeeInputStreamOutputStream and reflects only the number of bytes written.

Returns:
number of bytes written until now to a single destination.
Since:
1.2.5

getWriteTime

public long[] getWriteTime()

Return the time spent writing on 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.

innerRead

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

innerRead

Specified by:
innerRead in class AbstractInputStreamWrapper
Parameters:
b - an array of byte.
off - a int.
len - a int.
Returns:
a int.
Throws:
IOException - if any error occurs reading the bytes.

mark

public void mark(int readLimit)

Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

Overrides:
mark in class InputStream
Since:
1.2.0
See Also:
reset(), InputStream.mark(int)

markSupported

public boolean markSupported()

Overrides:
markSupported in class InputStream

read

public int read()
         throws IOException

Overrides:
read in class AbstractInputStreamWrapper
Throws:
IOException

reset

public void reset()
           throws IOException

Repositions this stream to the position at the time the mark method was last called on this input stream.

After reset() method is called the data is not copied anymore to the destination OutputStream until the position where reset was issued is reached again. This ensures the data copied to the destination OutputStream reflects the data contained in the source InputStream (the one passed in the constructor).

Overrides:
reset in class InputStream
Throws:
IOException - If the source stream has an exception in calling reset().
Since:
1.2.0
See Also:
mark(int), InputStream.reset()


Copyright © 2008–2014. All rights reserved.