List of usage examples for java.io InputStream wait
public final void wait() throws InterruptedException
From source file:org.roda.core.util.CommandUtility.java
/** * Execute the given command line.//from w ww .ja v a 2 s . com * * @param args * the command line as a list of arguments. * * @return a {@link String} with the output of the command. * * @throws CommandException */ public static String execute(boolean withErrorStream, String... args) throws CommandException { int exitValue = 0; String output; try { StringBuilder builder = new StringBuilder(); for (String arg : args) { builder.append(arg + " "); } LOGGER.debug("Executing {}", builder); // create and execute process ProcessBuilder processBuilder = new ProcessBuilder(args); processBuilder.redirectErrorStream(withErrorStream); Process process = processBuilder.start(); // Get process output InputStream is = process.getInputStream(); CaptureOutputThread captureOutputThread = new CaptureOutputThread(is); synchronized (is) { captureOutputThread.start(); // Wait until the CaptureOutputThread notifies that is finished // reading the input stream. LOGGER.debug("Waiting until CaptureOutputThread notifies"); is.wait(); } LOGGER.debug("CaptureOutputThread notified. Getting output..."); output = captureOutputThread.output; // Get process exit value exitValue = process.waitFor(); IOUtils.closeQuietly(is); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Command {} terminated with value {}", Arrays.toString(args), exitValue); } if (exitValue == 0) { return output; } else { throw new CommandException( "Command " + Arrays.toString(args) + " terminated with error code " + exitValue, exitValue, output); } } catch (IOException | InterruptedException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Error executing command {}", Arrays.toString(args), e); } throw new CommandException("Error executing command " + Arrays.toString(args) + " - " + e.getMessage(), e); } }