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     */
017    package org.apache.commons.io.output;
018    
019    import java.io.IOException;
020    import java.io.OutputStream;
021    
022    /**
023     * Data written to this stream is forwarded to a stream that has been associated
024     * with this thread.
025     *
026     * @version $Id: DemuxOutputStream.java 1302056 2012-03-18 03:03:38Z ggregory $
027     */
028    public class DemuxOutputStream
029        extends OutputStream
030    {
031        private final InheritableThreadLocal<OutputStream> m_streams = new InheritableThreadLocal<OutputStream>();
032    
033        /**
034         * Bind the specified stream to the current thread.
035         *
036         * @param output the stream to bind
037         * @return the OutputStream that was previously active
038         */
039        public OutputStream bindStream( OutputStream output )
040        {
041            OutputStream stream = m_streams.get();
042            m_streams.set( output );
043            return stream;
044        }
045    
046        /**
047         * Closes stream associated with current thread.
048         *
049         * @throws IOException if an error occurs
050         */
051        @Override
052        public void close()
053            throws IOException
054        {
055            OutputStream output = m_streams.get();
056            if( null != output )
057            {
058                output.close();
059            }
060        }
061    
062        /**
063         * Flushes stream associated with current thread.
064         *
065         * @throws IOException if an error occurs
066         */
067        @Override
068        public void flush()
069            throws IOException
070        {
071            OutputStream output = m_streams.get();
072            if( null != output )
073            {
074                output.flush();
075            }
076        }
077    
078        /**
079         * Writes byte to stream associated with current thread.
080         *
081         * @param ch the byte to write to stream
082         * @throws IOException if an error occurs
083         */
084        @Override
085        public void write( int ch )
086            throws IOException
087        {
088            OutputStream output = m_streams.get();
089            if( null != output )
090            {
091                output.write( ch );
092            }
093        }
094    }