Simplify multios

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1736011 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java
index 274bf28..a11f45f 100644
--- a/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java
+++ b/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Pipe.java
@@ -121,7 +121,7 @@
     private static final int READ = 1;
     private static final int WRITE = 2;
 
-    private void setStream(Channel ch, int fd, int readWrite, boolean begOfPipe, boolean endOfPipe) throws IOException {
+    private void setStream(Channel ch, int fd, int readWrite) throws IOException {
         if ((readWrite & (READ | WRITE)) == 0) {
             throw new IllegalArgumentException("Should specify READ and/or WRITE");
         }
@@ -131,13 +131,13 @@
         if ((readWrite & WRITE) != 0 && !(ch instanceof WritableByteChannel)) {
             throw new IllegalArgumentException("Channel is not writable");
         }
-        if (fd == 0 && !(ch instanceof ReadableByteChannel)) {
+        if (fd == 0 && (readWrite & READ) == 0) {
             throw new IllegalArgumentException("Stdin is not readable");
         }
-        if (fd == 1 && !(ch instanceof WritableByteChannel)) {
+        if (fd == 1 && (readWrite & WRITE) == 0) {
             throw new IllegalArgumentException("Stdout is not writable");
         }
-        if (fd == 2 && !(ch instanceof WritableByteChannel)) {
+        if (fd == 2 && (readWrite & WRITE) == 0) {
             throw new IllegalArgumentException("Stderr is not writable");
         }
         // TODO: externalize
@@ -146,24 +146,27 @@
             if (streams[fd] != null && (readWrite & READ) != 0 && (readWrite & WRITE) != 0) {
                 throw new IllegalArgumentException("Can not do multios with read/write streams");
             }
-            MultiChannel mrbc;
-            if (streams[fd] instanceof MultiChannel) {
-                mrbc = (MultiChannel) streams[fd];
-            } else {
-                mrbc = new MultiChannel();
-                if (streams[fd] != null
-                        && ((begOfPipe && (readWrite & READ) != 0)
-                            || (endOfPipe && (readWrite & WRITE) != 0))) {
-                    if (toclose[fd]) {
-                        streams[fd].close();
-                    }
-                } else {
-                    mrbc.addChannel(streams[fd], toclose[fd]);
-                }
-                streams[fd] = mrbc;
+            // If channel is inherited (for example standard input / output), replace it
+            if (streams[fd] != null && !toclose[fd]) {
+                streams[fd] = ch;
                 toclose[fd] = true;
             }
-            mrbc.addChannel(ch, true);
+            // Else do multios
+            else {
+                MultiChannel mrbc;
+                // If the channel is already multios
+                if (streams[fd] instanceof MultiChannel) {
+                    mrbc = (MultiChannel) streams[fd];
+                }
+                // Else create a multios channel
+                else {
+                    mrbc = new MultiChannel();
+                    mrbc.addChannel(streams[fd], toclose[fd]);
+                    streams[fd] = mrbc;
+                    toclose[fd] = true;
+                }
+                mrbc.addChannel(ch, true);
+            }
         }
         else {
             if (streams[fd] != null && toclose[fd]) {
@@ -199,8 +202,6 @@
         // will be effective just before actually running the command.
         WritableByteChannel errChannel = (WritableByteChannel) streams[2];
 
-        // TODO: not sure this is the correct way
-        boolean begOfPipe = !toclose[0];
         boolean endOfPipe = !toclose[1];
 
         try
@@ -232,10 +233,10 @@
                     }
                     Channel ch = Files.newByteChannel(outPath, options);
                     if (fd >= 0) {
-                        setStream(ch, fd, WRITE, begOfPipe, endOfPipe);
+                        setStream(ch, fd, WRITE);
                     } else {
-                        setStream(ch, 1, WRITE, begOfPipe, endOfPipe);
-                        setStream(ch, 2, WRITE, begOfPipe, endOfPipe);
+                        setStream(ch, 1, WRITE);
+                        setStream(ch, 2, WRITE);
                     }
                 }
                 else if ((m = Pattern.compile("([0-9])?>&([0-9])").matcher(t)).matches()) {
@@ -267,7 +268,7 @@
                         options.add(StandardOpenOption.CREATE);
                     }
                     Channel ch = Files.newByteChannel(inPath, options);
-                    setStream(ch, fd, READ + (output ? WRITE : 0), begOfPipe, endOfPipe);
+                    setStream(ch, fd, READ + (output ? WRITE : 0));
                 }
             }