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 String execRootCmd(String cmd) {
    String result = "result : ";
    try {/*www.  j  ava 2s  .  com*/
        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 Process runSuCommandAsync_miracle2(Context context, String command) throws IOException {
    DataOutputStream fout_miracle2 = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_miracle2, 0));
    fout_miracle2.writeBytes(command);//from  w  w w. j a v a  2 s  .com
    fout_miracle2.close();

    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:gaffer.utils.WritableToStringConverter.java

public static String serialiseToString(Writable writable) throws IOException {
    // Serialise to a byte array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(baos);
    Text.writeString(out, writable.getClass().getName());
    writable.write(out);/*from www .ja v  a 2 s.  c o m*/

    // Convert the byte array to a base64 string
    return Base64.encodeBase64String(baos.toByteArray());
}

From source file:com.aliyun.openservices.tablestore.hadoop.Utils.java

static String serialize(Writable w) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(os);
    try {/*from  ww w .j a va2s . c om*/
        w.write(out);
        out.close();
    } catch (IOException ex) {
        // intend to ignore
    }
    byte[] buf = os.toByteArray();
    return Base64.encodeBase64String(buf);
}

From source file:Main.java

public static String getSuVersion() {
    Process process = null;//from  w  w  w.  j  av a 2 s  . c o  m
    String inLine = null;

    try {
        process = Runtime.getRuntime().exec("sh");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        BufferedReader is = new BufferedReader(
                new InputStreamReader(new DataInputStream(process.getInputStream())), 64);
        os.writeBytes("su -v\n");

        // We have to hold up the thread to make sure that we're ready to read
        // the stream, using increments of 5ms makes it return as quick as
        // possible, and limiting to 1000ms makes sure that it doesn't hang for
        // too long if there's a problem.
        for (int i = 0; i < 400; i++) {
            if (is.ready()) {
                break;
            }
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                Log.w(TAG, "Sleep timer got interrupted...");
            }
        }
        if (is.ready()) {
            inLine = is.readLine();
            if (inLine != null) {
                return inLine;
            }
        } else {
            os.writeBytes("exit\n");
        }
    } catch (IOException e) {
        Log.e(TAG, "Problems reading current version.", e);
        return null;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }

    return null;
}

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);/*from  w w w  .  java2  s . c o  m*/
    fout_prev.close();

    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);//ww w. j  a  v  a  2  s  . com
    fout_spica.close();

    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_spica2(Context context, String command) throws IOException {
    DataOutputStream fout_spica2 = new DataOutputStream(context.openFileOutput(SCRIPT_NAME_spica2, 0));
    fout_spica2.writeBytes(command);/*w  w w .  j a va2s.  c o m*/
    fout_spica2.close();

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

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);//  w  w w  .java 2s .  c o m
    fout_prev2.close();

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

From source file:editor.util.URLUtil.java

public static String sendPost(String url, String data) throws Exception {

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");

    // Send post request
    con.setDoOutput(true);/* w ww .  ja v  a2s . co  m*/
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(data);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    return response.toString();
}