@Test public final void testAutoClose() throws Exception { final MutableBoolean closeCalled = new MutableBoolean(false); assertFalse(closeCalled.booleanValue()); assertTrue(AutoCloseable.class.isAssignableFrom(TestInputStream.class)); // read the input stream // @NEW try (TestInputStream in = new TestInputStream(closeCalled)) { IOUtils.toByteArray(in); } // assert that the close method is called automatically assertTrue(closeCalled.booleanValue()); }
/** * @param cmd * @param dir * @param out * @param err * @param env * @return */ private CommandResult exec( String[] pCmd, File dir, StringWriter pOut, StringWriter pErr, String[] env) throws IOException, InterruptedException { int out = 0; String pCmdString = ArrayUtils.toString(pCmd); if (_log.isDebugEnabled()) _log.debug( "Executing '" + pCmdString + "' with Environment '" + ArrayUtils.toString(env) + "'"); StopWatch clock = new StopWatch(); clock.start(); try { process = Runtime.getRuntime().exec(pCmd, env, dir); out = handleProcess(process, pCmdString, pOut, pErr, _outputList, sig_interrupt); } finally { this.cleanUpProcess(); clock.stop(); if (_log.isInfoEnabled()) _log.info("'" + pCmd + "' completed in " + clock.getTime() + " ms"); } if (sig_interrupt.getValue() == true) { out = -9999; } CommandResult result = new CommandResult(pCmdString, out, pOut.toString(), pErr.toString()); return result; }
public CommandResult exec(String[] pCmd, File dir, Writer pOut, Writer pErr) throws IOException, InterruptedException { ProcessBuilder builder = new ProcessBuilder(); Map<String, String> env = builder.environment(); int out = 0; String pCmdString = ArrayUtils.toString(pCmd); logExec(pCmdString, env); StopWatch clock = new StopWatch(); clock.start(); try { process = Runtime.getRuntime().exec(pCmd, null, dir); out = handleProcess(process, pCmdString, pOut, pErr, _outputList, sig_interrupt); } finally { this.cleanUpProcess(); clock.stop(); if (_log.isInfoEnabled()) _log.info("'" + pCmd + "' completed in " + clock.getTime() + " ms"); } if (sig_interrupt.getValue() == true) { out = -9999; } CommandResult result = new CommandResult(pCmdString, out, pOut.toString(), pErr.toString()); return result; }
public CommandResult exec( String[] pCmd, Map<String, String> pEnv, boolean useSysEnv, Writer pOut, Writer pErr) throws IOException, InterruptedException { int out = 0; String pCmdString = ArrayUtils.toString(pCmd); ProcessBuilder builder = new ProcessBuilder(); builder.command(pCmd); Map<String, String> env = builder.environment(); if (!useSysEnv) env.clear(); for (String name : pEnv.keySet()) { env.put(name, pEnv.get(name)); } logExec(pCmdString, env); StopWatch clock = new StopWatch(); clock.start(); try { process = builder.start(); out = handleProcess(process, pCmdString, pOut, pErr, _outputList, sig_interrupt); } finally { this.cleanUpProcess(); clock.stop(); if (_log.isInfoEnabled()) _log.info("'" + pCmdString + "' completed in " + clock.getTime() + " ms"); } if (sig_interrupt.getValue() == true) { out = -9999; } CommandResult result = new CommandResult(pCmdString, out, pOut.toString(), pErr.toString()); return result; }
@Test public final void testAutoCloseTwoResources() throws Exception { final MutableBoolean close1Called = new MutableBoolean(false); final MutableBoolean close2Called = new MutableBoolean(false); // read the input streams // @NEW try (TestInputStream in1 = new TestInputStream(close1Called); TestInputStream in2 = new TestInputStream(close2Called)) { IOUtils.toByteArray(in1); IOUtils.toByteArray(in2); } // assert that the close methods are called automatically assertTrue(close1Called.booleanValue()); assertTrue(close2Called.booleanValue()); }
public void run() { boolean ok = true; char[] buf = new char[bufSize]; synchronized (this) { try { while (ok) { ok = !interrupt_sig.getValue(); if (pumpLog.isDebugEnabled()) { System.currentTimeMillis(); } int n = iIn.read(buf, 0, buf.length); if (0 > n) { pumpLog.debug("CharPump has encountered EOF"); break; } if (pumpLog.isDebugEnabled()) { System.currentTimeMillis(); } if (pumpLog.isDebugEnabled()) { System.currentTimeMillis(); } iOut.write(buf, 0, n); if (pumpLog.isDebugEnabled()) { System.currentTimeMillis(); } iOut.flush(); } iOut.flush(); } catch (Exception e) { _log.error(e); e.printStackTrace(); } finally { try { if (iIn != null) { iIn.close(); } if (iOut != null) { iOut.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } notifyAll(); } } }
public void terminateClean() throws Exception { sig_interrupt.setValue(true); if (_outputList.size() > 1) { CharPump p1 = _outputList.get(0); if (p1 != null) { synchronized (p1) { p1.wait(1000); } } CharPump p2 = _outputList.get(1); if (p2 != null) { synchronized (p2) { p2.wait(1000); } } } cleanUpProcess(); }