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:myproject.Model.Common.ProcessExecutor.java

private static String properExecute(File file, String... commands) throws IOException, InterruptedException {
    ProcessBuilder builder = new ProcessBuilder(commands);
    if (file != null) {
        builder.directory(file);//from   w w  w  .j a  v  a  2 s  .  c  om
    }
    Process process = builder.start();
    String input = IOUtils.toString(process.getInputStream(), "utf-8");
    String error = IOUtils.toString(process.getErrorStream(), "utf-8");
    //String command = Arrays.toString(builder.command().toArray(new String[] {}));
    process.waitFor();
    process.getInputStream().close();
    if (!error.isEmpty()) {
        Log.errorLog(error, ProcessExecutor.class);
    }
    String result = input;
    if (input.isEmpty()) {
        result = error;
    }
    return result;
}

From source file:Main.java

/**
 * Retrieves the output (stdout) of the process represented by the given {@link Process}
 * object. This method blocks until the process is about to terminate and returns the
 * complete output as a string. The returned string contains newlines after each output
 * line. This is also true for the last line and for processes that do not output line
 * endings, i.e., the string always has a trailing newline except for empty outputs.
 * Note that the process is likely to have not terminated yet after this method returns.
 * //from   w  ww.j  a va  2  s  . c o  m
 * @param proc
 *          The process object.
 * @return The stdout output of the process. May be an empty string. The string always
 *         has a trailing newline if it is not empty.
 * @throws IOException
 *           If the process output cannot be read.
 */
public static String getProcessOutput(final Process proc) throws IOException {
    try (final InputStreamReader isr = new InputStreamReader(proc.getInputStream());
            final BufferedReader r = new BufferedReader(isr)) {
        final StringBuilder sb = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        return sb.toString();
    } catch (final IOException e) {
        throw e;
    }
}

From source file:Main.java

public static void execCmd(String szCmd, boolean bCatchLog) {
    try {/*from w w w  .ja v a 2s  .  co m*/
        Log.d("PvTorrent_proc", "execCmd=" + szCmd);
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(szCmd);

        if (bCatchLog) {
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while (null != (line = br.readLine())) {
                Log.e("PvTorrent_proc", line);
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("PvTorrent_proc", e.toString());
    }
}

From source file:design.process.ProcessUtil.java

/***/
public static String executeResult(final File workDir, final String... termArray)
        throws IOException, InterruptedException {
    final Process process = executeProcess(workDir, termArray);
    final String input = IOUtils.toString(process.getInputStream(), "UTF-8");
    final String error = IOUtils.toString(process.getErrorStream(), "UTF-8");
    final String result = input + error;
    return result;
}

From source file:Main.java

public static BufferedReader displayQRCode(String app, String email, String secret) throws IOException {
    String command = createCommand(app, email, secret);
    Process exec = Runtime.getRuntime().exec(command);
    BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
    String line;/*  ww w .ja  v a2 s. c o  m*/
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    return reader;
}

From source file:Main.java

public static String getCPUMax() {
    String txtInfo = "";
    try {//w ww . j a  va  2  s . c o  m
        Process proc = Runtime.getRuntime().exec("cat /proc/cpuinfo");
        InputStream is = proc.getInputStream();
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        try {
            while ((line = br.readLine()) != null) {
                Log.v("TestLog", line);
                if (line.indexOf("BogoMIPS") >= 0) {
                    txtInfo = line.substring(8);
                }
            }
        } catch (IOException e) {
            // Log.v("getStringFromInputStream", "------ getStringFromInputStream " + e.getMessage());
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }
        }
    } catch (IOException e) {
        Log.e("getCpuInfo", "------ getCpuInfo " + e.getMessage());
    }
    return txtInfo;
}

From source file:Main.java

public static String getDns() {

    try {//from   w  w w  .java  2s . c o m
        Process p = Runtime.getRuntime().exec("getprop net.dns1");
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String dns = in.readLine();
        return dns;
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getAdbPort() throws IOException {
    Process process = Runtime.getRuntime().exec("getprop service.adb.tcp.port");
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    return reader.readLine();
}

From source file:Main.java

public static void exec(File workingDir, String command) {
    try {//from  w w  w .j  av  a 2 s .  c o  m
        ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
        processBuilder.directory(workingDir);
        Process process = processBuilder.start();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        process.waitFor();
        process.destroy();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String dumpFile(String filename) {
    String line = "";
    try {/*w w w  . ja  va  2 s. c  o  m*/
        Process ifc = Runtime.getRuntime().exec("cat " + filename);
        BufferedReader bis = new BufferedReader(new InputStreamReader(ifc.getInputStream()));
        line = bis.readLine();
        ifc.destroy();

    } catch (java.io.IOException e) {
        return new String("");
    }

    return line;
}