Example usage for java.lang Process getInputStream

List of usage examples for java.lang Process getInputStream

Introduction

In this page you can find the example usage for java.lang Process getInputStream.

Prototype

public abstract InputStream getInputStream();

Source Link

Document

Returns the input stream connected to the normal output of the process.

Usage

From source file:biomine.bmvis2.pipeline.sources.StreamGraphSource.java

public static StreamGraphSource getNodeExpandProgramGraphSource(final String expandProgramName, VisualNode node)
        throws IOException, InterruptedException, GraphOperationException {
    String nodeId = node.getId();
    String cmd = expandProgramName + " " + nodeId;
    Logging.debug("expand", "Running command: " + cmd);
    Runtime run = Runtime.getRuntime();
    Process pr = run.exec(cmd);
    pr.waitFor();//from w  w w  . ja va  2 s.  c  o  m

    StreamGraphSource s = new StreamGraphSource(pr.getInputStream(), "Neighborhood for " + nodeId);
    StreamGraphSource.assignPositions(s.getBMGraph(), node);
    return s;
}

From source file:Main.java

public static String highlight(final List<String> lines, final String meta, final String prog,
        final String encoding) throws IOException {
    final File tmpIn = new File(System.getProperty("java.io.tmpdir"),
            String.format("txtmark_code_%d_%d.in", ID, IN_COUNT.incrementAndGet()));
    final File tmpOut = new File(System.getProperty("java.io.tmpdir"),
            String.format("txtmark_code_%d_%d.out", ID, OUT_COUNT.incrementAndGet()));

    try {// w  ww  .j ava 2s  .c  o  m

        final Writer w = new OutputStreamWriter(new FileOutputStream(tmpIn), encoding);

        try {
            for (final String s : lines) {
                w.write(s);
                w.write('\n');
            }
        } finally {
            w.close();
        }

        final List<String> command = new ArrayList<String>();

        command.add(prog);
        command.add(meta);
        command.add(tmpIn.getAbsolutePath());
        command.add(tmpOut.getAbsolutePath());

        final ProcessBuilder pb = new ProcessBuilder(command);
        final Process p = pb.start();
        final InputStream pIn = p.getInputStream();
        final byte[] buffer = new byte[2048];

        int exitCode = 0;
        for (;;) {
            if (pIn.available() > 0) {
                pIn.read(buffer);
            }
            try {
                exitCode = p.exitValue();
            } catch (final IllegalThreadStateException itse) {
                continue;
            }
            break;
        }

        if (exitCode == 0) {
            final Reader r = new InputStreamReader(new FileInputStream(tmpOut), encoding);
            try {
                final StringBuilder sb = new StringBuilder();
                for (;;) {
                    final int c = r.read();
                    if (c >= 0) {
                        sb.append((char) c);
                    } else {
                        break;
                    }
                }
                return sb.toString();
            } finally {
                r.close();
            }
        }

        throw new IOException("Exited with exit code: " + exitCode);
    } finally {
        tmpIn.delete();
        tmpOut.delete();
    }
}

From source file:nl.dreamkernel.s4.tweaker.util.RuntimeExec.java

