1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 25 31 public class PumpStreamHandler implements ExecuteStreamHandler { 32 33 private Thread outputThread; 34 private Thread errorThread; 35 private StreamPumper inputPump; 36 37 private OutputStream out; 38 private OutputStream err; 39 private InputStream input; 40 41 47 public PumpStreamHandler(OutputStream out, OutputStream err, 48 InputStream input) { 49 this.out = out; 50 this.err = err; 51 this.input = input; 52 } 53 54 59 public PumpStreamHandler(OutputStream out, OutputStream err) { 60 this(out, err, null); 61 } 62 63 67 public PumpStreamHandler(OutputStream outAndErr) { 68 this(outAndErr, outAndErr); 69 } 70 71 74 public PumpStreamHandler() { 75 this(System.out, System.err); 76 } 77 78 83 public void setProcessOutputStream(InputStream is) { 84 createProcessOutputPump(is, out); 85 } 86 87 92 public void setProcessErrorStream(InputStream is) { 93 if (err != null) { 94 createProcessErrorPump(is, err); 95 } 96 } 97 98 103 public void setProcessInputStream(OutputStream os) { 104 if (input != null) { 105 inputPump = createInputPump(input, os, true); 106 } else { 107 try { 108 os.close(); 109 } catch (IOException e) { 110 } 112 } 113 } 114 115 118 public void start() { 119 outputThread.start(); 120 errorThread.start(); 121 if (inputPump != null) { 122 Thread inputThread = new Thread (inputPump); 123 inputThread.setDaemon(true); 124 inputThread.start(); 125 } 126 } 127 128 131 public void stop() { 132 try { 133 outputThread.join(); 134 } catch (InterruptedException e) { 135 } 137 try { 138 errorThread.join(); 139 } catch (InterruptedException e) { 140 } 142 143 if (inputPump != null) { 144 inputPump.stop(); 145 } 146 147 try { 148 err.flush(); 149 } catch (IOException e) { 150 } 152 try { 153 out.flush(); 154 } catch (IOException e) { 155 } 157 } 158 159 163 protected OutputStream getErr() { 164 return err; 165 } 166 167 171 protected OutputStream getOut() { 172 return out; 173 } 174 175 180 protected void createProcessOutputPump(InputStream is, OutputStream os) { 181 outputThread = createPump(is, os); 182 } 183 184 189 protected void createProcessErrorPump(InputStream is, OutputStream os) { 190 errorThread = createPump(is, os); 191 } 192 193 200 protected Thread createPump(InputStream is, OutputStream os) { 201 return createPump(is, os, false); 202 } 203 204 212 protected Thread createPump(InputStream is, OutputStream os, 213 boolean closeWhenExhausted) { 214 final Thread result 215 = new Thread (new StreamPumper(is, os, closeWhenExhausted)); 216 result.setDaemon(true); 217 return result; 218 } 219 220 225 StreamPumper createInputPump(InputStream is, OutputStream os, 226 boolean closeWhenExhausted) { 227 StreamPumper pumper = new StreamPumper(is, os, closeWhenExhausted); 228 pumper.setAutoflush(true); 229 return pumper; 230 } 231 232 } 233 | Popular Tags |