1package org.bouncycastle.util.io;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.io.OutputStream;
6
7public class TeeInputStream
8    extends InputStream
9{
10    private final InputStream input;
11    private final OutputStream output;
12
13    public TeeInputStream(InputStream input, OutputStream output)
14    {
15        this.input = input;
16        this.output = output;
17    }
18
19    public int read(byte[] buf)
20        throws IOException
21    {
22        return read(buf, 0, buf.length);
23    }
24
25    public int read(byte[] buf, int off, int len)
26        throws IOException
27    {
28        int i = input.read(buf, off, len);
29
30        if (i > 0)
31        {
32            output.write(buf, off, i);
33        }
34
35        return i;
36    }
37
38    public int read()
39        throws IOException
40    {
41        int i = input.read();
42
43        if (i >= 0)
44        {
45            output.write(i);
46        }
47
48        return i;
49    }
50
51    public void close()
52        throws IOException
53    {
54        this.input.close();
55        this.output.close();
56    }
57
58    public OutputStream getOutputStream()
59    {
60        return output;
61    }
62}
63