xref: /AOO42X/test/testcommon/source/org/openoffice/test/common/StreamPump.java (revision a622bb93879b5dcf37e96471f1fc02fd35c09661)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package org.openoffice.test.common;
23 
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 
28 /**
29  * Pump data from input stream into a StringBuffer
30  *
31  */
32 public class StreamPump extends Thread {
33     StringBuffer stringBuffer = null;
34     InputStream inputStream = null;
35 
36     public StreamPump(StringBuffer stringBuffer, InputStream inputStream) {
37         this.stringBuffer = stringBuffer;
38         this.inputStream = inputStream;
39     }
40 
41     public void run() {
42         InputStreamReader reader = null;
43         try {
44             reader = new InputStreamReader(inputStream);
45             char[] buf = new char[1024];
46             int count = 0;
47             while ((count = reader.read(buf)) != -1) {
48                 // If we need collect the output
49                 if (stringBuffer != null)
50                     stringBuffer.append(buf, 0, count);
51             }
52         } catch (IOException e) {
53             // ignore.
54         } finally {
55             if (reader != null)
56                 try {
57                     reader.close();
58                 } catch (IOException e) {
59                     // ignore
60                 }
61         }
62     }
63 }