private int exec(String cmd) throws InterruptedException, IOException { ProcessBuilder pb = new ProcessBuilder(exe, cmd); pb.redirectErrorStream(true); Process p = pb.start(); p.getOutputStream().close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy(p.getInputStream(), baos); int r = p.waitFor(); if (r!=0) LOGGER.info(exe+" cmd: output:\n"+baos); return r; }
@Override public void run () { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; try { while ((line = reader.readLine()) != null) { // augment output with java file line references :D printFileLineNumber(line); } } catch (IOException e) { e.printStackTrace(); } }
@Override public void run () { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; try { while ((line = reader.readLine()) != null) { // augment output with java file line references :D printFileLineNumber(line); } } catch (IOException e) { e.printStackTrace(); } }
@Override public void run () { BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1); try { int c = 0; while ((c = reader.read()) != -1) { callback.character((char)c); } } catch (IOException e) { // e.printStackTrace(); } } });
public synchronized int getUserHZ() throws IOException { if (userHz < 0) { ProcessBuilder pb = new ProcessBuilder("getconf", "CLK_TCK"); Process p = pb.start(); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = in.readLine().trim(); userHz = Integer.valueOf(line); } return userHz; }
private void close(Process p) { try { IOUtils.closeQuietly(p.getInputStream()); IOUtils.closeQuietly(p.getOutputStream()); IOUtils.closeQuietly(p.getErrorStream()); } finally { if (p != null) { p.destroy(); } } }
private static String getNvidiaStats() throws java.io.IOException { String cmd = "nvidia-smi"; InputStream stdin = Runtime.getRuntime().exec(cmd).getInputStream(); InputStreamReader isr = new InputStreamReader(stdin); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String s; while ((s = br.readLine()) != null) { sb.append(s).append("\n"); } return sb.toString(); }
public static InputStream getBZip2PipedInputStream(String filename) throws IOException { String bzcat = System.getProperty("bzcat", "bzcat"); Runtime rt = Runtime.getRuntime(); String cmd = bzcat + " " + filename; //log.info("getBZip2PipedInputStream: Running command: "+cmd); Process p = rt.exec(cmd); Writer errWriter = new BufferedWriter(new OutputStreamWriter(System.err)); StreamGobbler errGobbler = new StreamGobbler(p.getErrorStream(), errWriter); errGobbler.start(); return p.getInputStream(); }
private static StringBuilder captureOutput(Process process) throws IOException, InterruptedException { BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream())); StringBuilder result = new StringBuilder(); result.append("output:\n"); dump(output, result); result.append("error:\n"); dump(error, result); process.waitFor(); return result; }
public ProcessOutputStream(ProcessBuilder builder, Writer output, Writer error) throws IOException { this.process = builder.start(); errWriterThread = new StreamGobbler(process.getErrorStream(), error); outWriterThread = new StreamGobbler(process.getInputStream(), output); errWriterThread.start(); outWriterThread.start(); }
private void registerWithCloser(Closer closer) { closer.register(process.getInputStream()); closer.register(process.getOutputStream()); } }
private void startPython(String tmpPath, String args) throws IOException { String pythonBinaryPath = config.getString(PythonOptions.PYTHON_BINARY_PATH); try { Runtime.getRuntime().exec(pythonBinaryPath); } catch (IOException ignored) { throw new RuntimeException(pythonBinaryPath + " does not point to a valid python binary."); } process = Runtime.getRuntime().exec(pythonBinaryPath + " -B " + tmpPath + FLINK_PYTHON_PLAN_NAME + args); new Thread(new StreamPrinter(process.getInputStream())).start(); new Thread(new StreamPrinter(process.getErrorStream())).start(); server = new ServerSocket(0); server.setSoTimeout(50); process.getOutputStream().write("plan\n".getBytes(ConfigConstants.DEFAULT_CHARSET)); process.getOutputStream().flush(); }
ProcessWrapper(Process process, String processTag, String command, ConsoleOutputStreamConsumer consumer, String encoding, String errorPrefix) { this.process = process; this.processTag = processTag; this.command = command; this.startTime = System.currentTimeMillis(); this.processOutputStream = StreamPumper.pump(process.getInputStream(), new OutputConsumer(consumer), "", encoding); this.processErrorStream = StreamPumper.pump(process.getErrorStream(), new ErrorConsumer(consumer), errorPrefix, encoding); this.processInputStream = new PrintWriter(new OutputStreamWriter(process.getOutputStream())); }
public OutputStream call() throws IOException { Process p = Runtime.getRuntime().exec(cmd, Util.mapToEnv(inherit(envOverrides)), workDir == null ? null : new File(workDir)); List<String> cmdLines = Arrays.asList(cmd); new StreamCopyThread("stdin copier for remote agent on "+cmdLines, p.getInputStream(), out.getOut()).start(); new StreamCopyThread("stderr copier for remote agent on "+cmdLines, p.getErrorStream(), err).start(); // TODO: don't we need to join? return new RemoteOutputStream(p.getOutputStream()); }
private void writeProcessOutputToFile( Process process, File outputFile ) throws Exception { BufferedReader reader = new BufferedReader( new InputStreamReader( process.getInputStream() ) ); String line = null; try ( PrintStream out = new PrintStream( outputFile ) ) { while ( (line = reader.readLine()) != null ) { out.println( line ); } } process.waitFor(); }
private Process getMockedProcess(OutputStream outputStream) { Process process = mock(Process.class); when(process.getErrorStream()).thenReturn(mock(InputStream.class)); when(process.getInputStream()).thenReturn(mock(InputStream.class)); when(process.getOutputStream()).thenReturn(outputStream); return process; }
private Process mockProcess(final InputStream outputStream, final InputStream errorStream, final OutputStream inputStream) throws InterruptedException { final Process subProcess = mock(Process.class); when(subProcess.waitFor()).thenReturn(42); when(subProcess.getInputStream()).thenReturn(outputStream); when(subProcess.getErrorStream()).thenReturn(errorStream); when(subProcess.getOutputStream()).thenReturn(inputStream); return subProcess; }
/** * Executes a process and returns the process output and exit code. */ public static ProcessResult run(Process process) throws IOException, InterruptedException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), baos, "out>"); StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), baos, "err>"); outputGobbler.start(); errorGobbler.start(); int result = process.waitFor(); outputGobbler.waitFor(); errorGobbler.waitFor(); return new ProcessResult(result, baos.toString()); }
private static void awaitSignal( Process process ) throws IOException { try ( BufferedReader reader = new BufferedReader( new InputStreamReader( process.getInputStream() ) ) ) { String line = reader.readLine(); if ( !SIGNAL.equals( line ) ) { fail( "Got weird signal " + line ); } // We got signal, great } } }
public ProcessStreamHandler( Process process, boolean quiet, String prefix, StreamExceptionHandler failureHandler, PrintStream out, PrintStream err ) { this.process = process; this.out = new Thread( new StreamConsumer( process.getInputStream(), out, quiet, prefix, failureHandler ) ); this.err = new Thread( new StreamConsumer( process.getErrorStream(), err, quiet, prefix, failureHandler ) ); }