From ef8793a8254c5e23bf21ffc16fe104b2eba7010b Mon Sep 17 00:00:00 2001 From: supertick Date: Wed, 1 May 2024 06:37:03 -0700 Subject: [PATCH] updated with javadoc, timeout, and default --- src/main/java/org/myrobotlab/io/FileIO.java | 78 ++++++++++++++------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/myrobotlab/io/FileIO.java b/src/main/java/org/myrobotlab/io/FileIO.java index e027aeacd4..95c5134a3c 100644 --- a/src/main/java/org/myrobotlab/io/FileIO.java +++ b/src/main/java/org/myrobotlab/io/FileIO.java @@ -52,6 +52,7 @@ import java.util.Properties; import java.util.Set; import java.util.TreeMap; +import java.util.concurrent.TimeUnit; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -1427,34 +1428,61 @@ public static String normalize(String dirPath) { } } + /** + * Checks if a specific executable command is available in the system. + * This function attempts to execute the command with a timeout of 3 seconds to ensure + * that the check does not hang indefinitely. The process is considered available + * if it can be started without throwing an exception and completes successfully + * within the specified timeout. + * + * @param command the command to check for availability. + * @return true if the command is available and completes successfully within the timeout, false otherwise. + */ + public static boolean isExecutableAvailable(String command) { - try { - // Attempt to execute the command - Process process = java.lang.Runtime.getRuntime().exec(command); + return isExecutableAvailable(command, 3); + } + + /** + * Checks if a specific executable command is available in the system. + * This function attempts to execute the command with a timeout to ensure + * that the check does not hang indefinitely. The process is considered available + * if it can be started without throwing an exception and completes successfully + * within the specified timeout. + * + * @param command the command to check for availability. + * @param timeoutSeconds the maximum time in seconds to wait for the command to complete. + * @return true if the command is available and completes successfully within the timeout, false otherwise. + */ + public static boolean isExecutableAvailable(String command, int timeoutSeconds) { + try { + // Attempt to execute the command + Process process = java.lang.Runtime.getRuntime().exec(command); - // Check the exit value of the process - // If the process has terminated correctly, the command is available - if (process.waitFor() == 0) { - return true; - } + // Wait for the process to complete with a timeout + if (process.waitFor(timeoutSeconds, TimeUnit.SECONDS) && process.exitValue() == 0) { + return true; + } - // Read any errors from the attempted command - try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) { - String line; - while ((line = reader.readLine()) != null) { - System.out.println(line); - } - } + // Read and log any errors from the attempted command + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) { + String line; + while ((line = reader.readLine()) != null) { + log.info(line); + } + } + + return false; + } catch (IOException e) { + log.info("IOException: " + e.getMessage()); + return false; + } catch (InterruptedException e) { + log.info("InterruptedException: " + e.getMessage()); + // Restore interrupted state + Thread.currentThread().interrupt(); + return false; + } + } - return false; - } catch (IOException e) { - log.info("IOException: " + e.getMessage()); - return false; - } catch (InterruptedException e) { - log.info("InterruptedException: " + e.getMessage()); - return false; - } -} - }