01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.process;
05:
06: import java.io.BufferedReader;
07: import java.io.BufferedWriter;
08: import java.io.IOException;
09: import java.io.InputStream;
10: import java.io.InputStreamReader;
11: import java.io.OutputStream;
12: import java.io.OutputStreamWriter;
13: import java.io.PrintWriter;
14:
15: public class StreamAppender {
16:
17: private PrintWriter output;
18: private Thread outWriter;
19: private Thread errWriter;
20:
21: public StreamAppender(OutputStream output) {
22: this .output = new PrintWriter(new BufferedWriter(
23: new OutputStreamWriter(output)));
24: }
25:
26: public void writeInput(final InputStream err, final InputStream out) {
27: errWriter = new Thread() {
28: BufferedReader reader = new BufferedReader(
29: new InputStreamReader(err));
30:
31: public void run() {
32: try {
33: String line;
34: while ((line = reader.readLine()) != null) {
35: output.println(line);
36: }
37: } catch (IOException e) {
38: e.printStackTrace();
39: } finally {
40: output.flush();
41: }
42: }
43: };
44:
45: outWriter = new Thread() {
46: BufferedReader reader = new BufferedReader(
47: new InputStreamReader(out));
48:
49: public void run() {
50: try {
51: String line;
52: while ((line = reader.readLine()) != null) {
53: output.println(line);
54: }
55: } catch (IOException e) {
56: e.printStackTrace();
57: } finally {
58: output.flush();
59: }
60: }
61: };
62:
63: errWriter.setDaemon(true);
64: outWriter.setDaemon(true);
65:
66: errWriter.start();
67: outWriter.start();
68: }
69:
70: public void finish() throws Exception {
71: outWriter.join();
72: errWriter.join();
73: }
74:
75: }
|