Skip to content

Commit

Permalink
updated with javadoc, timeout, and default
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed May 1, 2024
1 parent 46a3830 commit ef8793a
Showing 1 changed file with 53 additions and 25 deletions.
78 changes: 53 additions & 25 deletions src/main/java/org/myrobotlab/io/FileIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}


}

0 comments on commit ef8793a

Please sign in to comment.