1 18 19 package org.apache.tools.ant.input; 20 21 import java.io.InputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.taskdefs.StreamPumper; 25 import org.apache.tools.ant.util.FileUtils; 26 27 32 public class GreedyInputHandler extends DefaultInputHandler { 33 34 private static final int BUFFER_SIZE = 1024; 35 36 39 public GreedyInputHandler() { 40 } 41 42 48 public void handleInput(InputRequest request) throws BuildException { 49 String prompt = getPrompt(request); 50 InputStream in = null; 51 try { 52 in = getInputStream(); 53 System.err.println(prompt); 54 System.err.flush(); 55 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 56 StreamPumper p = new StreamPumper(in, baos); 57 Thread t = new Thread (p); 58 t.start(); 59 try { 60 t.join(); 61 } catch (InterruptedException e) { 62 try { 63 t.join(); 64 } catch (InterruptedException e2) { 65 } 67 } 68 request.setInput(new String (baos.toByteArray())); 69 if (!(request.isInputValid())) { 70 throw new BuildException( 71 "Received invalid console input"); 72 } 73 if (p.getException() != null) { 74 throw new BuildException( 75 "Failed to read input from console", p.getException()); 76 } 77 } finally { 78 FileUtils.close(in); 79 } 80 } 81 } 82 | Popular Tags |