View Javadoc
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   */
17  package org.apache.commons.io.output;
18  
19  import java.io.IOException;
20  import 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 1415850 2012-11-30 20:51:39Z ggregory $
28   */
29  public 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(final OutputStream out, final 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      @Override
50      public synchronized void write(final byte[] b) throws IOException {
51          super.write(b);
52          this.branch.write(b);
53      }
54  
55      /**
56       * Write the specified bytes to both streams.
57       * @param b the bytes to write
58       * @param off The start offset
59       * @param len The number of bytes to write
60       * @throws IOException if an I/O error occurs
61       */
62      @Override
63      public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
64          super.write(b, off, len);
65          this.branch.write(b, off, len);
66      }
67  
68      /**
69       * Write a byte to both streams.
70       * @param b the byte to write
71       * @throws IOException if an I/O error occurs
72       */
73      @Override
74      public synchronized void write(final int b) throws IOException {
75          super.write(b);
76          this.branch.write(b);
77      }
78  
79      /**
80       * Flushes both streams.
81       * @throws IOException if an I/O error occurs
82       */
83      @Override
84      public void flush() throws IOException {
85          super.flush();
86          this.branch.flush();
87      }
88  
89      /**
90       * Closes both output streams.
91       * 
92       * If closing the main output stream throws an exception, attempt to close the branch output stream.
93       * 
94       * If closing the main and branch output streams both throw exceptions, which exceptions is thrown by this method is
95       * currently unspecified and subject to change.
96       * 
97       * @throws IOException
98       *             if an I/O error occurs
99       */
100     @Override
101     public void close() throws IOException {
102         try {
103             super.close();
104         } finally {
105             this.branch.close();
106         }
107     }
108 
109 }