org.apache.commons.io.input
Class TeeInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by org.apache.commons.io.input.ProxyInputStream
              extended by org.apache.commons.io.input.TeeInputStream
All Implemented Interfaces:
java.io.Closeable

public class TeeInputStream
extends ProxyInputStream

InputStream proxy that transparently writes a copy of all bytes read from the proxied stream to a given OutputStream. Using ProxyInputStream.skip(long) or ProxyInputStream.mark(int)/ProxyInputStream.reset() on the stream will result on some bytes from the input stream being skipped or duplicated in the output stream.

The proxied input stream is closed when the close() method is called on this proxy. It is configurable whether the associated output stream will also closed.

Since:
Commons IO 1.4
Version:
$Id: TeeInputStream.java 587913 2007-10-24 15:47:30Z niallp $

Field Summary
private  java.io.OutputStream branch
          The output stream that will receive a copy of all bytes read from the proxied input stream.
private  boolean closeBranch
          Flag for closing also the associated output stream when this stream is closed.
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
TeeInputStream(java.io.InputStream input, java.io.OutputStream branch)
          Creates a TeeInputStream that proxies the given InputStream and copies all read bytes to the given OutputStream.
TeeInputStream(java.io.InputStream input, java.io.OutputStream branch, boolean closeBranch)
          Creates a TeeInputStream that proxies the given InputStream and copies all read bytes to the given OutputStream.
 
Method Summary
 void close()
          Closes the proxied input stream and, if so configured, the associated output stream.
 int read()
          Reads a single byte from the proxied input stream and writes it to the associated output stream.
 int read(byte[] bts)
          Reads bytes from the proxied input stream and writes the read bytes to the associated output stream.
 int read(byte[] bts, int st, int end)
          Reads bytes from the proxied input stream and writes the read bytes to the associated output stream.
 
Methods inherited from class org.apache.commons.io.input.ProxyInputStream
available, mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

branch

private final java.io.OutputStream branch
The output stream that will receive a copy of all bytes read from the proxied input stream.


closeBranch

private final boolean closeBranch
Flag for closing also the associated output stream when this stream is closed.

Constructor Detail

TeeInputStream

public TeeInputStream(java.io.InputStream input,
                      java.io.OutputStream branch)
Creates a TeeInputStream that proxies the given InputStream and copies all read bytes to the given OutputStream. The given output stream will not be closed when this stream gets closed.

Parameters:
input - input stream to be proxied
branch - output stream that will receive a copy of all bytes read

TeeInputStream

public TeeInputStream(java.io.InputStream input,
                      java.io.OutputStream branch,
                      boolean closeBranch)
Creates a TeeInputStream that proxies the given InputStream and copies all read bytes to the given OutputStream. The given output stream will be closed when this stream gets closed if the closeBranch parameter is true.

Parameters:
input - input stream to be proxied
branch - output stream that will receive a copy of all bytes read
closeBranch - flag for closing also the output stream when this stream is closed
Method Detail

close

public void close()
           throws java.io.IOException
Closes the proxied input stream and, if so configured, the associated output stream. An exception thrown from one stream will not prevent closing of the other stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class ProxyInputStream
Throws:
java.io.IOException - if either of the streams could not be closed

read

public int read()
         throws java.io.IOException
Reads a single byte from the proxied input stream and writes it to the associated output stream.

Overrides:
read in class ProxyInputStream
Returns:
next byte from the stream, or -1 if the stream has ended
Throws:
java.io.IOException - if the stream could not be read (or written)

read

public int read(byte[] bts,
                int st,
                int end)
         throws java.io.IOException
Reads bytes from the proxied input stream and writes the read bytes to the associated output stream.

Overrides:
read in class ProxyInputStream
Parameters:
bts - byte buffer
st - start offset within the buffer
end - maximum number of bytes to read
Returns:
number of bytes read, or -1 if the stream has ended
Throws:
java.io.IOException - if the stream could not be read (or written)

read

public int read(byte[] bts)
         throws java.io.IOException
Reads bytes from the proxied input stream and writes the read bytes to the associated output stream.

Overrides:
read in class ProxyInputStream
Parameters:
bts - byte buffer
Returns:
number of bytes read, or -1 if the stream has ended
Throws:
java.io.IOException - if the stream could not be read (or written)


Copyright (c) 2002-2012 Apache Software Foundation