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

From source file:Main.java

private static boolean installOrUninstallApk(String apkPath, String installOruninstall, String rOrP) {
    Process process = null;//  w  ww.j a v  a  2s  .c  o  m
    DataOutputStream os = null;
    String command = null;
    try {
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        command = "pm " + installOruninstall + " " + rOrP + " " + apkPath + " \n";
        os.writeBytes(command);
        os.flush();
        os.close();
        process.waitFor();
        process.destroy();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.enonic.cms.framework.util.UUIDGenerator.java

/**
 * Generate random uuid./*from w  w w .  j  av a  2 s  .co m*/
 */
public static String randomUUID() {
    UUID uuid = UUID.randomUUID();

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream dataOut = new DataOutputStream(out);
        dataOut.writeLong(uuid.getMostSignificantBits());
        dataOut.writeLong(uuid.getLeastSignificantBits());
        dataOut.close();
        return new String(Hex.encodeHex(out.toByteArray()));
    } catch (Exception e) {
        return uuid.toString();
    }
}

From source file:Main.java

public static String sudoForResult(String... strings) {
    String res = "";
    DataOutputStream outputStream = null;
    InputStream response = null;//  w  w  w.j a v  a 2  s .  c o m
    try {
        Process su = Runtime.getRuntime().exec("su");
        outputStream = new DataOutputStream(su.getOutputStream());
        response = su.getInputStream();

        for (String s : strings) {
            outputStream.writeBytes(s + "\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        res = readFully(response);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeSilently(outputStream, response);
    }
    return res;
}

From source file:Main.java

private static HttpURLConnection defineHttpsURLConnection(HttpURLConnection connection, JSONObject jsonObject) {
    try {//from  w w  w .j a  v  a2 s.  co m
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        /* Add headers to the request */
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(jsonObject.toString().getBytes(Charset.forName("UTF-8")));
        wr.flush();
        wr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return connection;
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {

    String cmd = "kill -9 " + pid;
    String Command = "am force-stop " + processName + "\n";
    Process sh = null;/*from   w  w w  .j  ava 2  s .c om*/
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        os.writeBytes(Command + "\n");
        os.writeBytes(cmd + "\n");
        os.writeBytes("exit\n");
        os.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        sh.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);
    Log.i("AppUtil", processName);
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = null;
    try {
        if (processName.indexOf(":") == -1) {
            packageName = processName;
        } else {
            packageName = processName.split(":")[0];
        }

        activityManager.killBackgroundProcesses(packageName);

        //
        Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage",
                String.class);
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(activityManager, packageName);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static byte[] getBodyBytes(String ip, String port, String key)
        throws NumberFormatException, IOException {

    String[] ipArr = ip.split("\\.");
    byte[] ipByte = new byte[4];
    ipByte[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
    ipByte[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
    ipByte[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
    ipByte[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.write(ipByte);//from  w  ww .j  av  a 2 s.  c  o m
    dos.writeShort(Short.parseShort(port));
    dos.writeByte(key.getBytes().length);
    dos.write(key.getBytes());
    byte[] bs = baos.toByteArray();
    baos.close();
    dos.close();

    return bs;
}

From source file:Main.java

public static String RunExecCmd(StringBuilder sb, Boolean su) {

    String shell;/*w  w w. jav a 2s . co m*/
    if (su) {
        shell = "su";
    } else {
        shell = "sh";
    }

    Process process = null;

    DataOutputStream processOutput = null;
    InputStream processInput = null;
    String outmsg = new String();

    try {

        process = Runtime.getRuntime().exec(shell);
        processOutput = new DataOutputStream(process.getOutputStream());
        processOutput.writeBytes(sb.toString() + "\n");
        processOutput.writeBytes("exit\n");
        processOutput.flush();
        processInput = process.getInputStream();
        outmsg = inputStream2String(processInput, "UTF-8");
        process.waitFor();

    } catch (Exception e) {
        //Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
        //return;
    }

    finally {
        try {
            if (processOutput != null) {
                processOutput.close();
            }
            process.destroy();
        } catch (Exception e) {
        }

    }
    return outmsg;
}

From source file:Main.java

public static BufferedReader shellExecute(String[] commands, boolean requireRoot) {
    Process shell = null;//from  w  w  w. java2s. c o  m
    DataOutputStream out = null;
    BufferedReader reader = null;
    String startCommand = requireRoot ? "su" : "sh";
    try {
        shell = Runtime.getRuntime().exec(startCommand);
        out = new DataOutputStream(shell.getOutputStream());
        reader = new BufferedReader(new InputStreamReader(shell.getInputStream()));
        for (String command : commands) {
            out.writeBytes(command + "\n");
            out.flush();
        }
        out.writeBytes("exit\n");
        out.flush();
        shell.waitFor();
    } catch (Exception e) {
        Log.e(TAG, "ShellRoot#shExecute() finished with error", e);
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (Exception e) {

        }
    }
    return reader;
}

From source file:Main.java

public static byte[] getBodyBytes(String ip, String port, byte[] key, byte[] ips)
        throws NumberFormatException, IOException {

    String[] ipArr = ip.split("\\.");
    byte[] ipByte = new byte[4];
    ipByte[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
    ipByte[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
    ipByte[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
    ipByte[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.write(ipByte);/*  ww w . j a  v a 2 s  .c o m*/
    if (!isNum(port))
        throw new NumberFormatException("port is not number...");
    byte[] portByte = short2bytes(Integer.parseInt(port));
    dos.write(portByte);
    //        dos.writeByte(key.getBytes().length);
    dos.write(key);
    if (ips != null && ips.length > 0 && dos != null) {
        dos.write(ips);
    }
    byte[] bs = baos.toByteArray();
    baos.close();
    dos.close();

    return bs;
}