/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.io.output;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Data written to this stream is forwarded to a stream that has been associated
 * with this thread.
 *
 * @version $Id$
 */
public class DemuxOutputStream
    extends OutputStream
{
    private final InheritableThreadLocal<OutputStream> m_streams = new InheritableThreadLocal<OutputStream>();

    /**
     * Bind the specified stream to the current thread.
     *
     * @param output the stream to bind
     * @return the OutputStream that was previously active
     */
    public OutputStream bindStream( final OutputStream output )
    {
        final OutputStream stream = m_streams.get();
        m_streams.set( output );
        return stream;
    }

    /**
     * Closes stream associated with current thread.
     *
     * @throws IOException if an error occurs
     */
    @Override
    public void close()
        throws IOException
    {
        final OutputStream output = m_streams.get();
        if( null != output )
        {
            output.close();
        }
    }

    /**
     * Flushes stream associated with current thread.
     *
     * @throws IOException if an error occurs
     */
    @Override
    public void flush()
        throws IOException
    {
        final OutputStream output = m_streams.get();
        if( null != output )
        {
            output.flush();
        }
    }

    /**
     * Writes byte to stream associated with current thread.
     *
     * @param ch the byte to write to stream
     * @throws IOException if an error occurs
     */
    @Override
    public void write( final int ch )
        throws IOException
    {
        final OutputStream output = m_streams.get();
        if( null != output )
        {
            output.write( ch );
        }
    }
}

@@ -979,19 +979,16 @@
 sociated
-%0A *
  with th
@@ -1052,20 +1052,16 @@
 utStream
-%0A   
  extends
@@ -1073,17 +1073,17 @@
 utStream
-%0A
+ 
 %7B%0A    pr
@@ -1279,16 +1279,34 @@
 m output
+%0A     *           
  the str
@@ -1419,17 +1419,16 @@
 dStream(
- 
 final Ou
@@ -1444,22 +1444,17 @@
 m output
- )%0A   
+)
  %7B%0A     
@@ -1523,24 +1523,22 @@
 ams.set(
- 
 output
- 
 );%0A     
@@ -1648,32 +1648,51 @@
 rows IOException
+%0A     *            
  if an error occ
@@ -1740,24 +1740,16 @@
  close()
-%0A       
  throws 
@@ -1751,36 +1751,32 @@
 rows IOException
-%0A   
  %7B%0A        final
@@ -1817,34 +1817,34 @@
 et();%0A        if
-(
  
+(
 null != output )
@@ -1837,34 +1837,25 @@
 ll != output
- )%0A       
+)
  %7B%0A         
@@ -1977,32 +1977,51 @@
 rows IOException
+%0A     *            
  if an error occ
@@ -2069,24 +2069,16 @@
  flush()
-%0A       
  throws 
@@ -2080,36 +2080,32 @@
 rows IOException
-%0A   
  %7B%0A        final
@@ -2146,34 +2146,34 @@
 et();%0A        if
-(
  
+(
 null != output )
@@ -2166,34 +2166,25 @@
 ll != output
- )%0A       
+)
  %7B%0A         
@@ -2311,16 +2311,34 @@
 param ch
+%0A     *           
  the byt
@@ -2384,16 +2384,35 @@
 xception
+%0A     *            
  if an e
@@ -2467,17 +2467,16 @@
 d write(
- 
 final in
@@ -2479,26 +2479,17 @@
 l int ch
- )%0A       
+)
  throws 
@@ -2499,20 +2499,16 @@
 xception
-%0A   
  %7B%0A     
@@ -2565,18 +2565,18 @@
       if
-(
  
+(
 null != 
@@ -2585,18 +2585,9 @@
 tput
- )%0A       
+)
  %7B%0A 
@@ -2614,12 +2614,10 @@
 ite(
- 
 ch
- 
 );%0A 

Fix funky formating.