001: /*
002:
003: Copyright 2004, Martian Software, Inc.
004:
005: Licensed under the Apache License, Version 2.0 (the "License");
006: you may not use this file except in compliance with the License.
007: You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016:
017: */
018:
019: package com.martiansoftware.nailgun;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: /**
025: * The class name is pretty descriptive. This creates an InputStream
026: * much like a FilterInputStream, but with the wrapped InputStream
027: * being local to the current Thread. By setting System.in to a
028: * ThreadLocalInputStream, different Threads can read from different
029: * InputStreams simply by using System.in. Of course, the init()
030: * method must be called by the Thread that wishes to use the
031: * wrapped stream.
032: *
033: * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
034: */
035: class ThreadLocalInputStream extends InputStream {
036:
037: /**
038: * The InputStreams for the various threads
039: */
040: private InheritableThreadLocal streams = null;
041:
042: private InputStream defaultInputStream = null;
043:
044: /**
045: * @param defaultInputStream the InputStream that will be used if the
046: * current thread has not called init()
047: */
048: ThreadLocalInputStream(InputStream defaultInputStream) {
049: super ();
050: streams = new InheritableThreadLocal();
051: this .defaultInputStream = defaultInputStream;
052: init(null);
053: }
054:
055: /**
056: * Sets the InputStream for the current thread
057: * @param streamForCurrentThread the InputStream for the current thread
058: */
059: void init(InputStream streamForCurrentThread) {
060: streams.set(streamForCurrentThread);
061: }
062:
063: /**
064: * Returns this thread's InputStream
065: * @return this thread's InputStream
066: */
067: InputStream getInputStream() {
068: InputStream result = (InputStream) streams.get();
069: return ((result == null) ? defaultInputStream : result);
070: }
071:
072: // BEGIN delegated java.io.InputStream methods
073:
074: /**
075: * @see java.io.InputStream#available()
076: */
077: public int available() throws IOException {
078: return (getInputStream().available());
079: }
080:
081: /**
082: * @see java.io.InputStream#close()
083: */
084: public void close() throws IOException {
085: getInputStream().close();
086: }
087:
088: /**
089: * @see java.io.InputStream#mark(int)
090: */
091: public void mark(int readlimit) {
092: getInputStream().mark(readlimit);
093: }
094:
095: /**
096: * @see java.io.InputStream#markSupported()
097: */
098: public boolean markSupported() {
099: return (getInputStream().markSupported());
100: }
101:
102: /**
103: * @see java.io.InputStream#read()
104: */
105: public int read() throws IOException {
106: return (getInputStream().read());
107: }
108:
109: /**
110: * @see java.io.InputStream#read(byte[])
111: */
112: public int read(byte[] b) throws IOException {
113: 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: return (getInputStream().read(b, off, len));
121: }
122:
123: /**
124: * @see java.io.InputStream#reset()
125: */
126: public void reset() throws IOException {
127: getInputStream().reset();
128: }
129:
130: /**
131: * @see java.io.InputStream#skip(long)
132: */
133: public long skip(long n) throws IOException {
134: 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: }
|