Coverage Report - org.apache.commons.io.input.DemuxInputStream
 
Classes in this File Line Coverage Branch Coverage Complexity
DemuxInputStream
61%
8/13
25%
1/4
2
 
 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.input;
 18  
 
 19  
 import static org.apache.commons.io.IOUtils.EOF;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 
 24  
 /**
 25  
  * Data written to this stream is forwarded to a stream that has been associated
 26  
  * with this thread.
 27  
  *
 28  
  * @version $Id: DemuxInputStream.java 1586350 2014-04-10 15:57:20Z ggregory $
 29  
  */
 30  1
 public class DemuxInputStream
 31  
     extends InputStream
 32  
 {
 33  1
     private final InheritableThreadLocal<InputStream> m_streams = new InheritableThreadLocal<InputStream>();
 34  
 
 35  
     /**
 36  
      * Bind the specified stream to the current thread.
 37  
      *
 38  
      * @param input the stream to bind
 39  
      * @return the InputStream that was previously active
 40  
      */
 41  
     public InputStream bindStream( final InputStream input )
 42  
     {
 43  4
         final InputStream oldValue = m_streams.get();
 44  4
         m_streams.set( input );
 45  4
         return oldValue;
 46  
     }
 47  
 
 48  
     /**
 49  
      * Closes stream associated with current thread.
 50  
      *
 51  
      * @throws IOException if an error occurs
 52  
      */
 53  
     @Override
 54  
     public void close()
 55  
         throws IOException
 56  
     {
 57  0
         final InputStream input = m_streams.get();
 58  0
         if( null != input )
 59  
         {
 60  0
             input.close();
 61  
         }
 62  0
     }
 63  
 
 64  
     /**
 65  
      * Read byte from stream associated with current thread.
 66  
      *
 67  
      * @return the byte read from stream
 68  
      * @throws IOException if an error occurs
 69  
      */
 70  
     @Override
 71  
     public int read()
 72  
         throws IOException
 73  
     {
 74  62
         final InputStream input = m_streams.get();
 75  63
         if( null != input )
 76  
         {
 77  67
             return input.read();
 78  
         }
 79  
         else
 80  
         {
 81  0
             return EOF;
 82  
         }
 83  
     }
 84  
 }