Patch [MinGW]: ProcessBuilder and redirect

Mohan Embar gnustuff@thisiscool.com
Wed Mar 7 15:39:00 GMT 2007


Hi Tom,

This makes the redirect flag work on Win32. Could you
glance at it to see if I've got all of the new / regenerated
files covered and that I haven't made any flagrant formatting
mistakes?

-- Mohan
http://www.thisiscool.com/
http://www.animalsong.org/

2007-03-07  Mohan Embar  <gnustuff@thisiscool.com>

	* java/lang/Win32Process.java: Added nested class EOFInputStream.
	* java/lang/natWin32Process.cc (ChildProcessPipe): Added DUMMY
	enum and implementation.
	(startProcess): Use redirect flag.
	* classpath/lib/java/lang/Win32Process.class: Regenerated.
	* classpath/lib/java/lang/Win32Process$EOFInputStream.class: New.
	* gcj/javaprims.h: Regenerated.
	* java/lang/Win32Process$EOFInputStream.h: New.

Index: java/lang/Win32Process.java
===================================================================
--- java/lang/Win32Process.java	(revision 122579)
+++ java/lang/Win32Process.java	(working copy)
@@ -85,4 +85,13 @@
 				    boolean redirect)
     throws IOException;
   private native void cleanup ();
+
+  private static class EOFInputStream extends InputStream
+  {
+    static EOFInputStream instance = new EOFInputStream();
+    public int read()
+    {
+      return -1;
+    }
+  }
 }
Index: java/lang/natWin32Process.cc
===================================================================
--- java/lang/natWin32Process.cc	(revision 122579)
+++ java/lang/natWin32Process.cc	(working copy)
@@ -25,6 +25,7 @@
 #include <java/io/FileOutputStream.h>
 #include <java/io/IOException.h>
 #include <java/lang/OutOfMemoryError.h>
+#include <java/lang/Win32Process$EOFInputStream.h>
 #include <gnu/java/nio/channels/FileChannelImpl.h>
 
 using gnu::java::nio::channels::FileChannelImpl;
@@ -146,7 +147,7 @@
 public:
   // Indicates from the child process' point of view
   // whether the pipe is for reading or writing.
-  enum EType {INPUT, OUTPUT};
+  enum EType {INPUT, OUTPUT, DUMMY};
 
   ChildProcessPipe(EType eType);
   ~ChildProcessPipe();
@@ -163,8 +164,11 @@
 };
 
 ChildProcessPipe::ChildProcessPipe(EType eType):
-  m_eType(eType)
+  m_eType(eType), m_hRead(0), m_hWrite(0)
 {
+  if (eType == DUMMY)
+    return;
+  
   SECURITY_ATTRIBUTES sAttrs;
 
   // Explicitly allow the handles to the pipes to be inherited.
@@ -195,7 +199,8 @@
   // Close the parent end of the pipe. This
   // destructor is called after the child process
   // has been spawned.
-  CloseHandle(getChildHandle());
+  if (m_eType != DUMMY)
+    CloseHandle(getChildHandle());
 }
 
 HANDLE ChildProcessPipe::getParentHandle()
@@ -284,7 +289,8 @@
       // on each of standard streams.
       ChildProcessPipe aChildStdIn(ChildProcessPipe::INPUT);
       ChildProcessPipe aChildStdOut(ChildProcessPipe::OUTPUT);
-      ChildProcessPipe aChildStdErr(ChildProcessPipe::OUTPUT);
+      ChildProcessPipe aChildStdErr(redirect ? ChildProcessPipe::DUMMY
+				    : ChildProcessPipe::OUTPUT);
 
       outputStream = new FileOutputStream (new FileChannelImpl (
                            (jint) aChildStdIn.getParentHandle (),
@@ -292,7 +298,10 @@
       inputStream = new FileInputStream (new FileChannelImpl (
                            (jint) aChildStdOut.getParentHandle (),
 			   FileChannelImpl::READ));
-      errorStream = new FileInputStream (new FileChannelImpl (
+      if (redirect)
+        errorStream = Win32Process$EOFInputStream::instance;
+      else
+        errorStream = new FileInputStream (new FileChannelImpl (
                            (jint) aChildStdErr.getParentHandle (),
 			   FileChannelImpl::READ));
 
@@ -310,7 +319,8 @@
 
       si.hStdInput = aChildStdIn.getChildHandle();
       si.hStdOutput = aChildStdOut.getChildHandle();
-      si.hStdError = aChildStdErr.getChildHandle();
+      si.hStdError = redirect ? aChildStdOut.getChildHandle()
+                              : aChildStdErr.getChildHandle();
 
       // Spawn the process. CREATE_NO_WINDOW only applies when
       // starting a console application; it suppresses the





More information about the Java-patches mailing list