01: package tide.utils;
02:
03: import java.io.*;
04: import snow.texteditor.SimpleDocument;
05:
06: /** A stream gobbler that writes in the given document
07: */
08: public class StreamReader extends Thread {
09: final InputStream input;
10: final SimpleDocument doc;
11: final String prefix;
12: final boolean isErrorStream;
13:
14: /** @param doc if null, uses System.out and System.err
15: */
16: public StreamReader(String prefix, InputStream input,
17: SimpleDocument doc, boolean isErrorStream, String descr) {
18: this .input = input;
19: this .doc = doc;
20: this .prefix = prefix;
21: this .isErrorStream = isErrorStream;
22: setName("StreamReader (" + descr + ")");
23: }
24:
25: @Override
26: public void run() {
27: try {
28: final InputStreamReader isr = new InputStreamReader(
29: this .input);
30: final BufferedReader br = new BufferedReader(isr);
31: String line = null;
32: while ((line = br.readLine()) != null) {
33: if (doc == null) {
34: if (isErrorStream) {
35: System.err.println(line);
36: } else {
37: System.out.println(line);
38: }
39: } else {
40: if (isErrorStream) {
41: doc.appendErrorLine(prefix + line);
42: } else {
43: doc.appendLine(prefix + line);
44: }
45: }
46: }
47: } catch (IOException ioe) {
48: ioe.printStackTrace();
49: }
50: } //run
51:
52: }
|