KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > StreamPumper


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

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 /**
26  * Copies all data from an input stream to an output stream.
27  *
28  * @since Ant 1.2
29  */

30 public class StreamPumper implements Runnable JavaDoc {
31
32     private InputStream JavaDoc is;
33     private OutputStream JavaDoc os;
34     private volatile boolean finish;
35     private volatile boolean finished;
36     private boolean closeWhenExhausted;
37     private boolean autoflush = false;
38     private Exception JavaDoc exception = null;
39     private int bufferSize = 128;
40     private boolean started = false;
41
42     /**
43      * Create a new stream pumper.
44      *
45      * @param is input stream to read data from
46      * @param os output stream to write data to.
47      * @param closeWhenExhausted if true, the output stream will be closed when
48      * the input is exhausted.
49      */

50     public StreamPumper(InputStream JavaDoc is, OutputStream JavaDoc os,
51                         boolean closeWhenExhausted) {
52         this.is = is;
53         this.os = os;
54         this.closeWhenExhausted = closeWhenExhausted;
55     }
56
57     /**
58      * Create a new stream pumper.
59      *
60      * @param is input stream to read data from
61      * @param os output stream to write data to.
62      */

63     public StreamPumper(InputStream JavaDoc is, OutputStream JavaDoc os) {
64         this(is, os, false);
65     }
66
67     /**
68      * Set whether data should be flushed through to the output stream.
69      * @param autoflush if true, push through data; if false, let it be buffered
70      * @since Ant 1.6.3
71      */

72     /*package*/ void setAutoflush(boolean autoflush) {
73         this.autoflush = autoflush;
74     }
75
76     /**
77      * Copies data from the input stream to the output stream.
78      *
79      * Terminates as soon as the input stream is closed or an error occurs.
80      */

81     public void run() {
82         synchronized (this) {
83             started = true;
84         }
85         finished = false;
86         finish = false;
87
88         final byte[] buf = new byte[bufferSize];
89
90         int length;
91         try {
92             while ((length = is.read(buf)) > 0 && !finish) {
93                 os.write(buf, 0, length);
94                 if (autoflush) {
95                     os.flush();
96                 }
97             }
98             os.flush();
99         } catch (Exception JavaDoc e) {
100             synchronized (this) {
101                 exception = e;
102             }
103         } finally {
104             if (closeWhenExhausted) {
105                 try {
106                     os.close();
107                 } catch (IOException JavaDoc e) {
108                     // ignore
109
}
110             }
111             finished = true;
112             synchronized (this) {
113                 notifyAll();
114             }
115         }
116     }
117
118     /**
119      * Tells whether the end of the stream has been reached.
120      * @return true is the stream has been exhausted.
121      */

122     public boolean isFinished() {
123         return finished;
124     }
125
126     /**
127      * This method blocks until the stream pumper finishes.
128      * @throws InterruptedException if interrupted.
129      * @see #isFinished()
130      */

131     public synchronized void waitFor()
132         throws InterruptedException JavaDoc {
133         while (!isFinished()) {
134             wait();
135         }
136     }
137
138     /**
139      * Set the size in bytes of the read buffer.
140      * @param bufferSize the buffer size to use.
141      * @throws IllegalStateException if the StreamPumper is already running.
142      */

143     public synchronized void setBufferSize(int bufferSize) {
144         if (started) {
145             throw new IllegalStateException JavaDoc(
146                 "Cannot set buffer size on a running StreamPumper");
147         }
148         this.bufferSize = bufferSize;
149     }
150
151     /**
152      * Get the size in bytes of the read buffer.
153      * @return the int size of the read buffer.
154      */

155     public synchronized int getBufferSize() {
156         return bufferSize;
157     }
158
159     /**
160      * Get the exception encountered, if any.
161      * @return the Exception encountered.
162      */

163     public synchronized Exception JavaDoc getException() {
164         return exception;
165     }
166
167     /**
168      * Stop the pumper as soon as possible.
169      * Note that it may continue to block on the input stream
170      * but it will really stop the thread as soon as it gets EOF
171      * or any byte, and it will be marked as finished.
172      * @since Ant 1.6.3
173      */

174     /*package*/ synchronized void stop() {
175         finish = true;
176         notifyAll();
177     }
178 }
179
Popular Tags