01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: *
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: /**
20: * @author Vitaly A. Provodin
21: * @version $Revision: 1.3 $
22: */
23:
24: /**
25: * Created on 29.01.2005
26: */package org.apache.harmony.jpda.tests.framework;
27:
28: import java.io.InputStreamReader;
29: import java.io.BufferedReader;
30: import java.io.InputStream;
31: import java.io.IOException;
32:
33: /**
34: * <p>This class provides redirection of debuggee output and error streams to logWriter.
35: */
36: public class StreamRedirector extends Thread {
37:
38: String name;
39: LogWriter logWriter;
40: BufferedReader br;
41: boolean doExit;
42:
43: /**
44: * Creates new redirector for the specified stream.
45: *
46: * @param is stream to be redirected
47: * @param logWriter logWriter to redirect stream to
48: * @param name stream name used as prefix for redirected output
49: */
50: public StreamRedirector(InputStream is, LogWriter logWriter,
51: String name) {
52: super ("Redirector for " + name);
53: this .name = name;
54: this .logWriter = logWriter;
55: InputStreamReader isr = new InputStreamReader(is);
56: br = new BufferedReader(isr, 1024);
57: doExit = false;
58: }
59:
60: /**
61: * Reads all lines from stream and puts them to logWriter.
62: */
63: public void run() {
64: logWriter.println("Redirector started: " + name);
65: try {
66: String line = "";
67: while (!doExit) {
68: try {
69: line = br.readLine();
70: if (line == null)
71: break;
72:
73: logWriter.println(name + "> " + line);
74: } catch (IllegalStateException e) {
75: //logWriter.printError("Illegal state exception! " + e);
76: //ignore
77: }
78:
79: }
80: logWriter.println("Redirector completed: " + name);
81: } catch (IOException e) {
82: logWriter.printError(e);
83: }
84: }
85:
86: /**
87: * Notifies redirector to stop stream redirection.
88: */
89: public void exit() {
90: doExit = true;
91: }
92: }
|