1package org.bouncycastle.util.io;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.io.OutputStream;
6
7/**
8 * An input stream which copies anything read through it to another stream.
9 */
10public class TeeInputStream
11    extends InputStream
12{
13    private final InputStream input;
14    private final OutputStream output;
15
16    /**
17     * Base constructor.
18     *
19     * @param input input stream to be wrapped.
20     * @param output output stream to copy any input read to.
21     */
22    public TeeInputStream(InputStream input, OutputStream output)
23    {
24        this.input = input;
25        this.output = output;
26    }
27
28    public int read(byte[] buf)
29        throws IOException
30    {
31        return read(buf, 0, buf.length);
32    }
33
34    public int read(byte[] buf, int off, int len)
35        throws IOException
36    {
37        int i = input.read(buf, off, len);
38
39        if (i > 0)
40        {
41            output.write(buf, off, i);
42        }
43
44        return i;
45    }
46
47    public int read()
48        throws IOException
49    {
50        int i = input.read();
51
52        if (i >= 0)
53        {
54            output.write(i);
55        }
56
57        return i;
58    }
59
60    public void close()
61        throws IOException
62    {
63        this.input.close();
64        this.output.close();
65    }
66
67    public OutputStream getOutputStream()
68    {
69        return output;
70    }
71}
72