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