Example usage for java.io DataOutputStream flush

List of usage examples for java.io DataOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this data output stream.

Usage

From source file:Main.java

public static void killProcess(int pid) {
    Process sh = null;//  www . j  av  a  2s .c  o  m
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        final String Command = "kill -9 " + pid + "\n";
        os.writeBytes(Command);
        os.flush();
        sh.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.pubkit.network.PubKitNetwork.java

public static JSONObject sendPost(String apiKey, JSONObject jsonObject) {
    URL url;//from   www  .j a  v a  2s.  c  om
    HttpURLConnection connection = null;
    try {
        //Create connection
        url = new URL(PUBKIT_API_URL);
        String encodedData = jsonObject.toString();
        byte[] postDataBytes = jsonObject.toString().getBytes("UTF-8");

        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Length", "" + String.valueOf(postDataBytes.length));
        connection.setRequestProperty("api_key", apiKey);

        connection.setUseCaches(false);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(encodedData);//set data
        wr.flush();

        //Get Response
        InputStream inputStream = connection.getErrorStream(); //first check for error.
        if (inputStream == null) {
            inputStream = connection.getInputStream();
        }
        BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));

        String line;
        StringBuilder response = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        wr.close();
        rd.close();

        String responseString = response.toString();
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            return new JSONObject("{'error':'" + responseString + "'}");
        } else {
            try {
                return new JSONObject(responseString);
            } catch (JSONException e) {
                Log.e("PUBKIT", "Error parsing data", e);
            }
        }
    } catch (Exception e) {
        Log.e("PUBKIT", "Network exception:", e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
    return null;
}

From source file:Main.java

public static void forceStopAPK(String pkgName) {
    Process sh = null;/*from   ww  w . j  a va 2 s  .c om*/
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        final String Command = "am force-stop" + pkgName;
        os.writeBytes(Command);
        os.flush();
        sh.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static int execRootCmdSilent(String cmd) {
    try {//from  w ww .  j av a2 s  . co  m
        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 boolean runWithRoot(String command) {
    int result = -1;

    Process process = null;//ww  w .  ja v  a2s .  c  o  m
    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:com.android.dialer.omni.PlaceUtil.java

/**
 * Executes a post request and return a JSON object
 * @param url The API URL//  ww w . j a v a  2  s .c om
 * @param data The data to post in POST field
 * @return the JSON object
 * @throws IOException
 * @throws JSONException
 */
public static JSONObject postJsonRequest(String url, String postData) throws IOException, JSONException {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    con.setRequestMethod("POST");

    if (DEBUG)
        Log.d(TAG, "Posting: " + postData + " to " + url);

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(postData);
    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();

    JSONObject json = new JSONObject(response.toString());
    return json;
}

From source file:Main.java

public static void ExecuteNoReturn(String command, Boolean useroot, boolean forcenew) throws Exception {

    Process p;//from   w  w  w  .  j a  v a  2  s  .c om
    DataOutputStream os;

    p = getProcess(useroot, forcenew);

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

    os.writeBytes(command + "\n");

    if (forcenew) {
        os.writeBytes("exit\n");
        os.flush();
        os.close();
    }

    //p.waitFor();
}

From source file:tachyon.master.Image.java

/**
 * Write a new image to path. This method assumes having a lock on the master info.
 * //from   w  ww.j  ava 2 s  . c  om
 * @param info the master info to generate the image
 * @param path the new image path
 * @throws IOException
 */
public static void create(MasterInfo info, String path) throws IOException {
    String tPath = path + ".tmp";
    String parentFolder = path.substring(0, path.lastIndexOf(TachyonURI.SEPARATOR));
    LOG.info("Creating the image file: " + tPath);
    UnderFileSystem ufs = UnderFileSystem.get(path, info.getTachyonConf());
    if (!ufs.exists(parentFolder)) {
        LOG.info("Creating parent folder " + parentFolder);
        ufs.mkdirs(parentFolder, true);
    }
    OutputStream os = ufs.create(tPath);
    DataOutputStream imageOs = new DataOutputStream(os);
    ObjectWriter writer = JsonObject.createObjectMapper().writer();

    info.writeImage(writer, imageOs);
    imageOs.flush();
    imageOs.close();

    LOG.info("Succefully created the image file: " + tPath);
    ufs.delete(path, false);
    ufs.rename(tPath, path);
    ufs.delete(tPath, false);
    LOG.info("Renamed " + tPath + " to " + path);
    // safe to close, nothing created here with scope outside function
    ufs.close();
}

From source file:Main.java

public static boolean runRootCommand(Context context, String command) {
    Process process = null;/*  w  w w  . ja  v  a2 s.co m*/
    DataOutputStream os = null;
    try {
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (Exception e) {
        Log.d("*** DEBUG ***", "Error - " + e.getMessage());
        return false;
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            process.destroy();
        } catch (Exception e) {

        }
    }
    return true;
}

From source file:Main.java

public static boolean tempRunRootCommand(Context context, String command) {
    Process process = null;/*from   ww  w .ja va2s.c o  m*/
    DataOutputStream os = null;
    try {
        process = Runtime.getRuntime().exec("su1");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (Exception e) {
        Log.d("*** DEBUG ***", "Error - " + e.getMessage());
        return false;
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            process.destroy();
        } catch (Exception e) {

        }
    }
    return true;
}