001: /*
002: * Copyright (c) 2006 Timothy Wall, All Rights Reserved
003: */
004: package abbot.util;
005:
006: import java.io.PrintStream;
007:
008: /** Splits a {@link PrintStream} into two targets. Useful for making
009: * a copy of stream go somewhere else, such as redirecting stdout/stderr
010: * to a file.
011: */
012: public class Tee extends PrintStream {
013: private PrintStream tee;
014: private PrintStream tee2;
015: private boolean splitting;
016:
017: public Tee(PrintStream p1, PrintStream p2) {
018: super (p1);
019: tee2 = p1;
020: tee = p2;
021: }
022:
023: public synchronized void write(int b) {
024: if (splitting) {
025: super .write(b);
026: } else {
027: splitting = true;
028: super .write(b);
029: tee.write(b);
030: }
031: }
032:
033: public synchronized void write(byte[] b, int o, int l) {
034: if (splitting) {
035: super .write(b, o, l);
036: } else {
037: splitting = true;
038: super .write(b, o, l);
039: tee.write(b, o, l);
040: splitting = false;
041: }
042: }
043:
044: public synchronized void flush() {
045: super .flush();
046: tee.flush();
047: }
048:
049: public synchronized void close() {
050: super .close();
051: tee.close();
052: }
053:
054: public synchronized void print(boolean b) {
055: if (splitting) {
056: super .print(b);
057: } else {
058: splitting = true;
059: super .print(b);
060: tee.print(b);
061: splitting = false;
062: }
063: }
064:
065: public synchronized void print(char b) {
066: if (splitting) {
067: super .print(b);
068: } else {
069: splitting = true;
070: super .print(b);
071: tee.print(b);
072: splitting = false;
073: }
074: }
075:
076: public synchronized void print(int b) {
077: if (splitting) {
078: super .print(b);
079: } else {
080: splitting = true;
081: super .print(b);
082: tee.print(b);
083: splitting = false;
084: }
085: }
086:
087: public synchronized void print(long b) {
088: if (splitting) {
089: super .print(b);
090: } else {
091: splitting = true;
092: super .print(b);
093: tee.print(b);
094: splitting = false;
095: }
096: }
097:
098: public synchronized void print(float b) {
099: if (splitting) {
100: super .print(b);
101: } else {
102: splitting = true;
103: super .print(b);
104: tee.print(b);
105: splitting = false;
106: }
107: }
108:
109: public synchronized void print(double b) {
110: if (splitting) {
111: super .print(b);
112: } else {
113: splitting = true;
114: super .print(b);
115: tee.print(b);
116: splitting = false;
117: }
118: }
119:
120: public synchronized void print(char[] b) {
121: if (splitting) {
122: super .print(b);
123: } else {
124: splitting = true;
125: super .print(b);
126: tee.print(b);
127: splitting = false;
128: }
129: }
130:
131: public synchronized void print(String b) {
132: if (splitting) {
133: super .print(b);
134: } else {
135: splitting = true;
136: super .print(b);
137: tee.print(b);
138: splitting = false;
139: }
140: }
141:
142: public synchronized void print(Object b) {
143: if (splitting) {
144: super .print(b);
145: } else {
146: splitting = true;
147: super .print(b);
148: tee.print(b);
149: splitting = false;
150: }
151: }
152:
153: public synchronized void println() {
154: if (splitting) {
155: super .println();
156: } else {
157: splitting = true;
158: super .println();
159: tee.println();
160: splitting = false;
161: }
162: }
163:
164: public synchronized void println(boolean x) {
165: if (splitting) {
166: super .println(x);
167: } else {
168: splitting = true;
169: super .println(x);
170: tee.println(x);
171: splitting = false;
172: }
173: }
174:
175: public synchronized void println(char x) {
176: if (splitting) {
177: super .println(x);
178: } else {
179: splitting = true;
180: super .println(x);
181: tee.println(x);
182: splitting = false;
183: }
184: }
185:
186: public synchronized void println(int x) {
187: if (splitting) {
188: super .println(x);
189: } else {
190: splitting = true;
191: super .println(x);
192: tee.println(x);
193: splitting = false;
194: }
195: }
196:
197: public synchronized void println(long x) {
198: if (splitting) {
199: super .println(x);
200: } else {
201: splitting = true;
202: super .println(x);
203: tee.println(x);
204: splitting = false;
205: }
206: }
207:
208: public synchronized void println(float x) {
209: if (splitting) {
210: super .println(x);
211: } else {
212: splitting = true;
213: super .println(x);
214: tee.println(x);
215: splitting = false;
216: }
217: }
218:
219: public synchronized void println(double x) {
220: if (splitting) {
221: super .println(x);
222: } else {
223: splitting = true;
224: super .println(x);
225: tee.println(x);
226: splitting = false;
227: }
228: }
229:
230: public synchronized void println(byte[] x) {
231: if (splitting) {
232: super .println(x);
233: } else {
234: splitting = true;
235: super .println(x);
236: tee.println(x);
237: splitting = false;
238: }
239: }
240:
241: public synchronized void println(String x) {
242: if (splitting) {
243: super .println(x);
244: } else {
245: splitting = true;
246: super .println(x);
247: tee.println(x);
248: splitting = false;
249: }
250: }
251:
252: public synchronized void println(Object x) {
253: if (splitting) {
254: super .println(x);
255: } else {
256: splitting = true;
257: super .println(x);
258: tee.println(x);
259: splitting = false;
260: }
261: }
262:
263: public String toString() {
264: return "Tee " + tee2 + " and " + tee;
265: }
266: }
|