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