Example usage for java.io DataOutputStream DataOutputStream

List of usage examples for java.io DataOutputStream DataOutputStream

Introduction

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

Prototype

public DataOutputStream(OutputStream out) 

Source Link

Document

Creates a new data output stream to write data to the specified underlying output stream.

Usage

From source file:Main.java

public static byte[] convertIntToByteArray(int my_int) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeInt(my_int);
    dos.close();/*from   w ww .  j  av  a  2s .  c o m*/
    byte[] int_bytes = bos.toByteArray();
    bos.close();
    return int_bytes;
}

From source file:Main.java

public static Process execRuntimeProcess(String cmd) {
    Runtime localRuntime = Runtime.getRuntime();
    try {/*from w  w  w  .  j  a v a 2 s .c  om*/
        Process pro = localRuntime.exec("su");
        DataOutputStream out = new DataOutputStream(pro.getOutputStream());
        out.writeBytes(cmd + " \n");
        return pro;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] intToByteArray(int number) {
    try {/*from  ww  w. ja va 2 s.  c o m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeInt(number);
        dos.flush();
        // lenth have to be 2 byte
        byte[] d = bos.toByteArray();
        dos.close();

        return d;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] longToByteArray(long number) {
    try {/* w w  w  .ja v a  2s  .c o m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeLong(number);
        dos.flush();
        // lenth have to be 2 byte
        byte[] d = bos.toByteArray();
        dos.close();

        return d;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] shortToByteArray(short number) {
    try {/*from  w w  w  .  ja  v  a  2s  . c  o  m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeShort(number);
        dos.flush();
        // lenth have to be 2 byte
        byte[] d = bos.toByteArray();
        dos.close();

        return d;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static boolean runWithRoot(String command) {
    int result = -1;

    Process process = null;//  ww  w.  j  av a2  s.com
    DataOutputStream os = null;
    try {
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        result = process.waitFor();
    } catch (Exception e) {
        return false;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }

    return result == 1;
}

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;/*from  www  .  j  av a 2  s  .  c  o  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 boolean isRoot() {
    Process process = null;//from   ww  w . j  a  va 2  s  . c  o  m
    DataOutputStream dos = null;
    try {
        process = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(process.getOutputStream());
        dos.writeBytes("exit\n");
        dos.flush();
        int exitValue = process.waitFor();
        if (exitValue == 0) {
            return true;
        }
    } catch (IOException e) {

    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean executeAsRoot(String command) {
    try {//from   ww  w .j a  va  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 int execRootCmdSilent(String cmd) {

    int result = -1;
    DataOutputStream dos = null;//from  w w w.  ja va2s.  c om

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

        dos.writeBytes(cmd + "\n");
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();
        p.waitFor();
        result = p.exitValue();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}