TeeOutputStream.java

  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. import java.io.IOException;
  19. import java.io.OutputStream;

  20. /**
  21.  * Classic splitter of {@link OutputStream}. Named after the Unix 'tee' command. It allows a stream to be branched off
  22.  * so there are now two streams.
  23.  */
  24. public class TeeOutputStream extends ProxyOutputStream {

  25.     /**
  26.      * The second OutputStream to write to.
  27.      *
  28.      * TODO Make private and final in 3.0.
  29.      */
  30.     protected OutputStream branch;

  31.     /**
  32.      * Constructs a TeeOutputStream.
  33.      *
  34.      * @param out    the main OutputStream
  35.      * @param branch the second OutputStream
  36.      */
  37.     public TeeOutputStream(final OutputStream out, final OutputStream branch) {
  38.         super(out);
  39.         this.branch = branch;
  40.     }

  41.     /**
  42.      * Closes both output streams.
  43.      * <p>
  44.      * If closing the main output stream throws an exception, attempt to close the branch output stream.
  45.      * </p>
  46.      *
  47.      * <p>
  48.      * If closing the main and branch output streams both throw exceptions, which exceptions is thrown by this method is
  49.      * currently unspecified and subject to change.
  50.      * </p>
  51.      *
  52.      * @throws IOException if an I/O error occurs.
  53.      */
  54.     @Override
  55.     public void close() throws IOException {
  56.         try {
  57.             super.close();
  58.         } finally {
  59.             this.branch.close();
  60.         }
  61.     }

  62.     /**
  63.      * Flushes both streams.
  64.      *
  65.      * @throws IOException if an I/O error occurs.
  66.      */
  67.     @Override
  68.     public void flush() throws IOException {
  69.         super.flush();
  70.         this.branch.flush();
  71.     }

  72.     /**
  73.      * Writes the bytes to both streams.
  74.      *
  75.      * @param b the bytes to write
  76.      * @throws IOException if an I/O error occurs.
  77.      */
  78.     @Override
  79.     public synchronized void write(final byte[] b) throws IOException {
  80.         super.write(b);
  81.         this.branch.write(b);
  82.     }

  83.     /**
  84.      * Writes the specified bytes to both streams.
  85.      *
  86.      * @param b   the bytes to write
  87.      * @param off The start offset
  88.      * @param len The number of bytes to write
  89.      * @throws IOException if an I/O error occurs.
  90.      */
  91.     @Override
  92.     public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
  93.         super.write(b, off, len);
  94.         this.branch.write(b, off, len);
  95.     }

  96.     /**
  97.      * Writes a byte to both streams.
  98.      *
  99.      * @param b the byte to write
  100.      * @throws IOException if an I/O error occurs.
  101.      */
  102.     @Override
  103.     public synchronized void write(final int b) throws IOException {
  104.         super.write(b);
  105.         this.branch.write(b);
  106.     }

  107. }