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     @Override
     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 }
     93