KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > launcher > StreamConnector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.launcher;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23  /**
24  * A class for connecting an OutputStream to an InputStream.
25  *
26  * @author Patrick Luby
27  */

28 public class StreamConnector extends Thread JavaDoc {
29
30     //------------------------------------------------------------------ Fields
31

32     /**
33      * Input stream to read from.
34      */

35     private InputStream JavaDoc is = null;
36
37     /**
38      * Output stream to write to.
39      */

40     private OutputStream JavaDoc os = null;
41
42     //------------------------------------------------------------ Constructors
43

44     /**
45      * Specify the streams that this object will connect in the {@link #run()}
46      * method.
47      *
48      * @param is the InputStream to read from.
49      * @param os the OutputStream to write to.
50      */

51     public StreamConnector(InputStream JavaDoc is, OutputStream JavaDoc os) {
52
53         this.is = is;
54         this.os = os;
55
56     }
57
58     //----------------------------------------------------------------- Methods
59

60     /**
61      * Connect the InputStream and OutputStream objects specified in the
62      * {@link #StreamConnector(InputStream, OutputStream)} constructor.
63      */

64     public void run() {
65
66         // If the InputStream is null, don't do anything
67
if (is == null)
68             return;
69
70         // Connect the streams until the InputStream is unreadable
71
try {
72             int bytesRead = 0;
73             byte[] buf = new byte[4096];
74             while ((bytesRead = is.read(buf)) != -1) {
75                 if (os != null && bytesRead > 0) {
76                     os.write(buf, 0, bytesRead);
77                     os.flush();
78                 }
79                 yield();
80             }
81         } catch (IOException JavaDoc e) {}
82
83     }
84
85 }
86
Popular Tags