| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DemuxInputStream |
|
| 2.0;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 java.io.IOException; | |
| 20 | import java.io.InputStream; | |
| 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: DemuxInputStream.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 27 | */ | |
| 28 | 1 | public class DemuxInputStream |
| 29 | extends InputStream | |
| 30 | { | |
| 31 | 1 | private final InheritableThreadLocal<InputStream> m_streams = new InheritableThreadLocal<InputStream>(); |
| 32 | ||
| 33 | /** | |
| 34 | * Bind the specified stream to the current thread. | |
| 35 | * | |
| 36 | * @param input the stream to bind | |
| 37 | * @return the InputStream that was previously active | |
| 38 | */ | |
| 39 | public InputStream bindStream( final InputStream input ) | |
| 40 | { | |
| 41 | 4 | final InputStream oldValue = m_streams.get(); |
| 42 | 4 | m_streams.set( input ); |
| 43 | 4 | return oldValue; |
| 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 InputStream input = m_streams.get(); |
| 56 | 0 | if( null != input ) |
| 57 | { | |
| 58 | 0 | input.close(); |
| 59 | } | |
| 60 | 0 | } |
| 61 | ||
| 62 | /** | |
| 63 | * Read byte from stream associated with current thread. | |
| 64 | * | |
| 65 | * @return the byte read from stream | |
| 66 | * @throws IOException if an error occurs | |
| 67 | */ | |
| 68 | @Override | |
| 69 | public int read() | |
| 70 | throws IOException | |
| 71 | { | |
| 72 | 68 | final InputStream input = m_streams.get(); |
| 73 | 68 | if( null != input ) |
| 74 | { | |
| 75 | 68 | return input.read(); |
| 76 | } | |
| 77 | else | |
| 78 | { | |
| 79 | 0 | return -1; |
| 80 | } | |
| 81 | } | |
| 82 | } |