KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > update > StreamPumper


1 /*
2  * Copyright 2000,2002-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  */

17
18 package com.openedit.modules.update;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 /**
27  * This class was swiped from ant.
28  */

29 public class StreamPumper extends Thread JavaDoc
30 {
31     private BufferedReader JavaDoc din;
32     private boolean endOfStream = false;
33     private int SLEEP_TIME = 5;
34     private OutputStream JavaDoc fieldOutputStream;
35
36     public StreamPumper( InputStream JavaDoc is, OutputStream JavaDoc os )
37     {
38         this.din = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( is ) );
39         fieldOutputStream = os;
40     }
41
42     public void pumpStream() throws IOException JavaDoc
43     {
44         if (!endOfStream)
45         {
46             String JavaDoc line = din.readLine();
47
48             if (line != null)
49             {
50                 getOutputStream().write( line.getBytes() );
51
52             }
53             else
54             {
55                 endOfStream = true;
56             }
57         }
58     }
59
60     public void run()
61     {
62         try
63         {
64             try
65             {
66                 while( !endOfStream )
67                 {
68                     pumpStream();
69                     sleep( SLEEP_TIME );
70                 }
71             }
72             catch( InterruptedException JavaDoc ie )
73             {
74                 //ignore
75
}
76             din.close();
77         }
78         catch( IOException JavaDoc ioe )
79         {
80             // ignore
81
}
82     }
83
84     protected OutputStream JavaDoc getOutputStream()
85     {
86         return fieldOutputStream;
87     }
88 }
Popular Tags