This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git


The following commit(s) were added to refs/heads/master by this push:
     new a7c8d4bf Use tryt-with-resources
a7c8d4bf is described below

commit a7c8d4bf254bd25672826ddcad24d80b7a9bdbd8
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Dec 31 15:50:23 2023 -0500

    Use tryt-with-resources
---
 .../org/apache/commons/exec/issues/Exec49Test.java | 50 +++++++++++-----------
 1 file changed, 26 insertions(+), 24 deletions(-)

diff --git a/src/test/java/org/apache/commons/exec/issues/Exec49Test.java 
b/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
index 725cb04e..7d7fc494 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
@@ -50,22 +50,23 @@ public class Exec49Test {
             final CommandLine cl = CommandLine.parse("/bin/ls");
             cl.addArgument("/opt");
             // redirect stdout/stderr to pipedOutputStream
-            final PipedOutputStream pipedOutputStream = new 
PipedOutputStream();
-            final PumpStreamHandler psh = new 
PumpStreamHandler(pipedOutputStream);
-            exec.setStreamHandler(psh);
-            // start an asynchronous process to enable the main thread
-            System.out.println("Preparing to execute process - commandLine=" + 
cl.toString());
-            final DefaultExecuteResultHandler handler = new 
DefaultExecuteResultHandler();
-            exec.execute(cl, handler);
-            System.out.println("Process spun off successfully - process=" + 
cl.getExecutable());
-            try (PipedInputStream pis = new 
PipedInputStream(pipedOutputStream)) {
-                while (pis.read() >= 0) {
+            try (PipedOutputStream pipedOutputStream = new 
PipedOutputStream()) {
+                final PumpStreamHandler psh = new 
PumpStreamHandler(pipedOutputStream);
+                exec.setStreamHandler(psh);
+                // start an asynchronous process to enable the main thread
+                System.out.println("Preparing to execute process - 
commandLine=" + cl.toString());
+                final DefaultExecuteResultHandler handler = new 
DefaultExecuteResultHandler();
+                exec.execute(cl, handler);
+                System.out.println("Process spun off successfully - process=" 
+ cl.getExecutable());
+                try (PipedInputStream pis = new 
PipedInputStream(pipedOutputStream)) {
+                    while (pis.read() >= 0) {
 //                 System.out.println("pis.available() " + pis.available());
 //                 System.out.println("x " + x);
+                    }
                 }
+                handler.waitFor(WAIT);
+                handler.getExitValue(); // will fail if process has not 
finished
             }
-            handler.waitFor(WAIT);
-            handler.getExitValue(); // will fail if process has not finished
         }
     }
 
@@ -81,22 +82,23 @@ public class Exec49Test {
             final CommandLine cl = CommandLine.parse("/bin/ls");
             cl.addArgument("/opt");
             // redirect only stdout to pipedOutputStream
-            final PipedOutputStream pipedOutputStream = new 
PipedOutputStream();
-            final PumpStreamHandler psh = new 
PumpStreamHandler(pipedOutputStream, new ByteArrayOutputStream());
-            exec.setStreamHandler(psh);
-            // start an asynchronous process to enable the main thread
-            System.out.println("Preparing to execute process - commandLine=" + 
cl.toString());
-            final DefaultExecuteResultHandler handler = new 
DefaultExecuteResultHandler();
-            exec.execute(cl, handler);
-            System.out.println("Process spun off successfully - process=" + 
cl.getExecutable());
-            try (PipedInputStream pis = new 
PipedInputStream(pipedOutputStream)) {
-                while (pis.read() >= 0) {
+            try (PipedOutputStream pipedOutputStream = new 
PipedOutputStream()) {
+                final PumpStreamHandler psh = new 
PumpStreamHandler(pipedOutputStream, new ByteArrayOutputStream());
+                exec.setStreamHandler(psh);
+                // start an asynchronous process to enable the main thread
+                System.out.println("Preparing to execute process - 
commandLine=" + cl.toString());
+                final DefaultExecuteResultHandler handler = new 
DefaultExecuteResultHandler();
+                exec.execute(cl, handler);
+                System.out.println("Process spun off successfully - process=" 
+ cl.getExecutable());
+                try (PipedInputStream pis = new 
PipedInputStream(pipedOutputStream)) {
+                    while (pis.read() >= 0) {
 //                 System.out.println("pis.available() " + pis.available());
 //                 System.out.println("x " + x);
+                    }
                 }
+                handler.waitFor(WAIT);
+                handler.getExitValue(); // will fail if process has not 
finished
             }
-            handler.waitFor(WAIT);
-            handler.getExitValue(); // will fail if process has not finished
         }
     }
 

Reply via email to