Coverage details for com.martiansoftware.nailgun.ThreadLocalPrintStream

LineHitsSource
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.PrintStream;
23  
24 /**
25  * The class name is pretty descriptive. This creates a PrintStream
26  * much like a FilterOutputStream, but with the wrapped PrintStream
27  * being local to the current Thread. By setting System.out to a
28  * ThreadLocalPrintStream, different Threads can write to different
29  * PrintStreams simply by using System.out. 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 ThreadLocalPrintStream extends PrintStream {
36  
37     /**
38      * The PrintStreams for the various threads
39      */
400    private InheritableThreadLocal streams = null;
41  
420    private PrintStream defaultPrintStream = null;
43     
44     /**
45      * Creates a new InheritedThreadLocalPrintStream
46      * @param defaultPrintStream the PrintStream that will be used if the
47      * current thread has not called init()
48      */
49     public ThreadLocalPrintStream(PrintStream defaultPrintStream) {
500        super(defaultPrintStream);
510        streams = new InheritableThreadLocal();
520        this.defaultPrintStream = defaultPrintStream;
530        init(null);
540    }
55  
56     /**
57      * Sets the PrintStream for the current thread
58      * @param streamForCurrentThread the PrintStream for the current thread
59      */
60     void init(PrintStream streamForCurrentThread) {
610        streams.set(streamForCurrentThread);
620    }
63  
64     /**
65      * Returns this thread's PrintStream
66      * @return this thread's PrintStream
67      */
68     PrintStream getPrintStream() {
690        PrintStream result = (PrintStream) streams.get();
700        return ((result == null) ? defaultPrintStream : result);
71     }
72  
73 // BEGIN delegated java.io.PrintStream methods
74  
75     /**
76      * @see java.io.PrintStream#checkError()
77      */
78     public boolean checkError() {
790        return (getPrintStream().checkError());
80     }
81  
82     /**
83      * @see java.io.PrintStream#close()
84      */
85     public void close() {
860        getPrintStream().close();
870    }
88  
89     /**
90      * @see java.io.PrintStream#flush()
91      */
92     public void flush() {
930        getPrintStream().flush();
940    }
95  
96     /**
97      * @see java.io.PrintStream#print(boolean)
98      */
99     public void print(boolean b) {
1000        getPrintStream().print(b);
1010    }
102  
103     /**
104      * @see java.io.PrintStream#print(char)
105      */
106     public void print(char c) {
1070        getPrintStream().print(c);
1080    }
109  
110     /**
111      * @see java.io.PrintStream#print(char[])
112      */
113     public void print(char[] s) {
1140        getPrintStream().print(s);
1150    }
116  
117     /**
118      * @see java.io.PrintStream#print(double)
119      */
120     public void print(double d) {
1210        getPrintStream().print(d);
1220    }
123  
124     /**
125      * @see java.io.PrintStream#print(float)
126      */
127     public void print(float f) {
1280        getPrintStream().print(f);
1290    }
130  
131     /**
132      * @see java.io.PrintStream#print(int)
133      */
134     public void print(int i) {
1350        getPrintStream().print(i);
1360    }
137  
138     /**
139      * @see java.io.PrintStream#print(long)
140      */
141     public void print(long l) {
1420        getPrintStream().print(l);
1430    }
144  
145     /**
146      * @see java.io.PrintStream#print(Object)
147      */
148     public void print(Object obj) {
1490        getPrintStream().print(obj);
1500    }
151  
152     /**
153      * @see java.io.PrintStream#print(String)
154      */
155     public void print(String s) {
1560        getPrintStream().print(s);
1570    }
158  
159     /**
160      * @see java.io.PrintStream#println()
161      */
162     public void println() {
1630        getPrintStream().println();
1640    }
165  
166     /**
167      * @see java.io.PrintStream#println(boolean)
168      */
169     public void println(boolean x) {
1700        getPrintStream().println(x);
1710    }
172  
173     /**
174      * @see java.io.PrintStream#println(char)
175      */
176     public void println(char x) {
1770        getPrintStream().println(x);
1780    }
179  
180     /**
181      * @see java.io.PrintStream#println(char[])
182      */
183     public void println(char[] x) {
1840        getPrintStream().println(x);
1850    }
186  
187     /**
188      * @see java.io.PrintStream#println(double)
189      */
190     public void println(double x) {
1910        getPrintStream().println(x);
1920    }
193  
194     /**
195      * @see java.io.PrintStream#println(float)
196      */
197     public void println(float x) {
1980        getPrintStream().println(x);
1990    }
200  
201     /**
202      * @see java.io.PrintStream#println(int)
203      */
204     public void println(int x) {
2050        getPrintStream().println(x);
2060    }
207  
208     /**
209      * @see java.io.PrintStream#println(long)
210      */
211     public void println(long x) {
2120        getPrintStream().println(x);
2130    }
214  
215     /**
216      * @see java.io.PrintStream#println(Object)
217      */
218     public void println(Object x) {
2190        getPrintStream().println(x);
2200    }
221  
222     /**
223      * @see java.io.PrintStream#println(String)
224      */
225     public void println(String x) {
2260        getPrintStream().println(x);
2270    }
228  
229     /**
230      * @see java.io.PrintStream#write(byte[],int,int)
231      */
232     public void write(byte[] buf, int off, int len) {
2330        getPrintStream().write(buf, off, len);
2340    }
235  
236     /**
237      * @see java.io.PrintStream#write(int)
238      */
239     public void write(int b) {
2400        getPrintStream().write(b);
2410    }
242  
243 // END delegated java.io.PrintStream methods
244  
245 // BEGIN delegated java.io.FilterOutputStream methods
246  
247     /**
248      * @see java.io.FilterOutputStream#write(byte[])
249      */
250     public void write(byte[] b) throws IOException {
2510        getPrintStream().write(b);
2520    }
253  
254 // END delegated java.io.FilterOutputStream methods
255  
256 // Note: Should java.lang.Object methods be delegated? If not, and
257 // someone synchronizes on this stream, processes might be blocked
258 // that shouldn't be. It would certainly be stupid to delegate
259 // finalize(). Not so clear are hashcode(), equals(), notify(), and
260 // the wait() methods.
261 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.