Example usage for java.io DataOutputStream writeBytes

List of usage examples for java.io DataOutputStream writeBytes

Introduction

In this page you can find the example usage for java.io DataOutputStream writeBytes.

Prototype

public final void writeBytes(String s) throws IOException 

Source Link

Document

Writes out the string to the underlying output stream as a sequence of bytes.

Usage

From source file:Main.java

public static void runAsRoot(String[] commands) throws IOException, InterruptedException {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    for (String cmd : commands) {
        os.writeBytes(cmd + "\n");
    }//from   w  w w .  j av a2s . c o  m
    os.writeBytes("exit\n");
    os.flush();
    os.close();
    p.waitFor();
}

From source file:Main.java

public static Process runSuCommandAsync(Context context, String command) throws IOException {
    DataOutputStream fout = new DataOutputStream(context.openFileOutput(SCRIPT_NAME, 0));
    fout.writeBytes(command);
    fout.close();//from w  w  w. ja va 2s .  c o m

    String[] args = new String[] { "su", "-c",
            ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME };
    Process proc = Runtime.getRuntime().exec(args);
    return proc;
}

From source file:Main.java

public static String do_exec_with_root(String cmd) {
    String s = "\n";
    try {/*from www  . j a va  2s  . 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 void RunAsRootNoOutput(String[] cmds) {
    try {/*from ww w  .j  av a  2s .  c o  m*/
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean executeAsRoot(String command) {
    try {/*from ww  w.ja  v  a  2 s  .c o  m*/

        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 void RunAsRoot(String string) throws IOException {
    Process P = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(P.getOutputStream());
    os.writeBytes(string + "\n");
    os.writeBytes("exit\n");
    os.flush();//  ww  w  . j  a va2s .co m
}

From source file:Main.java

public static Process runSuCommandAsync_prev(Context context, String command) throws IOException {
    DataOutputStream fout_prev = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_prev, 0));
    fout_prev.writeBytes(command);
    fout_prev.close();/*from w  w w  .  j  av  a  2s . c  o m*/

    String[] args_prev = new String[] { "su", "-c",
            ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME_prev };
    Process proc_prev = Runtime.getRuntime().exec(args_prev);
    return proc_prev;
}

From source file:Main.java

public static Process runSuCommandAsync_spica(Context context, String command) throws IOException {
    DataOutputStream fout_spica = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_spica, 0));
    fout_spica.writeBytes(command);
    fout_spica.close();//  w w w.  ja  va2s .c  o  m

    String[] args_spica = new String[] { "su", "-c",
            ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME_spica };
    Process proc_spica = Runtime.getRuntime().exec(args_spica);
    return proc_spica;
}

From source file:Main.java

public static Process runSuCommandAsync_miracle2(Context context, String command) throws IOException {
    DataOutputStream fout_miracle2 = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_miracle2, 0));
    fout_miracle2.writeBytes(command);
    fout_miracle2.close();/*from  w ww  . j  ava  2  s  .  co m*/

    String[] args_miracle2 = new String[] { "su", "-c",
            ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME_miracle2 };
    Process proc_miracle2 = Runtime.getRuntime().exec(args_miracle2);
    return proc_miracle2;
}

From source file:Main.java

public static Process runSuCommandAsync_prev2(Context context, String command) throws IOException {
    DataOutputStream fout_prev2 = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_prev2, 0));
    fout_prev2.writeBytes(command);
    fout_prev2.close();/*from www.j a  v  a 2s.  c om*/

    String[] args_prev2 = new String[] { "su", "-c",
            ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME_prev2 };
    Process proc_prev2 = Runtime.getRuntime().exec(args_prev2);
    return proc_prev2;
}