1 package net.jaekl.cfb.util;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
9 public static class Result
11 private int m_retCode;
12 private String m_stdout;
13 private String m_stderr;
15 Result(int retCode, String stdout, String stderr) {
21 public int getRetCode() { return m_retCode; }
22 public String getStdout() { return m_stdout; }
23 public String getStderr() { return m_stderr; }
26 private static class StreamGobbler extends Thread {
28 private StringBuilder m_sb;
29 private BufferedReader m_br;
31 public StreamGobbler(InputStream is) {
32 m_sb = new StringBuilder();
33 m_br = new BufferedReader(new InputStreamReader(is));
40 line = m_br.readLine();
41 while (null != line) {
42 m_sb.append(line).append(System.lineSeparator());
43 line = m_br.readLine();
45 } catch (IOException exc) {
46 m_sb.append(Util.stringify(exc));
51 } catch (IOException exc) {
52 m_sb.append(Util.stringify(exc));
57 public String getOutput() { return m_sb.toString(); }
65 public Result exec(String cmd) throws IOException
71 Process proc = doRuntimeExec(cmd);
72 assert( null != proc );
74 StreamGobbler stdoutGobbler = new StreamGobbler(proc.getInputStream());
75 StreamGobbler stderrGobbler = new StreamGobbler(proc.getErrorStream());
77 stdoutGobbler.start();
78 stderrGobbler.start();
81 retCode = proc.waitFor();
85 stdout = stdoutGobbler.getOutput();
86 stderr = stdoutGobbler.getOutput();
88 } catch (InterruptedException exc) {
89 stderr += Util.stringify(exc);
92 return new Result(retCode, stdout, stderr);
95 Process doRuntimeExec(String cmd) throws IOException {
96 return Runtime.getRuntime().exec(cmd);