01: /*
02: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
03: *
04: * http://izpack.org/
05: * http://izpack.codehaus.org/
06: *
07: * Licensed under the Apache License, Version 2.0 (the "License");
08: * you may not use this file except in compliance with the License.
09: * You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: package com.izforge.izpack.util;
21:
22: import java.io.BufferedReader;
23: import java.io.BufferedWriter;
24: import java.io.IOException;
25: import java.io.Reader;
26: import java.io.Writer;
27:
28: /**
29: * This is a grabber for stdout and stderr. It will be launched once at command execution end
30: * terminates if the apropriate stream runs out of data.
31: *
32: * @author Olexij Tkatchenko <ot@parcs.de>
33: */
34: public class MonitorInputStream implements Runnable {
35:
36: private BufferedReader reader;
37:
38: private BufferedWriter writer;
39:
40: private boolean shouldStop = false;
41:
42: /**
43: * Construct a new monitor.
44: *
45: * @param in The input to read.
46: * @param out The writer to write to.
47: */
48: public MonitorInputStream(Reader in, Writer out) {
49: this .reader = new BufferedReader(in);
50: this .writer = new BufferedWriter(out);
51: }
52:
53: /**
54: * Request stopping this thread.
55: */
56: public void doStop() {
57: this .shouldStop = true;
58: }
59:
60: /**
61: * {@inheritDoc}
62: */
63: public void run() {
64: try {
65: String line;
66: while ((line = this .reader.readLine()) != null) {
67: this .writer.write(line);
68: this .writer.newLine();
69: this .writer.flush();
70: if (this .shouldStop)
71: return;
72: }
73: } catch (IOException ioe) {
74: ioe.printStackTrace(System.out);
75: }
76: }
77: }
|