@Test(timeOut=1000, expectedExceptions=IllegalStateException.class) public void testExecuteAndWaitForConsumingErrorStream() throws Exception { Process process = createDumpingProcess(true); MonitorUtils.waitFor(process); }
public static String waitFor(Process process) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerOut = new StreamGobbler(process.getInputStream(), bytesOut, null); gobblerOut.start(); ByteArrayOutputStream bytesErr = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerErr = new StreamGobbler(process.getErrorStream(), bytesErr, null); gobblerErr.start(); try { process.waitFor(); gobblerOut.blockUntilFinished(); gobblerErr.blockUntilFinished(); if (bytesErr.size() > 0) { throw new ProcessHasStderr(bytesErr.toByteArray()); } return new String(bytesOut.toByteArray()); } catch (Exception e) { throw Throwables.propagate(e); } finally { if (gobblerOut.isAlive()) gobblerOut.interrupt(); if (gobblerErr.isAlive()) gobblerErr.interrupt(); } }
MonitorUtils { public static String waitFor(Process process) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerOut = new StreamGobbler(process.getInputStream(), bytesOut, null); gobblerOut.start(); ByteArrayOutputStream bytesErr = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerErr = new StreamGobbler(process.getErrorStream(), bytesErr, null); gobblerErr.start(); try { process.waitFor(); gobblerOut.blockUntilFinished(); gobblerErr.blockUntilFinished(); if (bytesErr.size() > 0) { throw new ProcessHasStderr(bytesErr.toByteArray()); } return new String(bytesOut.toByteArray()); } catch (Exception e) { throw Throwables.propagate(e); } finally { if (gobblerOut.isAlive()) gobblerOut.interrupt(); if (gobblerErr.isAlive()) gobblerErr.interrupt(); } } }
MonitorUtils { public static String waitFor(Process process) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerOut = new StreamGobbler(process.getInputStream(), bytesOut, null); gobblerOut.start(); ByteArrayOutputStream bytesErr = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerErr = new StreamGobbler(process.getErrorStream(), bytesErr, null); gobblerErr.start(); try { process.waitFor(); gobblerOut.blockUntilFinished(); gobblerErr.blockUntilFinished(); if (bytesErr.size() > 0) { throw new ProcessHasStderr(bytesErr.toByteArray()); } return new String(bytesOut.toByteArray()); } catch (Exception e) { throw Throwables.propagate(e); } finally { if (gobblerOut.isAlive()) gobblerOut.interrupt(); if (gobblerErr.isAlive()) gobblerErr.interrupt(); } }  }
MonitorUtils { public static String waitFor(Process process) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerOut = new StreamGobbler(process.getInputStream(), bytesOut, null); gobblerOut.start(); ByteArrayOutputStream bytesErr = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerErr = new StreamGobbler(process.getErrorStream(), bytesErr, null); gobblerErr.start(); try { process.waitFor(); gobblerOut.blockUntilFinished(); gobblerErr.blockUntilFinished(); if (bytesErr.size() > 0) { throw new ProcessHasStderr(bytesErr.toByteArray()); } return new String(bytesOut.toByteArray()); } catch (Exception e) { throw Throwables.propagate(e); } finally { if (gobblerOut.isAlive()) gobblerOut.interrupt(); if (gobblerErr.isAlive()) gobblerErr.interrupt(); } }  static boolean isUrlUp(URL url); static boolean isPidRunning(int pid); static boolean isPidRunning(int pid, String regex); static List<Integer> getRunningPids(String regex); static List<Integer> getRunningPids(String regex, String excludingRegex); static MemoryUsage getMemoryUsage(int pid); static MemoryUsage getMemoryUsage(int pid, String clazzRegexOfInterest, int minInstancesOfInterest); static List<String> searchLog(File file, String grepOfInterest); static List<String> searchLog(File file, String grepOfInterest, Set<String> grepExclusions); static Process exec(String cmd); static String waitFor(Process process); static int findOwnPid(); }
MonitorUtils { public static String waitFor(Process process) { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerOut = new StreamGobbler(process.getInputStream(), bytesOut, null); gobblerOut.start(); ByteArrayOutputStream bytesErr = new ByteArrayOutputStream(); @SuppressWarnings("resource") StreamGobbler gobblerErr = new StreamGobbler(process.getErrorStream(), bytesErr, null); gobblerErr.start(); try { process.waitFor(); gobblerOut.blockUntilFinished(); gobblerErr.blockUntilFinished(); if (bytesErr.size() > 0) { throw new ProcessHasStderr(bytesErr.toByteArray()); } return new String(bytesOut.toByteArray()); } catch (Exception e) { throw Throwables.propagate(e); } finally { if (gobblerOut.isAlive()) gobblerOut.interrupt(); if (gobblerErr.isAlive()) gobblerErr.interrupt(); } }  static boolean isUrlUp(URL url); static boolean isPidRunning(int pid); static boolean isPidRunning(int pid, String regex); static List<Integer> getRunningPids(String regex); static List<Integer> getRunningPids(String regex, String excludingRegex); static MemoryUsage getMemoryUsage(int pid); static MemoryUsage getMemoryUsage(int pid, String clazzRegexOfInterest, int minInstancesOfInterest); static List<String> searchLog(File file, String grepOfInterest); static List<String> searchLog(File file, String grepOfInterest, Set<String> grepExclusions); static Process exec(String cmd); static String waitFor(Process process); static int findOwnPid();  }