01: package com.tagtraum.framework.compiler;
02:
03: import java.io.OutputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06:
07: /**
08: * Pump streams so that there is no blocking.
09: *
10: * Date: 31.03.2002
11: * Time: 09:33:16
12: * @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
13: * @version 1.1beta1 $Id: StreamPumper.java,v 1.3 2003/07/10 00:52:31 hendriks73 Exp $
14: */
15: class StreamPumper extends Thread {
16:
17: private boolean endOfStream = false;
18: private OutputStream out;
19: private InputStream in;
20: private byte[] buf;
21:
22: public StreamPumper(InputStream in) {
23: this (in, null);
24: }
25:
26: public StreamPumper(InputStream in, OutputStream out) {
27: this .in = in;
28: this .out = out;
29: buf = new byte[512];
30: }
31:
32: public void pump() throws IOException {
33: if (!endOfStream) {
34: int bytesRead = in.read(buf, 0, 512);
35:
36: if (bytesRead > 0 && out != null) {
37: out.write(buf, 0, bytesRead);
38: } else if (bytesRead == -1) {
39: endOfStream = true;
40: }
41: }
42: }
43:
44: public void run() {
45: try {
46: while (!endOfStream) {
47: pump();
48: sleep(5);
49: }
50: } catch (InterruptedException ie) {
51: // ignore
52: } catch (IOException ioe) {
53: // ignore
54: }
55: }
56:
57: }
|