Home | History | Annotate | Download | only in framework
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     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  */
     22 
     23 /**
     24  * Created on 29.01.2005
     25  */
     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, String name) {
     51         super("Redirector for " + name);
     52         this.name = name;
     53         this.logWriter = logWriter;
     54         InputStreamReader isr = new InputStreamReader(is);
     55         br = new BufferedReader(isr, 1024);
     56         doExit = false;
     57     }
     58 
     59     /**
     60      * Reads all lines from stream and puts them to logWriter.
     61      */
     62     public void run() {
     63         logWriter.println("Redirector started: " + name);
     64         try {
     65             String line = "";
     66             while (!doExit) {
     67                 try {
     68                     line = br.readLine();
     69                     if (line == null)
     70                         break;
     71 
     72                     logWriter.println(name + "> " + line);
     73                 } catch (IllegalStateException e) {
     74                      //logWriter.printError("Illegal state exception! " + e);
     75                     //ignore
     76                 }
     77 
     78             }
     79             logWriter.println("Redirector completed: " + name);
     80         } catch (IOException e) {
     81             logWriter.printError(e);
     82         }
     83     }
     84 
     85     /**
     86      * Notifies redirector to stop stream redirection.
     87      */
     88     public void exit() {
     89         doExit = true;
     90     }
     91 }
     92