Example usage for java.lang Process getOutputStream

List of usage examples for java.lang Process getOutputStream

Introduction

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

Prototype

public abstract OutputStream getOutputStream();

Source Link

Document

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

Usage

From source file:Main.java

/**
 * Run command as root.//  w  w  w .ja v a  2  s .  c  om
 * 
 * @param command
 * @return true, if command was successfully executed
 */
private static boolean runAsRoot(final String command) {
    try {

        Process pro = Runtime.getRuntime().exec("su");
        DataOutputStream outStr = new DataOutputStream(pro.getOutputStream());

        outStr.writeBytes(command);
        outStr.writeBytes("\nexit\n");
        outStr.flush();

        int retval = pro.waitFor();

        return (retval == 0);

    } catch (Exception e) {

        return false;

    }
}

From source file:Main.java

public static String runAsRoot(String[] cmds) throws Exception {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    InputStream is = p.getInputStream();
    String result = null;/* www.j  a va 2s  .  co m*/
    for (String tmpCmd : cmds) {
        os.writeBytes(tmpCmd + "\n");
        int readed = 0;
        byte[] buff = new byte[4096];
        while (is.available() <= 0) {
            try {
                Thread.sleep(5000);
            } catch (Exception ex) {
            }
        }

        while (is.available() > 0) {
            readed = is.read(buff);
            if (readed <= 0)
                break;
            String seg = new String(buff, 0, readed);
            result = seg; //result is a string to show in textview
        }
    }
    os.writeBytes("exit\n");
    os.flush();
    return result;
}

From source file:Main.java

public static void RunRootCmd(String cmd) {
    try {//  w w w  .  j a  v a 2 s . co m
        //
        // run our command using 'su' to gain root
        //
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());

        outputStream.writeBytes(cmd + "\n");
        outputStream.flush();

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static int execRootCmdSilent(String cmd) {
    try {// www  .  j  ava2s  .c om
        Process p = Runtime.getRuntime().exec("su ");
        Object obj = p.getOutputStream();
        DataOutputStream dOutStream = new DataOutputStream((OutputStream) obj);
        String str = String.valueOf(cmd);
        obj = str + "\n";
        dOutStream.writeBytes((String) obj);
        dOutStream.flush();
        dOutStream.writeBytes("exit\n");
        dOutStream.flush();
        p.waitFor();
        int result = p.exitValue();
        return (Integer) result;
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}

From source file:Main.java

public static void GetSUAccess() {
    Thread su = new Thread(new Thread() {
        public void run() {
            try {
                Log.i("SU", "Trying to grant access to port...");
                Process suProcess = Runtime.getRuntime().exec("su");

                DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

                if (null != os) {
                    os.writeBytes("chmod 666 /dev/ttymxc1 \n");
                    os.flush();//from  w  ww  .j  a  va 2 s. c  o m
                }
                Log.i("SU", "Granted Access to Port");
            } catch (IOException e) {
                Log.e("SU", "Grant SuperUser Access Failed");
            }
        }
    });
    su.start();
}

From source file:Main.java

public static String execRootCmd(String cmd) {
    String result = "result : ";
    try {// w w  w . ja  va2 s  . co  m
        Process p = Runtime.getRuntime().exec("su ");
        OutputStream outStream = p.getOutputStream();
        DataOutputStream dOutStream = new DataOutputStream(outStream);
        InputStream inStream = p.getInputStream();
        DataInputStream dInStream = new DataInputStream(inStream);
        String str1 = String.valueOf(cmd);
        String str2 = str1 + "\n";
        dOutStream.writeBytes(str2);
        dOutStream.flush();
        String str3 = null;
        String line = "";
        while ((line = dInStream.readLine()) != null) {
            Log.d("result", str3);
            str3 += line;
        }
        dOutStream.writeBytes("exit\n");
        dOutStream.flush();
        p.waitFor();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return result;
    }
}

From source file:Main.java

public static void makeInternalCopy(Context c, String path, int resource) {
    InputStream is = c.getResources().openRawResource(resource);
    try {//w ww  .  j  a  va  2 s  .  co m

        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        File sniff = new File(path);
        if (sniff.exists()) {
            os.writeBytes("rm " + path + "\n");
        }
        byte[] bytes = new byte[is.available()];

        FileOutputStream setdbOutStream = new FileOutputStream(path);
        DataInputStream dis = new DataInputStream(is);
        dis.readFully(bytes);

        setdbOutStream.write(bytes);
        setdbOutStream.close();

        os.writeBytes("chmod 777 " + path + "\n");
        os.writeBytes("exit\n");
        os.flush();
    } catch (Exception e) {
        //           Toast.makeText(c, "Error Copying file: " + e.toString(),Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}

From source file:Main.java

public static String do_exec_with_root(String cmd) {
    String s = "\n";
    try {/*from ww  w. ja  v a2  s .  c  o m*/
        Process su_p = Runtime.getRuntime().exec("su");
        DataOutputStream dataOutputStream = new DataOutputStream(su_p.getOutputStream());
        dataOutputStream.writeBytes(cmd + "\n");
        dataOutputStream.writeBytes("exit" + "\n");
        dataOutputStream.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(su_p.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            s += line + "\n";
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return s;
}

From source file:Main.java

public static boolean executeAsRoot(String command) {
    try {/*from   w w w  .j  ava2  s  . c om*/

        Process suProcess = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

        os.writeBytes(command + "\n");
        os.flush();
        Log.d("ROOT", command);

        os.writeBytes("exit\n");
        os.flush();

        try {
            int suProcessRet = suProcess.waitFor();
            if (255 != suProcessRet)
                return true;
            else
                return false;
        } catch (Exception ex) {
            Log.w("ROOT-ERROR", ex);
        }
    } catch (IOException ex) {
        Log.w("ROOT", "Can't get root access", ex);
    } catch (SecurityException ex) {
        Log.w("ROOT", "Can't get root access", ex);
    } catch (Exception ex) {
        Log.w("ROOT", "Error executing internal operation", ex);
    }
    return false;
}

From source file:Main.java

public static int execRootCmdForExitCode(String[] cmds) {
    int result = -1;
    DataOutputStream dos = null;/*from w ww . j av  a 2s.  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;
}