Package org.apache.commons.io.input
Class TeeInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- org.apache.commons.io.input.ProxyInputStream
-
- org.apache.commons.io.input.TeeInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class TeeInputStream extends ProxyInputStream
InputStream proxy that transparently writes a copy of all bytes read from the proxied stream to a given OutputStream. UsingProxyInputStream.skip(long)
orProxyInputStream.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. You may configure whether the input stream closes the output stream.- Since:
- 1.4
- See Also:
ObservableInputStream
-
-
Constructor Summary
Constructors Constructor Description TeeInputStream(java.io.InputStream input, java.io.OutputStream branch)
Creates a TeeInputStream that proxies the givenInputStream
and copies all read bytes to the givenOutputStream
.TeeInputStream(java.io.InputStream input, java.io.OutputStream branch, boolean closeBranch)
Creates a TeeInputStream that proxies the givenInputStream
and copies all read bytes to the givenOutputStream
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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
afterRead, available, beforeRead, handleIOException, mark, markSupported, reset, skip
-
-
-
-
Constructor Detail
-
TeeInputStream
public TeeInputStream(java.io.InputStream input, java.io.OutputStream branch)
Creates a TeeInputStream that proxies the givenInputStream
and copies all read bytes to the givenOutputStream
. The given output stream will not be closed when this stream gets closed.- Parameters:
input
- input stream to be proxiedbranch
- 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 givenInputStream
and copies all read bytes to the givenOutputStream
. The given output stream will be closed when this stream gets closed if the closeBranch parameter istrue
.- Parameters:
input
- input stream to be proxiedbranch
- output stream that will receive a copy of all bytes readcloseBranch
- 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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classProxyInputStream
- 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 classProxyInputStream
- 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 classProxyInputStream
- Parameters:
bts
- byte bufferst
- start offset within the bufferend
- 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 classProxyInputStream
- 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)
-
-