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  
  */
 29  2
 public class DemuxInputStream
 30  
     extends InputStream
 31  
 {
 32  2
     private final InheritableThreadLocal<InputStream> m_streams = new InheritableThreadLocal<>();
 33  
 
 34  
     /**
 35  
      * Bind the specified stream to the current thread.
 36  
      *
 37  
      * @param input the stream to bind
 38  
      * @return the InputStream that was previously active
 39  
      */
 40  
     public InputStream bindStream( final InputStream input )
 41  
     {
 42  8
         final InputStream oldValue = m_streams.get();
 43  8
         m_streams.set( input );
 44  8
         return oldValue;
 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  0
         final InputStream input = m_streams.get();
 57  0
         if( null != input )
 58  
         {
 59  0
             input.close();
 60  
         }
 61  0
     }
 62  
 
 63  
     /**
 64  
      * Read byte from stream associated with current thread.
 65  
      *
 66  
      * @return the byte read from stream
 67  
      * @throws IOException if an error occurs
 68  
      */
 69  
     @Override
 70  
     public int read()
 71  
         throws IOException
 72  
     {
 73  131
         final InputStream input = m_streams.get();
 74  136
         if( null != input )
 75  
         {
 76  134
             return input.read();
 77  
         }
 78  
         else
 79  
         {
 80  0
             return EOF;
 81  
         }
 82  
     }
 83  
 }