Source:
protected static String readPipe(File dir, String[] command, String encoding, Map<String, String> env) { final boolean debug = LOG.isDebugEnabled(); try { if (debug) { LOG.debug("readpipe " + Arrays.asList(command) + "," + dir); } ProcessBuilder pb = new ProcessBuilder(command); pb.directory(dir); if (env != null) pb.environment().putAll(env); final Process p = pb.start(); final BufferedReader lineRead = new BufferedReader( new InputStreamReader(p.getInputStream(), encoding)); p.getOutputStream().close(); final AtomicBoolean gooblerFail = new AtomicBoolean(false); Thread gobbler = new Thread() { public void run() { InputStream is = p.getErrorStream(); try { int ch; if (debug) while ((ch = is.read()) != -1) System.err.print((char) ch); else while (is.read() != -1) { } } catch (IOException e) { if (debug) e.printStackTrace(System.err); gooblerFail.set(true); } try { is.close(); } catch (IOException e) { if (debug) { LOG.debug("Caught exception in gobbler thread", e); } gooblerFail.set(true); } } }; gobbler.start(); String r = null; try { r = lineRead.readLine(); if (debug) { LOG.debug("readpipe may return '" + r + "'"); LOG.debug("(ignoring remaing output:"); } String l; while ((l = lineRead.readLine()) != null) { if (debug) { LOG.debug(l); } } } finally { p.getErrorStream().close(); lineRead.close(); } for (;;) { try { int rc = p.waitFor(); gobbler.join(); if (rc == 0 && r != null && r.length() > 0 && !gooblerFail.get()) return r; if (debug) { LOG.debug("readpipe rc=" + rc); } break; } catch (InterruptedException ie) { } } } catch (IOException e) { LOG.debug("Caught exception in FS.readPipe()", e); } if (debug) { LOG.debug("readpipe returns null"); } return null; }

Target:
protected static String readPipe(File dir, String[] command, String encoding, Map<String, String> env) { final boolean debug = LOG.isDebugEnabled(); try { if (debug) { LOG.debug("readpipe " + Arrays.asList(command) + "," + dir); } ProcessBuilder pb = new ProcessBuilder(command); pb.directory(dir); if (env != null) { pb.environment().putAll(env); } final Process p = pb.start(); final BufferedReader lineRead = new BufferedReader( new InputStreamReader(p.getInputStream(), encoding)); p.getOutputStream().close(); final AtomicBoolean gooblerFail = new AtomicBoolean(false); Thread gobbler = new Thread() { public void run() { InputStream is = p.getErrorStream(); try { int ch; if (debug) while ((ch = is.read()) != -1) System.err.print((char) ch); else while (is.read() != -1) { } } catch (IOException e) { if (debug) e.printStackTrace(System.err); gooblerFail.set(true); } try { is.close(); } catch (IOException e) { if (debug) { LOG.debug("Caught exception in gobbler thread", e); } gooblerFail.set(true); } } }; gobbler.start(); String r = null; try { r = lineRead.readLine(); if (debug) { LOG.debug("readpipe may return '" + r + "'"); LOG.debug("(ignoring remaing output:"); } String l; while ((l = lineRead.readLine()) != null) { if (debug) { LOG.debug(l); } } } finally { p.getErrorStream().close(); lineRead.close(); } for (;;) { try { int rc = p.waitFor(); gobbler.join(); if (rc == 0 && r != null && r.length() > 0 && !gooblerFail.get()) return r; if (debug) { LOG.debug("readpipe rc=" + rc); } break; } catch (InterruptedException ie) { } } } catch (IOException e) { LOG.debug("Caught exception in FS.readPipe()", e); } if (debug) { LOG.debug("readpipe returns null"); } return null; }