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