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 * Forwards data to a stream that has been associated with this thread.
024 *
025 * @version $Id: DemuxOutputStream.java 1737901 2016-04-06 00:23:36Z ggregory $
026 */
027public class DemuxOutputStream extends OutputStream {
028    private final InheritableThreadLocal<OutputStream> outputStreamThreadLocal = new InheritableThreadLocal<OutputStream>();
029
030    /**
031     * Binds the specified stream to the current thread.
032     *
033     * @param output
034     *            the stream to bind
035     * @return the OutputStream that was previously active
036     */
037    public OutputStream bindStream(final OutputStream output) {
038        final OutputStream stream = outputStreamThreadLocal.get();
039        outputStreamThreadLocal.set(output);
040        return stream;
041    }
042
043    /**
044     * Closes stream associated with current thread.
045     *
046     * @throws IOException
047     *             if an error occurs
048     */
049    @Override
050    public void close() throws IOException {
051        final OutputStream output = outputStreamThreadLocal.get();
052        if (null != output) {
053            output.close();
054        }
055    }
056
057    /**
058     * Flushes stream associated with current thread.
059     *
060     * @throws IOException
061     *             if an error occurs
062     */
063    @Override
064    public void flush() throws IOException {
065        @SuppressWarnings("resource")
066        final OutputStream output = outputStreamThreadLocal.get();
067        if (null != output) {
068            output.flush();
069        }
070    }
071
072    /**
073     * Writes byte to stream associated with current thread.
074     *
075     * @param ch
076     *            the byte to write to stream
077     * @throws IOException
078     *             if an error occurs
079     */
080    @Override
081    public void write(final int ch) throws IOException {
082        @SuppressWarnings("resource")
083        final OutputStream output = outputStreamThreadLocal.get();
084        if (null != output) {
085            output.write(ch);
086        }
087    }
088}