KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > ThreadLocalInputStream


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 JavaDoc;
22 import java.io.InputStream JavaDoc;
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 JavaDoc {
36
37     /**
38      * The InputStreams for the various threads
39      */

40     private InheritableThreadLocal JavaDoc streams = null;
41
42     private InputStream JavaDoc 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 JavaDoc defaultInputStream) {
49         super();
50         streams = new InheritableThreadLocal JavaDoc();
51         this.defaultInputStream = defaultInputStream;
52         init(null);
53     }
54
55     /**
56      * Sets the InputStream for the current thread
57      * @param streamForCurrentThread the InputStream for the current thread
58      */

59     void init(InputStream JavaDoc streamForCurrentThread) {
60         streams.set(streamForCurrentThread);
61     }
62
63     /**
64      * Returns this thread's InputStream
65      * @return this thread's InputStream
66      */

67     InputStream JavaDoc getInputStream() {
68         InputStream JavaDoc result = (InputStream JavaDoc) streams.get();
69         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 JavaDoc {
78         return (getInputStream().available());
79     }
80
81     /**
82      * @see java.io.InputStream#close()
83      */

84     public void close() throws IOException JavaDoc {
85         getInputStream().close();
86     }
87
88     /**
89      * @see java.io.InputStream#mark(int)
90      */

91     public void mark(int readlimit) {
92         getInputStream().mark(readlimit);
93     }
94
95     /**
96      * @see java.io.InputStream#markSupported()
97      */

98     public boolean markSupported() {
99         return (getInputStream().markSupported());
100     }
101
102     /**
103      * @see java.io.InputStream#read()
104      */

105     public int read() throws IOException JavaDoc {
106         return (getInputStream().read());
107     }
108
109     /**
110      * @see java.io.InputStream#read(byte[])
111      */

112     public int read(byte[] b) throws IOException JavaDoc {
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 JavaDoc {
120         return (getInputStream().read(b, off, len));
121     }
122
123     /**
124      * @see java.io.InputStream#reset()
125      */

126     public void reset() throws IOException JavaDoc {
127         getInputStream().reset();
128     }
129
130     /**
131      * @see java.io.InputStream#skip(long)
132      */

133     public long skip(long n) throws IOException JavaDoc {
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
}
145
Popular Tags