You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Paul Kilroy <pk...@channelpoint.com> on 2001/07/23 19:07:18 UTC

[PATCH] Fixed TODO in PumpStreamHandler.java for stdin

I added support for sub-process gaining access to standard input. I wrote a
taskdef that uses this functionality called "javainput". It is used with
Java programs that take standard input. There were obvious problems when
those programs where run in a separate process (fork="true"). This patch
fixes that (this is my first patch to the mailing list. Please let me know
if another format is preferable):

Index: PumpStreamHandler.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/PumpStrea
mHandler.java,v
retrieving revision 1.3
diff -c -p -r1.3 PumpStreamHandler.java
*** PumpStreamHandler.java	2001/07/13 12:54:30	1.3
--- PumpStreamHandler.java	2001/07/23 16:36:39
*************** import java.io.IOException;
*** 59,81 ****
  import java.io.OutputStream;
  
  /**
!  * Copies standard output and error of subprocesses to standard output and
!  * error of the parent process.
   *
-  * TODO: standard input of the subprocess is not implemented.
-  *
   * @author thomas.haas@softwired-inc.com
   */
  public class PumpStreamHandler implements ExecuteStreamHandler {
  
      private Thread inputThread;
      private Thread errorThread;
  
      private OutputStream out, err;
  
!     public PumpStreamHandler(OutputStream out, OutputStream err) {
          this.out = out;
          this.err = err;
      }
  
      public PumpStreamHandler(OutputStream outAndErr) {
--- 59,87 ----
  import java.io.OutputStream;
  
  /**
!  * Copies standard output and error of subprocesses to standard input,
output 
!  * and error of the parent process.
   *
   * @author thomas.haas@softwired-inc.com
   */
  public class PumpStreamHandler implements ExecuteStreamHandler {
  
      private Thread inputThread;
      private Thread errorThread;
+     private Thread outputThread;
  
      private OutputStream out, err;
+     private InputStream in;
  
!     public PumpStreamHandler(OutputStream out, OutputStream err, 
!       InputStream in) {
          this.out = out;
          this.err = err;
+         this.in = in;
+     }
+ 
+     public PumpStreamHandler(OutputStream out, OutputStream err) {
+         this(out, err, System.in);
      }
  
      public PumpStreamHandler(OutputStream outAndErr) {
*************** public class PumpStreamHandler implement
*** 97,124 ****
  
  
      public void setProcessInputStream(OutputStream os) {
      }
  
  
      public void start() {
          inputThread.start();
          errorThread.start();
      }
  
  
      public void stop() {
          try {
              inputThread.join();
          } catch(InterruptedException e) {}
          try {
              errorThread.join();
          } catch(InterruptedException e) {}
          try {
!             err.flush();
!         } catch (IOException e) {}
!         try {
!             out.flush();
!         } catch (IOException e) {}
      }
  
      protected OutputStream getErr() {
--- 103,132 ----
  
  
      public void setProcessInputStream(OutputStream os) {
+         createProcessInputPump(os, in);
      }
  
  
      public void start() {
          inputThread.start();
          errorThread.start();
+         outputThread.start();
      }
  
  
      public void stop() {
          try {
+             inputThread.interrupt();
              inputThread.join();
          } catch(InterruptedException e) {}
          try {
+             errorThread.interrupt();
              errorThread.join();
          } catch(InterruptedException e) {}
          try {
!             outputThread.interrupt();
!             outputThread.join();
!         } catch(InterruptedException e) {}
      }
  
      protected OutputStream getErr() {
*************** public class PumpStreamHandler implement
*** 129,134 ****
--- 137,146 ----
          return out;
      }
  
+     protected InputStream getIn() {
+         return in;
+     }
+ 
      protected void createProcessOutputPump(InputStream is, OutputStream
os) {
          inputThread = createPump(is, os);
      }
*************** public class PumpStreamHandler implement
*** 137,145 ****
          errorThread = createPump(is, os);
      }
  
  
      /**
!      * Creates a stream pumper to copy the given input stream to the given
output stream.
       */
      protected Thread createPump(InputStream is, OutputStream os) {
          final Thread result = new Thread(new StreamPumper(is, os));
--- 149,161 ----
          errorThread = createPump(is, os);
      }
  
+     protected void createProcessInputPump(OutputStream os, InputStream is)
{
+         outputThread = createPump(is, os);
+     }
  
      /**
!      * Creates a stream pumper to copy the given input stream to the given

!      * output stream.
       */
      protected Thread createPump(InputStream is, OutputStream os) {
          final Thread result = new Thread(new StreamPumper(is, os));
Index: StreamPumper.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/StreamPum
per.java,v
retrieving revision 1.2
diff -c -p -r1.2 StreamPumper.java
*** StreamPumper.java	2001/01/03 14:18:31	1.2
--- StreamPumper.java	2001/07/23 16:36:39
*************** public class StreamPumper implements Run
*** 73,79 ****
      private InputStream is;
      private OutputStream os;
  
- 
      /**
       * Create a new stream pumper.
       *
--- 73,78 ----
*************** public class StreamPumper implements Run
*** 85,91 ****
          this.os = os;
      }
  
- 
      /**
       * Copies data from the input stream to the output stream.
       *
--- 84,89 ----
*************** public class StreamPumper implements Run
*** 102,107 ****
--- 100,106 ----
                      Thread.sleep(SLEEP);
                  } catch (InterruptedException e) {}
              }
+             os.flush();
          } catch(IOException e) {}
      }
  }