| 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.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 | */ | |
| 40 | 0 | private InheritableThreadLocal streams = null; |
| 41 | ||
| 42 | 0 | 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) { | |
| 50 | 0 | super(defaultPrintStream); |
| 51 | 0 | streams = new InheritableThreadLocal(); |
| 52 | 0 | this.defaultPrintStream = defaultPrintStream; |
| 53 | 0 | init(null); |
| 54 | 0 | } |
| 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) { | |
| 61 | 0 | streams.set(streamForCurrentThread); |
| 62 | 0 | } |
| 63 | ||
| 64 | /** | |
| 65 | * Returns this thread's PrintStream | |
| 66 | * @return this thread's PrintStream | |
| 67 | */ | |
| 68 | PrintStream getPrintStream() { | |
| 69 | 0 | PrintStream result = (PrintStream) streams.get(); |
| 70 | 0 | 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() { | |
| 79 | 0 | return (getPrintStream().checkError()); |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * @see java.io.PrintStream#close() | |
| 84 | */ | |
| 85 | public void close() { | |
| 86 | 0 | getPrintStream().close(); |
| 87 | 0 | } |
| 88 | ||
| 89 | /** | |
| 90 | * @see java.io.PrintStream#flush() | |
| 91 | */ | |
| 92 | public void flush() { | |
| 93 | 0 | getPrintStream().flush(); |
| 94 | 0 | } |
| 95 | ||
| 96 | /** | |
| 97 | * @see java.io.PrintStream#print(boolean) | |
| 98 | */ | |
| 99 | public void print(boolean b) { | |
| 100 | 0 | getPrintStream().print(b); |
| 101 | 0 | } |
| 102 | ||
| 103 | /** | |
| 104 | * @see java.io.PrintStream#print(char) | |
| 105 | */ | |
| 106 | public void print(char c) { | |
| 107 | 0 | getPrintStream().print(c); |
| 108 | 0 | } |
| 109 | ||
| 110 | /** | |
| 111 | * @see java.io.PrintStream#print(char[]) | |
| 112 | */ | |
| 113 | public void print(char[] s) { | |
| 114 | 0 | getPrintStream().print(s); |
| 115 | 0 | } |
| 116 | ||
| 117 | /** | |
| 118 | * @see java.io.PrintStream#print(double) | |
| 119 | */ | |
| 120 | public void print(double d) { | |
| 121 | 0 | getPrintStream().print(d); |
| 122 | 0 | } |
| 123 | ||
| 124 | /** | |
| 125 | * @see java.io.PrintStream#print(float) | |
| 126 | */ | |
| 127 | public void print(float f) { | |
| 128 | 0 | getPrintStream().print(f); |
| 129 | 0 | } |
| 130 | ||
| 131 | /** | |
| 132 | * @see java.io.PrintStream#print(int) | |
| 133 | */ | |
| 134 | public void print(int i) { | |
| 135 | 0 | getPrintStream().print(i); |
| 136 | 0 | } |
| 137 | ||
| 138 | /** | |
| 139 | * @see java.io.PrintStream#print(long) | |
| 140 | */ | |
| 141 | public void print(long l) { | |
| 142 | 0 | getPrintStream().print(l); |
| 143 | 0 | } |
| 144 | ||
| 145 | /** | |
| 146 | * @see java.io.PrintStream#print(Object) | |
| 147 | */ | |
| 148 | public void print(Object obj) { | |
| 149 | 0 | getPrintStream().print(obj); |
| 150 | 0 | } |
| 151 | ||
| 152 | /** | |
| 153 | * @see java.io.PrintStream#print(String) | |
| 154 | */ | |
| 155 | public void print(String s) { | |
| 156 | 0 | getPrintStream().print(s); |
| 157 | 0 | } |
| 158 | ||
| 159 | /** | |
| 160 | * @see java.io.PrintStream#println() | |
| 161 | */ | |
| 162 | public void println() { | |
| 163 | 0 | getPrintStream().println(); |
| 164 | 0 | } |
| 165 | ||
| 166 | /** | |
| 167 | * @see java.io.PrintStream#println(boolean) | |
| 168 | */ | |
| 169 | public void println(boolean x) { | |
| 170 | 0 | getPrintStream().println(x); |
| 171 | 0 | } |
| 172 | ||
| 173 | /** | |
| 174 | * @see java.io.PrintStream#println(char) | |
| 175 | */ | |
| 176 | public void println(char x) { | |
| 177 | 0 | getPrintStream().println(x); |
| 178 | 0 | } |
| 179 | ||
| 180 | /** | |
| 181 | * @see java.io.PrintStream#println(char[]) | |
| 182 | */ | |
| 183 | public void println(char[] x) { | |
| 184 | 0 | getPrintStream().println(x); |
| 185 | 0 | } |
| 186 | ||
| 187 | /** | |
| 188 | * @see java.io.PrintStream#println(double) | |
| 189 | */ | |
| 190 | public void println(double x) { | |
| 191 | 0 | getPrintStream().println(x); |
| 192 | 0 | } |
| 193 | ||
| 194 | /** | |
| 195 | * @see java.io.PrintStream#println(float) | |
| 196 | */ | |
| 197 | public void println(float x) { | |
| 198 | 0 | getPrintStream().println(x); |
| 199 | 0 | } |
| 200 | ||
| 201 | /** | |
| 202 | * @see java.io.PrintStream#println(int) | |
| 203 | */ | |
| 204 | public void println(int x) { | |
| 205 | 0 | getPrintStream().println(x); |
| 206 | 0 | } |
| 207 | ||
| 208 | /** | |
| 209 | * @see java.io.PrintStream#println(long) | |
| 210 | */ | |
| 211 | public void println(long x) { | |
| 212 | 0 | getPrintStream().println(x); |
| 213 | 0 | } |
| 214 | ||
| 215 | /** | |
| 216 | * @see java.io.PrintStream#println(Object) | |
| 217 | */ | |
| 218 | public void println(Object x) { | |
| 219 | 0 | getPrintStream().println(x); |
| 220 | 0 | } |
| 221 | ||
| 222 | /** | |
| 223 | * @see java.io.PrintStream#println(String) | |
| 224 | */ | |
| 225 | public void println(String x) { | |
| 226 | 0 | getPrintStream().println(x); |
| 227 | 0 | } |
| 228 | ||
| 229 | /** | |
| 230 | * @see java.io.PrintStream#write(byte[],int,int) | |
| 231 | */ | |
| 232 | public void write(byte[] buf, int off, int len) { | |
| 233 | 0 | getPrintStream().write(buf, off, len); |
| 234 | 0 | } |
| 235 | ||
| 236 | /** | |
| 237 | * @see java.io.PrintStream#write(int) | |
| 238 | */ | |
| 239 | public void write(int b) { | |
| 240 | 0 | getPrintStream().write(b); |
| 241 | 0 | } |
| 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 { | |
| 251 | 0 | getPrintStream().write(b); |
| 252 | 0 | } |
| 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. |
copyright © 2003, jcoverage ltd. all rights reserved. |