DemuxOutputStream.java revision bc47398187c6ffd132435e51d8d61e6ec79a79db
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.apache.commons.io.output;
18
19import java.io.IOException;
20import java.io.OutputStream;
21
22/**
23 * Data written to this stream is forwarded to a stream that has been associated
24 * with this thread.
25 *
26 * @author <a href="mailto:peter@apache.org">Peter Donald</a>
27 * @version $Revision: 437567 $ $Date: 2006-08-28 07:39:07 +0100 (Mon, 28 Aug 2006) $
28 */
29public class DemuxOutputStream
30    extends OutputStream
31{
32    private InheritableThreadLocal<OutputStream> m_streams = new InheritableThreadLocal<OutputStream>();
33
34    /**
35     * Bind the specified stream to the current thread.
36     *
37     * @param output the stream to bind
38     * @return the OutputStream that was previously active
39     */
40    public OutputStream bindStream( OutputStream output )
41    {
42        OutputStream stream = getStream();
43        m_streams.set( output );
44        return stream;
45    }
46
47    /**
48     * Closes stream associated with current thread.
49     *
50     * @throws IOException if an error occurs
51     */
52    @Override
53    public void close()
54        throws IOException
55    {
56        OutputStream output = getStream();
57        if( null != output )
58        {
59            output.close();
60        }
61    }
62
63    /**
64     * Flushes stream associated with current thread.
65     *
66     * @throws IOException if an error occurs
67     */
68    @Override
69    public void flush()
70        throws IOException
71    {
72        OutputStream output = getStream();
73        if( null != output )
74        {
75            output.flush();
76        }
77    }
78
79    /**
80     * Writes byte to stream associated with current thread.
81     *
82     * @param ch the byte to write to stream
83     * @throws IOException if an error occurs
84     */
85    @Override
86    public void write( int ch )
87        throws IOException
88    {
89        OutputStream output = getStream();
90        if( null != output )
91        {
92            output.write( ch );
93        }
94    }
95
96    /**
97     * Utility method to retrieve stream bound to current thread (if any).
98     *
99     * @return the output stream
100     */
101    private OutputStream getStream()
102    {
103        return m_streams.get();
104    }
105}
106