    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 *      https://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
    018package org.apache.commons.io.output;
    019
    020import java.io.IOException;
    021import java.io.OutputStream;
    022
    023import org.apache.commons.io.IOUtils;
    024
    025/**
    026 * Forwards data to a stream that has been associated with this thread.
    027 */
    028public class DemuxOutputStream extends OutputStream {
    029
    030    private final InheritableThreadLocal<OutputStream> outputStreamThreadLocal = new InheritableThreadLocal<>();
    031
    032    /**
    033     * Construct a new instance.
    034     */
    035    public DemuxOutputStream() {
    036        // empty
    037    }
    038
    039    /**
    040     * Binds the specified stream to the current thread.
    041     *
    042     * @param output 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 if an error occurs.
    055     */
    056    @SuppressWarnings("resource") // we actually close the stream here
    057    @Override
    058    public void close() throws IOException {
    059        IOUtils.close(outputStreamThreadLocal.get());
    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() throws IOException {
    069        @SuppressWarnings("resource")
    070        final OutputStream output = outputStreamThreadLocal.get();
    071        if (null != output) {
    072            output.flush();
    073        }
    074    }
    075
    076    /**
    077     * Writes byte to stream associated with current thread.
    078     *
    079     * @param ch the byte to write to stream.
    080     * @throws IOException if an error occurs.
    081     */
    082    @Override
    083    public void write(final int ch) throws IOException {
    084        @SuppressWarnings("resource")
    085        final OutputStream output = outputStreamThreadLocal.get();
    086        if (null != output) {
    087            output.write(ch);
    088        }
    089    }
    090}