01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.process;
06:
07: import java.io.BufferedReader;
08: import java.io.IOException;
09: import java.io.InputStream;
10: import java.io.InputStreamReader;
11: import java.io.OutputStream;
12:
13: /**
14: * A simple thread that copies one stream to another. Useful for copying a process's output/error streams to this
15: * process's output/error streams.
16: */
17: public class StreamCopier extends Thread {
18:
19: protected final OutputStream out;
20: private final BufferedReader reader;
21:
22: private final String identifier;
23:
24: public StreamCopier(InputStream stream, OutputStream out) {
25: this (stream, out, null);
26: }
27:
28: public StreamCopier(InputStream stream, OutputStream out,
29: String identifier) {
30: if ((stream == null) || (out == null)) {
31: throw new AssertionError("null streams not allowed");
32: }
33:
34: reader = new BufferedReader(new InputStreamReader(stream));
35: this .out = out;
36:
37: this .identifier = identifier;
38:
39: setName("Stream Copier");
40: setDaemon(true);
41: }
42:
43: public void run() {
44: String line;
45: try {
46: while ((line = reader.readLine()) != null) {
47: if (identifier != null) {
48: line = identifier + line;
49: }
50: line += System.getProperty("line.separator", "\n");
51: out.write(line.getBytes());
52: out.flush();
53: }
54: } catch (IOException ioe) {
55: ioe.printStackTrace();
56: }
57: }
58:
59: }
|