public static String[] execute(String[] commands, boolean needResponce) {
    try {//from w ww .ja va2  s . c  o  m
        Process process = Runtime.getRuntime().exec(commands);
        if (needResponce) {
            DataInputStream inputStream = new DataInputStream(process.getInputStream());
            if (inputStream != null) {
                String ret = "";
                int size = 0;
                byte[] buffer = new byte[1024];
                try {
                    do {
                        size = inputStream.read(buffer);
                        if (size > 0) {
                            ret += new String(buffer, 0, size, HTTP.UTF_8);
                        }
                    } while (inputStream.available() > 0);
                } catch (IOException e) {
                }
                return ret.split("\n");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.frostwire.desktop.DesktopVPNMonitor.java

private static String readProcessOutput(String command, String arguments) {
    String result = "";
    ProcessBuilder pb = new ProcessBuilder(command, arguments);
    pb.redirectErrorStream(true);/*www.  j a va2  s  .  com*/
    try {
        Process process = pb.start();
        InputStream stdout = process.getInputStream();
        final BufferedReader brstdout = new BufferedReader(new InputStreamReader(stdout));
        String line;

        try {
            StringBuilder sb = new StringBuilder();
            while ((line = brstdout.readLine()) != null) {
                sb.append(line + "\r\n");
            }

            result = sb.toString();
        } catch (Throwable e) {
            LOG.error("Error reading routing table command output", e);
        } finally {
            IOUtils.closeQuietly(brstdout);
            IOUtils.closeQuietly(stdout);
        }

    } catch (Throwable e) {
        LOG.error("Error executing routing table command", e);
    }
    return result;
}

From source file:com.github.piasy.biv.example.App.java

static String getSystemProperty(String propName) {
    String line;/*from   w ww  .  j ava 2 s. c o m*/
    BufferedReader input = null;
    try {
        Process p = Runtime.getRuntime().exec("getprop " + propName);
        input = new BufferedReader(new InputStreamReader(p.getInputStream(), "UTF-8"), 1024);
        line = input.readLine();
        input.close();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to read sysprop " + propName, ex);
        return null;
    } finally {
        IOUtils.closeQuietly(input);
    }
    return line;
}

From source file:com.thoughtworks.go.utils.CommandUtils.java

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);/*  w  w w. ja  v  a2 s . c  om*/
    result.append("error:\n");
    dump(error, result);
    process.waitFor();
    return result;
}

From source file:Main.java

public static int execRootCmdForExitCode(String[] cmds) {
    int result = -1;
    DataOutputStream dos = null;/*from  ww  w.ja  v  a  2  s .c om*/
    DataInputStream dis = null;

    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        dis = new DataInputStream(p.getInputStream());

        for (String cmd : cmds) {
            Log.i("CmdUtils", cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
        }
        dos.writeBytes("exit\n");
        dos.flush();
        String line;
        while ((line = dis.readLine()) != null) {
            Log.d("result", line);
        }
        p.waitFor();
        result = p.exitValue();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:Main.java

private static boolean checkRootMethod3() {
    Process process = null;
    try {// w  w w.ja va  2 s . c  o  m
        process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        if (in.readLine() != null)
            return true;
        return false;
    } catch (Throwable t) {
        return false;
    } finally {
        if (process != null)
            process.destroy();
    }
}

From source file:Main.java

public static String execRootCmd(String[] cmds) {
    String result = "";
    DataOutputStream dos = null;//from  www  .  j a v a  2s.co  m
    DataInputStream dis = null;

    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        dis = new DataInputStream(p.getInputStream());

        for (String cmd : cmds) {
            Log.i("CmdUtils", cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
        }
        dos.writeBytes("exit\n");
        dos.flush();
        String line;
        while ((line = dis.readLine()) != null) {
            Log.d("result", line);
            result += line;
        }
        p.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:io.ingenieux.lambda.shell.LambdaShell.java

private static void runCommandArray(OutputStream os, String... args) throws Exception {
    PrintWriter pw = new PrintWriter(os, true);

    File tempPath = File.createTempFile("tmp-", ".sh");

    IOUtils.write(StringUtils.join(args, " "), new FileOutputStream(tempPath));

    List<String> processArgs = new ArrayList<>(Arrays.asList("/bin/bash", "-x", tempPath.getAbsolutePath()));

    ProcessBuilder psBuilder = new ProcessBuilder(processArgs).//
            redirectErrorStream(true);//

    final Process process = psBuilder.start();

    final Thread t = new Thread(() -> {
        try {//from  w  w  w . j a v a  2  s.co  m
            IOUtils.copy(process.getInputStream(), os);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });

    t.start();

    process.waitFor();

    t.join();

    int resultCode = process.exitValue();
}