1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.apache.commons.io.output;
18
19import java.io.IOException;
20import java.io.OutputStream;
21
22/**
23 * Classic splitter of OutputStream. Named after the unix 'tee'
24 * command. It allows a stream to be branched off so there
25 * are now two streams.
26 *
27 * @version $Id: TeeOutputStream.java 610010 2008-01-08 14:50:59Z niallp $
28 */
29public class TeeOutputStream extends ProxyOutputStream {
30
31    /** the second OutputStream to write to */
32    protected OutputStream branch;
33
34    /**
35     * Constructs a TeeOutputStream.
36     * @param out the main OutputStream
37     * @param branch the second OutputStream
38     */
39    public TeeOutputStream( OutputStream out, OutputStream branch ) {
40        super(out);
41        this.branch = branch;
42    }
43
44    /**
45     * Write the bytes to both streams.
46     * @param b the bytes to write
47     * @throws IOException if an I/O error occurs
48     */
49    public synchronized void write(byte[] b) throws IOException {
50        super.write(b);
51        this.branch.write(b);
52    }
53
54    /**
55     * Write the specified bytes to both streams.
56     * @param b the bytes to write
57     * @param off The start offset
58     * @param len The number of bytes to write
59     * @throws IOException if an I/O error occurs
60     */
61    public synchronized void write(byte[] b, int off, int len) throws IOException {
62        super.write(b, off, len);
63        this.branch.write(b, off, len);
64    }
65
66    /**
67     * Write a byte to both streams.
68     * @param b the byte to write
69     * @throws IOException if an I/O error occurs
70     */
71    public synchronized void write(int b) throws IOException {
72        super.write(b);
73        this.branch.write(b);
74    }
75
76    /**
77     * Flushes both streams.
78     * @throws IOException if an I/O error occurs
79     */
80    public void flush() throws IOException {
81        super.flush();
82        this.branch.flush();
83    }
84
85    /**
86     * Closes both streams.
87     * @throws IOException if an I/O error occurs
88     */
89    public void close() throws IOException {
90        super.close();
91        this.branch.close();
92    }
93
94}
95