01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.util;
05:
06: import java.io.BufferedReader;
07: import java.io.IOException;
08: import java.io.InputStream;
09: import java.io.InputStreamReader;
10: import java.io.PrintStream;
11:
12: public class ExternalProcessStreamWriter {
13:
14: private volatile IOException exception;
15:
16: public void printSys(final InputStream in) {
17: print(System.out, in);
18: }
19:
20: public void printErr(final InputStream in) {
21: print(System.err, in);
22: }
23:
24: public boolean hasException() {
25: return (exception != null);
26: }
27:
28: public IOException getException() {
29: return exception;
30: }
31:
32: private void print(final PrintStream stream, final InputStream in) {
33: Thread writer = new Thread() {
34: BufferedReader reader = new BufferedReader(
35: new InputStreamReader(in));
36:
37: public void run() {
38: try {
39: String line;
40: while ((line = reader.readLine()) != null) {
41: stream.println(line);
42: }
43: } catch (IOException e) {
44: // connection closed
45: } finally {
46: try {
47: reader.close();
48: } catch (IOException e) {
49: exception = e;
50: }
51: }
52: }
53: };
54: writer.start();
55: }
56: }
|