• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 
22 package org.apache.qetest;
23 
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 
27 /**
28  * Utility class for reading buffered streams on a thread.
29  *
30  * <p>Common usage case:
31  * <pre>
32  * ThreadedStreamReader errReader = new ThreadedStreamReader();
33  * Process p = Runtime.exec(cmdLine, environment);
34  * errReader.setInputStream(
35  *     new BufferedReader(
36  *         new InputStreamReader(proc.getErrorStream()), bufSize));
37  * errReader.start();
38  * proc.waitFor();
39  * errReader.join();
40  * String sysErr = errReader.getBuffer().toString();
41  * </pre>
42  *
43  * @author Shane_Curcuru@lotus.com
44  * @version $Id$
45  */
46 public class ThreadedStreamReader extends Thread
47 {
48 
49     /** Reader we pull data from.  */
50     BufferedReader is = null;
51 
52     /** Buffer we store the data in.  */
53     StringBuffer sb = null;
54 
55     /**
56      * Asks us to capture data from the provided stream.
57      * @param set BufferedReader we should pull data from
58      */
setInputStream(BufferedReader set)59     public void setInputStream(BufferedReader set)
60     {
61         is = set;
62     }
63 
64     /**
65      * Implement Thread.run().
66      * This asks us to start reading data from our setInputStream()
67      * and storing it in our getBuffer() area.  Note you probably
68      * should have called setInputStream() first.
69      */
run()70     public void run()
71     {
72         sb = new StringBuffer();
73         sb.append(READER_START_MARKER);
74 
75         if (null == is)
76         {
77             sb.append("ERROR! setInputStream(null)");
78             sb.append(READER_END_MARKER);
79             return;
80         }
81 
82         String i = null;
83         try
84         {
85             i = is.readLine();
86         }
87         catch (IOException ioe1)
88         {
89             sb.append("ERROR! no data to read, threw: " + ioe1.toString());
90             i = null;
91         }
92 
93         while (i != null)
94         {
95             sb.append(i);
96             // Note: also append the implicit newline that readLine grabbed
97             sb.append('\n');
98 
99             try
100             {
101                 i = is.readLine();
102             }
103             catch (IOException ioe2)
104             {
105                 sb.append("WARNING! readLine() threw: " + ioe2.toString());
106                 i = null;
107             }
108         }
109         sb.append(READER_END_MARKER);
110     }
111 
112     /**
113      * Get the buffer of data we've captured.
114      *
115      * @return our internal StringBuffer; will be null if we
116      * have not been run yet.
117      */
getBuffer()118     public StringBuffer getBuffer()
119     {
120         return sb;
121     }
122 
123     public static final String READER_START_MARKER = "<stream>\n";
124     public static final String READER_END_MARKER = "</stream>\n";
125 
126 }  // end of class ThreadedStreamReader
127 
128