M
Morten Wittrock
Hi
I need a FilterInputStream with the following behaviour: Whenever one
or more bytes are read from the underlying InputStream, the same bytes
are written to an associated OutputStream (similar to the Unix tee
utility). I've come up with the following class (imports not shown):
----------------------------8<----------------------------
final class TeeInputStream extends FilterInputStream {
private OutputStream out;
public TeeInputStream(InputStream in, OutputStream out) {
super(in);
this.out = out;
}
public int read() throws IOException {
int b = super.read();
if (b != -1) {
out.write(b);
}
return b;
}
public int read(byte[] b, int off, int len) throws IOException {
int nb = super.read(b, off, len);
if (nb != -1) {
out.write(b, off, nb);
}
return nb;
}
}
----------------------------8<----------------------------
The read(byte b[]) method has not been overridden, since the
FilterInputStream implementation already returns read(b, 0, b.length).
My question to the group at large: Is the above a decent solution or am
I missing something here? Comments in general are also welcomed.
Thread-safety is not an issue.
Regards,
Morten Wittrock
I need a FilterInputStream with the following behaviour: Whenever one
or more bytes are read from the underlying InputStream, the same bytes
are written to an associated OutputStream (similar to the Unix tee
utility). I've come up with the following class (imports not shown):
----------------------------8<----------------------------
final class TeeInputStream extends FilterInputStream {
private OutputStream out;
public TeeInputStream(InputStream in, OutputStream out) {
super(in);
this.out = out;
}
public int read() throws IOException {
int b = super.read();
if (b != -1) {
out.write(b);
}
return b;
}
public int read(byte[] b, int off, int len) throws IOException {
int nb = super.read(b, off, len);
if (nb != -1) {
out.write(b, off, nb);
}
return nb;
}
}
----------------------------8<----------------------------
The read(byte b[]) method has not been overridden, since the
FilterInputStream implementation already returns read(b, 0, b.length).
My question to the group at large: Is the above a decent solution or am
I missing something here? Comments in general are also welcomed.
Thread-safety is not an issue.
Regards,
Morten Wittrock