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 {@link OutputStream}. Named after the Unix 'tee' command. It allows a stream to be branched off
24   * so there are now two streams.
25   */
26  public class TeeOutputStream extends ProxyOutputStream {
27  
28      /**
29       * The second OutputStream to write to.
30       *
31       * TODO Make private and final in 3.0.
32       */
33      protected OutputStream branch;
34  
35      /**
36       * Constructs a TeeOutputStream.
37       *
38       * @param out    the main OutputStream
39       * @param branch the second OutputStream
40       */
41      public TeeOutputStream(final OutputStream out, final OutputStream branch) {
42          super(out);
43          this.branch = branch;
44      }
45  
46      /**
47       * Writes the bytes to both streams.
48       *
49       * @param b the bytes to write
50       * @throws IOException if an I/O error occurs.
51       */
52      @Override
53      public synchronized void write(final byte[] b) throws IOException {
54          super.write(b);
55          this.branch.write(b);
56      }
57  
58      /**
59       * Writes the specified bytes to both streams.
60       *
61       * @param b   the bytes to write
62       * @param off The start offset
63       * @param len The number of bytes to write
64       * @throws IOException if an I/O error occurs.
65       */
66      @Override
67      public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
68          super.write(b, off, len);
69          this.branch.write(b, off, len);
70      }
71  
72      /**
73       * Writes a byte to both streams.
74       *
75       * @param b the byte to write
76       * @throws IOException if an I/O error occurs.
77       */
78      @Override
79      public synchronized void write(final int b) throws IOException {
80          super.write(b);
81          this.branch.write(b);
82      }
83  
84      /**
85       * Flushes both streams.
86       *
87       * @throws IOException if an I/O error occurs.
88       */
89      @Override
90      public void flush() throws IOException {
91          super.flush();
92          this.branch.flush();
93      }
94  
95      /**
96       * Closes both output streams.
97       * <p>
98       * If closing the main output stream throws an exception, attempt to close the branch output stream.
99       * </p>
100      *
101      * <p>
102      * If closing the main and branch output streams both throw exceptions, which exceptions is thrown by this method is
103      * currently unspecified and subject to change.
104      * </p>
105      *
106      * @throws IOException if an I/O error occurs.
107      */
108     @Override
109     public void close() throws IOException {
110         try {
111             super.close();
112         } finally {
113             this.branch.close();
114         }
115     }
116 
117 }