001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.output;
018
019import java.io.IOException;
020import java.io.OutputStream;
021
022/**
023 * Classic splitter of {@link OutputStream}. Named after the UNIX 'tee' command. It allows a stream to be branched off
024 * so there are now two streams.
025 */
026public class TeeOutputStream extends ProxyOutputStream {
027
028    /**
029     * The second OutputStream to write to.
030     *
031     * TODO Make private and final in 3.0.
032     */
033    protected OutputStream branch;
034
035    /**
036     * Constructs a TeeOutputStream.
037     *
038     * @param out    the main OutputStream
039     * @param branch the second OutputStream
040     */
041    public TeeOutputStream(final OutputStream out, final OutputStream branch) {
042        super(out);
043        this.branch = branch;
044    }
045
046    /**
047     * Closes both output streams.
048     * <p>
049     * If closing the main output stream throws an exception, attempt to close the branch output stream.
050     * </p>
051     *
052     * <p>
053     * If closing the main and branch output streams both throw exceptions, which exceptions is thrown by this method is
054     * currently unspecified and subject to change.
055     * </p>
056     *
057     * @throws IOException if an I/O error occurs.
058     */
059    @Override
060    public void close() throws IOException {
061        try {
062            super.close();
063        } finally {
064            this.branch.close();
065        }
066    }
067
068    /**
069     * Flushes both streams.
070     *
071     * @throws IOException if an I/O error occurs.
072     */
073    @Override
074    public void flush() throws IOException {
075        super.flush();
076        this.branch.flush();
077    }
078
079    /**
080     * Writes the bytes to both streams.
081     *
082     * @param b the bytes to write
083     * @throws IOException if an I/O error occurs.
084     */
085    @Override
086    public synchronized void write(final byte[] b) throws IOException {
087        super.write(b);
088        this.branch.write(b);
089    }
090
091    /**
092     * Writes the specified bytes to both streams.
093     *
094     * @param b   the bytes to write
095     * @param off The start offset
096     * @param len The number of bytes to write
097     * @throws IOException if an I/O error occurs.
098     */
099    @Override
100    public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
101        super.write(b, off, len);
102        this.branch.write(b, off, len);
103    }
104
105    /**
106     * Writes a byte to both streams.
107     *
108     * @param b the byte to write
109     * @throws IOException if an I/O error occurs.
110     */
111    @Override
112    public synchronized void write(final int b) throws IOException {
113        super.write(b);
114        this.branch.write(b);
115    }
116
117}