TeeInputStreampublic class TeeInputStream extends ProxyInputStream InputStream proxy that transparently writes a copy of all bytes read
from the proxied stream to a given OutputStream. Using {@link #skip(long)}
or {@link #mark(int)}/{@link #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 {@link #close()} method is
called on this proxy. It is configurable whether the associated output
stream will also closed. |
Fields Summary |
---|
private final OutputStream | branchThe output stream that will receive a copy of all bytes read from the
proxied input stream. | private final boolean | closeBranchFlag for closing also the associated output stream when this
stream is closed. |
Constructors Summary |
---|
public TeeInputStream(InputStream input, OutputStream branch)Creates a TeeInputStream that proxies the given {@link InputStream}
and copies all read bytes to the given {@link OutputStream}. The given
output stream will not be closed when this stream gets closed.
this(input, branch, false);
| public TeeInputStream(InputStream input, OutputStream branch, boolean closeBranch)Creates a TeeInputStream that proxies the given {@link InputStream}
and copies all read bytes to the given {@link OutputStream}. The given
output stream will be closed when this stream gets closed if the
closeBranch parameter is true .
super(input);
this.branch = branch;
this.closeBranch = closeBranch;
|
Methods Summary |
---|
public void | close()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.
try {
super.close();
} finally {
if (closeBranch) {
branch.close();
}
}
| public int | read()Reads a single byte from the proxied input stream and writes it to
the associated output stream.
int ch = super.read();
if (ch != -1) {
branch.write(ch);
}
return ch;
| public 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.
int n = super.read(bts, st, end);
if (n != -1) {
branch.write(bts, st, n);
}
return n;
| public int | read(byte[] bts)Reads bytes from the proxied input stream and writes the read bytes
to the associated output stream.
int n = super.read(bts);
if (n != -1) {
branch.write(bts, 0, n);
}
return n;
|
|