| Line | Hits | Source |
|---|---|---|
| 1 | /* | |
| 2 | ||
| 3 | Copyright 2004, Martian Software, Inc. | |
| 4 | ||
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); | |
| 6 | you may not use this file except in compliance with the License. | |
| 7 | 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 | */ | |
| 18 | ||
| 19 | package com.martiansoftware.nailgun; | |
| 20 | ||
| 21 | import java.io.IOException; | |
| 22 | import java.io.InputStream; | |
| 23 | ||
| 24 | /** | |
| 25 | * The class name is pretty descriptive. This creates an InputStream | |
| 26 | * much like a FilterInputStream, but with the wrapped InputStream | |
| 27 | * being local to the current Thread. By setting System.in to a | |
| 28 | * ThreadLocalInputStream, different Threads can read from different | |
| 29 | * InputStreams simply by using System.in. Of course, the init() | |
| 30 | * method must be called by the Thread that wishes to use the | |
| 31 | * wrapped stream. | |
| 32 | * | |
| 33 | * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a> | |
| 34 | */ | |
| 35 | class ThreadLocalInputStream extends InputStream { | |
| 36 | ||
| 37 | /** | |
| 38 | * The InputStreams for the various threads | |
| 39 | */ | |
| 40 | 0 | private InheritableThreadLocal streams = null; |
| 41 | ||
| 42 | 0 | private InputStream defaultInputStream = null; |
| 43 | ||
| 44 | /** | |
| 45 | * @param defaultInputStream the InputStream that will be used if the | |
| 46 | * current thread has not called init() | |
| 47 | */ | |
| 48 | ThreadLocalInputStream(InputStream defaultInputStream) { | |
| 49 | 0 | super(); |
| 50 | 0 | streams = new InheritableThreadLocal(); |
| 51 | 0 | this.defaultInputStream = defaultInputStream; |
| 52 | 0 | init(null); |
| 53 | 0 | } |
| 54 | ||
| 55 | /** | |
| 56 | * Sets the InputStream for the current thread | |
| 57 | * @param streamForCurrentThread the InputStream for the current thread | |
| 58 | */ | |
| 59 | void init(InputStream streamForCurrentThread) { | |
| 60 | 0 | streams.set(streamForCurrentThread); |
| 61 | 0 | } |
| 62 | ||
| 63 | /** | |
| 64 | * Returns this thread's InputStream | |
| 65 | * @return this thread's InputStream | |
| 66 | */ | |
| 67 | InputStream getInputStream() { | |
| 68 | 0 | InputStream result = (InputStream) streams.get(); |
| 69 | 0 | return ((result == null) ? defaultInputStream : result); |
| 70 | } | |
| 71 | ||
| 72 | // BEGIN delegated java.io.InputStream methods | |
| 73 | ||
| 74 | /** | |
| 75 | * @see java.io.InputStream#available() | |
| 76 | */ | |
| 77 | public int available() throws IOException { | |
| 78 | 0 | return (getInputStream().available()); |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * @see java.io.InputStream#close() | |
| 83 | */ | |
| 84 | public void close() throws IOException { | |
| 85 | 0 | getInputStream().close(); |
| 86 | 0 | } |
| 87 | ||
| 88 | /** | |
| 89 | * @see java.io.InputStream#mark(int) | |
| 90 | */ | |
| 91 | public void mark(int readlimit) { | |
| 92 | 0 | getInputStream().mark(readlimit); |
| 93 | 0 | } |
| 94 | ||
| 95 | /** | |
| 96 | * @see java.io.InputStream#markSupported() | |
| 97 | */ | |
| 98 | public boolean markSupported() { | |
| 99 | 0 | return (getInputStream().markSupported()); |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * @see java.io.InputStream#read() | |
| 104 | */ | |
| 105 | public int read() throws IOException { | |
| 106 | 0 | return (getInputStream().read()); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * @see java.io.InputStream#read(byte[]) | |
| 111 | */ | |
| 112 | public int read(byte[] b) throws IOException { | |
| 113 | 0 | return (getInputStream().read(b)); |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * @see java.io.InputStream#read(byte[],int,int) | |
| 118 | */ | |
| 119 | public int read(byte[] b, int off, int len) throws IOException { | |
| 120 | 0 | return (getInputStream().read(b, off, len)); |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * @see java.io.InputStream#reset() | |
| 125 | */ | |
| 126 | public void reset() throws IOException { | |
| 127 | 0 | getInputStream().reset(); |
| 128 | 0 | } |
| 129 | ||
| 130 | /** | |
| 131 | * @see java.io.InputStream#skip(long) | |
| 132 | */ | |
| 133 | public long skip(long n) throws IOException { | |
| 134 | 0 | return (getInputStream().skip(n)); |
| 135 | } | |
| 136 | ||
| 137 | // BEGIN delegated java.io.InputStream methods | |
| 138 | ||
| 139 | // Note: Should java.lang.Object methods be delegated? If not, and | |
| 140 | // someone synchronizes on this stream, processes might be blocked | |
| 141 | // that shouldn't be. It would certainly be stupid to delegate | |
| 142 | // finalize(). Not so clear are hashcode(), equals(), notify(), and | |
| 143 | // the wait() methods. | |
| 144 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |