You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/12 06:47:56 UTC

svn commit: r1102168 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/Exec.java test/org/apache/commons/runtime/TestExec.java

Author: mturk
Date: Thu May 12 04:47:56 2011
New Revision: 1102168

URL: http://svn.apache.org/viewvc?rev=1102168&view=rev
Log:
Use byte arrays instead streams. We get all the data ayhow

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java?rev=1102168&r1=1102167&r2=1102168&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Exec.java Thu May 12 04:47:56 2011
@@ -15,8 +15,6 @@
  */
 package org.apache.commons.runtime;
 
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
 import java.io.File;
 import java.util.List;
 
@@ -118,30 +116,30 @@ public abstract class Exec
         env = envp.toArray(new String[0]);
     }
 
-    public InputStream getOutputStream()
+    public byte[] getStdout()
     {
         if (out == null)
             out = new byte[0];
-        return new ByteArrayInputStream(out);
+        return out;
     }
 
-    public InputStream getErrorStream()
+    public byte[] getStderr()
     {
         if (err == null)
             err = new byte[0];
-        return new ByteArrayInputStream(err);
+        return err;
     }
 
     /**
-     * Indicates whether the standard error should be redirected.
-     * If not redirected, the {@link Exec#getOutputStream()} will always
-     * return {@code null} and standard output is written to
+     * Indicates whether the standard output should be redirected.
+     * If not redirected, the {@link Exec#getStdout()} will always
+     * return {@code empty} byte array and standard output is written to
      * {@code /dev/null}.
      *
      * @return {@code true} if the standard output is redirected; {@code false}
      *         otherwise.
      */
-    public boolean redirectOutputStream()
+    public boolean redirectStdout()
     {
         return (redirect & 1) != 0;
     }
@@ -149,14 +147,14 @@ public abstract class Exec
     /**
      * Indicates whether the standard error should be redirected.
      * 
-     * If not redirected, the {@link Exec#getErrorStream()} will always
-     * return {@code null} and standard error is written to
+     * If not redirected, the {@link Exec#getStderr()} will always
+     * return {@code empty} byte array and standard error is written to
      * {@link Exec#getOutputStream()}.
      *
      * @return {@code true} if the standard error is redirected; {@code false}
      *         otherwise.
      */
-    public boolean redirectErrorStream()
+    public boolean redirectStderr()
     {
         return (redirect & 2) != 0;
     }
@@ -169,7 +167,7 @@ public abstract class Exec
      *            otherwise.
      * @return this exec instance.
      */
-    public Exec redirectOutputStream(boolean redirect)
+    public Exec redirectStdout(boolean redirect)
     {
         if (redirect)
             this.redirect |= 1;
@@ -186,7 +184,7 @@ public abstract class Exec
      *            otherwise.
      * @return this exec instance.
      */
-    public Exec redirectErrorStream(boolean redirect)
+    public Exec redirectStderr(boolean redirect)
     {
         if (redirect)
             this.redirect |= 2;

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java?rev=1102168&r1=1102167&r2=1102168&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestExec.java Thu May 12 04:47:56 2011
@@ -18,7 +18,6 @@ package org.apache.commons.runtime;
 
 import org.testng.annotations.*;
 import org.testng.Assert;
-import java.io.InputStream;
 import java.io.IOException;
 import java.io.File;
 import java.util.ArrayList;
@@ -36,15 +35,15 @@ public class TestExec extends Assert
         ArrayList<String> args = new ArrayList<String>();
         args.add("/bin/ls");
         args.add("-al");
-        e.redirectOutputStream(true);
-        e.redirectErrorStream(true);
+        e.redirectStdout(true);
+        e.redirectStderr(true);
         int r = e.run(args);
         assertEquals(r, Status.CHILD_DONE);
-        InputStream stdout = e.getOutputStream();
+        byte[] stdout = e.getStdout();
         assertNotNull(stdout);
-        assertTrue(stdout.available() > 0);
-        InputStream stderr = e.getErrorStream();
-        assertEquals(stderr.available(), 0);
+        assertTrue(stdout.length > 0);
+        byte[] stderr = e.getStderr();
+        assertEquals(stderr.length, 0);
     }
 
     @Test(groups = { "posix" })
@@ -56,14 +55,12 @@ public class TestExec extends Assert
         ArrayList<String> args = new ArrayList<String>();
         args.add("/bin/cat");
         e.setInput("hello world".getBytes());
-        e.redirectOutputStream(true);
+        e.redirectStdout(true);
         int r = e.run(args);
         assertEquals(r, Status.CHILD_DONE);
-        InputStream stdout = e.getOutputStream();
+        byte[] stdout = e.getStdout();
         assertNotNull(stdout);
-        byte[] rv = new byte[32];
-        int readn = stdout.read(rv);
-        String rs = new String(rv, 0, readn);
+        String rs = new String(stdout);
         assertEquals(rs, "hello world");
     }
 
@@ -76,13 +73,13 @@ public class TestExec extends Assert
         ArrayList<String> args = new ArrayList<String>();
         args.add(ExecImpl.getInstance().getShell());
         args.add("/V");
-        e.redirectOutputStream(true);
-        e.redirectErrorStream(true);
+        e.redirectStdout(true);
+        e.redirectStderr(true);
         int r = e.run(args);
         assertEquals(r, Status.CHILD_DONE);
-        InputStream stdout = e.getOutputStream();
+        byte[] stdout = e.getStdout();
         assertNotNull(stdout);
-        assertTrue(stdout.available() > 0);
+        assertTrue(stdout.length > 0);
     }
 
     @Test(groups = { "core" })
@@ -106,13 +103,13 @@ public class TestExec extends Assert
         ArrayList<String> args = new ArrayList<String>();
         args.add(Vm.executable().getPath());
         args.add("-version");
-        e.redirectOutputStream(true);
+        e.redirectStdout(true);
         int r = e.run(args);
         assertEquals(r, Status.CHILD_DONE);
         assertEquals(e.exitval(), 0);
-        InputStream stdout = e.getOutputStream();
+        byte[] stdout = e.getStdout();
         assertNotNull(stdout);
-        assertTrue(stdout.available() > 0);
+        assertTrue(stdout.length > 0);
     }
 
     @Test(groups = { "core" })
@@ -121,13 +118,15 @@ public class TestExec extends Assert
     {
         Exec e = Exec.newInstance();
         assertNotNull(e);
-        e.redirectOutputStream(true);
+        e.redirectStdout(true);
         int r = e.runCommand("echo foo");
         assertEquals(r, Status.CHILD_DONE);
         assertEquals(e.exitval(), 0);
-        InputStream stdout = e.getOutputStream();
+        byte[] stdout = e.getStdout();
         assertNotNull(stdout);
-        assertTrue(stdout.available() >= 3);
+        assertEquals(stdout[0], (byte)'f');
+        assertEquals(stdout[1], (byte)'o');
+        assertEquals(stdout[2], (byte)'o');
     }
 
 }