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:ota.otaupdates.utils.Utils.java

/**
 * Credit goes to Matthew Booth (http://www.github.com/MatthewBooth) for this function
 * @param propName The prop to be checked
 * @return boolean If the prop exists//from  w ww .  ja v a  2s . co m
 */
public static Boolean doesPropExist(String propName) {
    boolean valid = false;

    try {
        Process process = Runtime.getRuntime().exec("getprop");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (line.contains("[" + propName + "]")) {
                valid = true;
            }
        }
        bufferedReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return valid;
}

From source file:com.github.pemapmodder.pocketminegui.utils.Utils.java

public static String exec(String... cmdLine) {
    try {//from   ww  w .  ja  v  a  2 s .  com
        Process process = new ProcessBuilder(cmdLine).start();
        process.waitFor();
        return new String(IOUtils.toByteArray(process.getInputStream()));
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.crushpaper.ThreadedSynchronousStreamReader.java

/**
 * Synchronously forks a child process capturing its standard out and err
 * into StringBuffers. Returns the child process's exit code or -1 if it is
 * not available. It creates threads for each stream because that is
 * required in Java to avoid the possibility of deadlock.
 *///  w  w  w . j  a v a 2s.c o  m
public static int exec(StringBuffer output, StringBuffer error, String args[]) {
    try {
        String osName = System.getProperty("os.name");
        if (osName.equals("Windows 95")) {
            String[] prefix = new String[2];
            prefix[0] = "command.com";
            prefix[1] = "/C";
            args = ArrayUtils.addAll(prefix, args);
        } else if (osName.startsWith("Windows")) {
            String[] prefix = new String[2];
            prefix[0] = "cmd.exe";
            prefix[1] = "/C";
            args = ArrayUtils.addAll(prefix, args);
        }

        Runtime runtime = Runtime.getRuntime();
        Process childProcess = runtime.exec(args);
        ReaderThread errorReader = new ReaderThread(childProcess.getErrorStream(), output);
        ReaderThread outputReader = new ReaderThread(childProcess.getInputStream(), error);
        errorReader.start();
        outputReader.start();
        int exitValue = childProcess.waitFor();
        return exitValue;
    } catch (Throwable t) {
        t.printStackTrace();
    }

    return -1;
}

From source file:com.googlecode.jsendnsca.MessagePayloadTest.java

private static String getShortHostNameFromOS() throws Exception {
    final Runtime runtime = Runtime.getRuntime();
    final Process process = runtime.exec("hostname");
    final BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

    final String expectedHostName = input.readLine();
    input.close();/*  w  w w.j a  v  a2s  .c  o  m*/
    assertEquals(0, process.waitFor());

    return expectedHostName;
}

From source file:com.k42b3.sacmis.ExecutorAbstract.java

/**
 * Returns whether the command returns an response that contains the given 
 * string/*  www .j av a 2 s.  c  o m*/
 * 
 * @param String cmd
 * @param String contains
 * @return boolean
 * @throws IOException 
 */
public static boolean hasExecutable(String cmd, String contains) throws IOException {
    Process process = Runtime.getRuntime().exec(cmd);
    BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    StringBuilder response = new StringBuilder();
    while ((line = input.readLine()) != null) {
        response.append(line);
    }
    input.close();

    return response.toString().indexOf(contains) != -1;
}

From source file:de.rub.syssec.saaf.analysis.steps.hash.SSDeep.java

protected static String calculateFuzzyHash(File f) throws IOException {
    String hash = null;//from   w w  w.  ja v a 2  s.  co m

    if (SSDEEP_PATH != null) {
        ProcessBuilder pb = new ProcessBuilder(SSDEEP_PATH, f.getAbsolutePath());
        Process proc;
        Scanner in = null;
        try {
            proc = pb.start();
            // Start reading from the program
            in = new Scanner(proc.getInputStream());
            while (in.hasNextLine()) {
                hash = in.nextLine();
            }
            if (hash != null)
                return hash.substring(0, hash.lastIndexOf(","));
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (Exception ignored) {
            }
        }
    } else {
        LOGGER.warn("exec_ssdeep could not be found in saaf.conf");
    }
    return "";
}

From source file:io.grpc.alts.CheckGcpEnvironment.java

private static boolean isRunningOnGcp() {
    try {/*from   w w  w.j  a va2  s . c  o  m*/
        if (SystemUtils.IS_OS_LINUX) {
            // Checks GCE residency on Linux platform.
            return checkProductNameOnLinux(Files.newBufferedReader(Paths.get(DMI_PRODUCT_NAME), UTF_8));
        } else if (SystemUtils.IS_OS_WINDOWS) {
            // Checks GCE residency on Windows platform.
            Process p = new ProcessBuilder().command(WINDOWS_COMMAND, "Get-WmiObject", "-Class", "Win32_BIOS")
                    .start();
            return checkBiosDataOnWindows(new BufferedReader(new InputStreamReader(p.getInputStream(), UTF_8)));
        }
    } catch (IOException e) {
        logger.log(Level.WARNING, "Fail to read platform information: ", e);
        return false;
    }
    // Platforms other than Linux and Windows are not supported.
    return false;
}

From source file:com.cloudera.sqoop.util.Executor.java

/**
 * Run a command via Runtime.exec(), with its stdout and stderr streams
 * directed to be handled by threads generated by AsyncSinks.
 * Block until the child process terminates. Allows the programmer to
 * specify an environment for the child program.
 *
 * @return the exit status of the ran program
 *///www .ja v  a 2 s.  co m
public static int exec(String[] args, String[] envp, AsyncSink outSink, AsyncSink errSink) throws IOException {

    // launch the process.
    Process p = Runtime.getRuntime().exec(args, envp);

    // dispatch its stdout and stderr to stream sinks if available.
    if (null != outSink) {
        outSink.processStream(p.getInputStream());
    }

    if (null != errSink) {
        errSink.processStream(p.getErrorStream());
    }

    // wait for the return value.
    while (true) {
        try {
            int ret = p.waitFor();
            return ret;
        } catch (InterruptedException ie) {
            continue;
        }
    }
}

From source file:Main.java

public static boolean isRooted() {
    Process p;
    try {// w w  w  . jav  a2 s  .  co  m
        p = new ProcessBuilder("su").start();
        BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));

        stdin.write("whoami");
        stdin.newLine();
        stdin.write("exit");
        stdin.newLine();
        stdin.close();
        try {
            p.waitFor();
            if (!stdout.ready())
                return false;
            String user = stdout.readLine(); //We only expect one line of output
            stdout.close();
            if (user == "root") {
                return true;
            } else {
                return false;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static String runCommand(String[] commands) {
    DataOutputStream outStream = null;
    DataInputStream responseStream;
    try {/*from  w w w  . j  a v  a 2  s .  c om*/
        ArrayList<String> logs = new ArrayList<String>();
        Process process = Runtime.getRuntime().exec("su");
        Log.i(TAG, "Executed su");
        outStream = new DataOutputStream(process.getOutputStream());
        responseStream = new DataInputStream(process.getInputStream());

        for (String single : commands) {
            Log.i(TAG, "Command = " + single);
            outStream.writeBytes(single + "\n");
            outStream.flush();
            if (responseStream.available() > 0) {
                Log.i(TAG, "Reading response");
                logs.add(responseStream.readLine());
                Log.i(TAG, "Read response");
            } else {
                Log.i(TAG, "No response available");
            }
        }
        outStream.writeBytes("exit\n");
        outStream.flush();
        String log = "";
        for (int i = 0; i < logs.size(); i++) {
            log += logs.get(i) + "\n";
        }
        Log.i(TAG, "Execution compeleted");
        return log;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
    return null;
}