Coverage Report - org.apache.commons.io.output.DemuxOutputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
DemuxOutputStream
52%
9/17
16%
1/6
1.75
 
 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  
  */
 17  
 package org.apache.commons.io.output;
 18  
 
 19  
 import java.io.IOException;
 20  
 import 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  
  * @version $Id: DemuxOutputStream.java 1415850 2012-11-30 20:51:39Z ggregory $
 27  
  */
 28  2
 public class DemuxOutputStream
 29  
     extends OutputStream
 30  
 {
 31  2
     private final InheritableThreadLocal<OutputStream> m_streams = new InheritableThreadLocal<OutputStream>();
 32  
 
 33  
     /**
 34  
      * Bind the specified stream to the current thread.
 35  
      *
 36  
      * @param output the stream to bind
 37  
      * @return the OutputStream that was previously active
 38  
      */
 39  
     public OutputStream bindStream( final OutputStream output )
 40  
     {
 41  8
         final OutputStream stream = m_streams.get();
 42  8
         m_streams.set( output );
 43  8
         return stream;
 44  
     }
 45  
 
 46  
     /**
 47  
      * Closes stream associated with current thread.
 48  
      *
 49  
      * @throws IOException if an error occurs
 50  
      */
 51  
     @Override
 52  
     public void close()
 53  
         throws IOException
 54  
     {
 55  0
         final OutputStream output = m_streams.get();
 56  0
         if( null != output )
 57  
         {
 58  0
             output.close();
 59  
         }
 60  0
     }
 61  
 
 62  
     /**
 63  
      * Flushes stream associated with current thread.
 64  
      *
 65  
      * @throws IOException if an error occurs
 66  
      */
 67  
     @Override
 68  
     public void flush()
 69  
         throws IOException
 70  
     {
 71  0
         final OutputStream output = m_streams.get();
 72  0
         if( null != output )
 73  
         {
 74  0
             output.flush();
 75  
         }
 76  0
     }
 77  
 
 78  
     /**
 79  
      * Writes byte to stream associated with current thread.
 80  
      *
 81  
      * @param ch the byte to write to stream
 82  
      * @throws IOException if an error occurs
 83  
      */
 84  
     @Override
 85  
     public void write( final int ch )
 86  
         throws IOException
 87  
     {
 88  128
         final OutputStream output = m_streams.get();
 89  128
         if( null != output )
 90  
         {
 91  128
             output.write( ch );
 92  
         }
 93  128
     }
 94  
 }