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:gov.nasa.arc.geocam.geocam.HttpPost.java

public static int post(String url, JSONObject json, String username, String password) throws IOException {
    HttpURLConnection conn = createConnection(url, username, password);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setRequestProperty("Content-Type", "application/json");

    byte[] jsonBytes = json.toString().getBytes("UTF-8");

    conn.setRequestProperty("Content-Length", Integer.toString(jsonBytes.length));

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.write(jsonBytes);/*from w w w. ja  v  a2s.c  o m*/
    out.flush();

    InputStream in = conn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in), 2048);

    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    }
    out.close();

    int responseCode = 0;
    responseCode = conn.getResponseCode();
    return responseCode;
}

From source file:com.ibm.mobilefirst.mobileedge.utils.RequestUtils.java

public static void executeTrainRequest(String name, String uuid, List<AccelerometerData> accelerometerData,
        List<GyroscopeData> gyroscopeData, RequestResult requestResult) throws IOException {

    URL url = new URL("https://medge.mybluemix.net/alg/train");

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setReadTimeout(5000);// w  w  w . jav a 2  s  . c  o m
    conn.setConnectTimeout(10000);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");

    conn.setDoInput(true);
    conn.setDoOutput(true);

    JSONObject jsonToSend = createTrainBodyJSON(name, uuid, accelerometerData, gyroscopeData);

    OutputStream outputStream = conn.getOutputStream();
    DataOutputStream wr = new DataOutputStream(outputStream);
    wr.writeBytes(jsonToSend.toString());
    wr.flush();
    wr.close();

    outputStream.close();

    String response = "";

    int responseCode = conn.getResponseCode();

    //Log.e("BBB2","" + responseCode);

    if (responseCode == HttpsURLConnection.HTTP_OK) {
        String line;
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line = br.readLine()) != null) {
            response += line;
        }
    } else {
        response = "{}";
    }

    handleResponse(response, requestResult);
}

From source file:ke.co.tawi.babblesms.server.servlet.accountmngmt.Login.java

public static boolean validateCaptcha(String gRecaptchaResponse) throws IOException {
    if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
        return false;
    }/*from   w ww .  j  a v a  2  s .com*/

    try {
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        // add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String postParams = "secret=" + secret + "&response=" + gRecaptchaResponse;

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(postParams);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + postParams);
        System.out.println("Response Code : " + responseCode);

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

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

        // print result
        System.out.println(response.toString());

        //parse JSON response and return 'success' value
        JsonReader jsonReader = Json.createReader(new StringReader(response.toString()));
        JsonObject jsonObject = jsonReader.readObject();
        jsonReader.close();

        return jsonObject.getBoolean("success");
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.clearcenter.mobile_demo.mdRest.java

static public String Login(String host, String username, String password, String token) throws JSONException {
    if (password == null)
        Log.i(TAG, "Login by cookie, host: " + host + ", username: " + username);
    else//from w  ww  . jav  a  2s .  c  o  m
        Log.i(TAG, "Login by password, host: " + host + ", username: " + username);

    try {
        URL url = new URL("https://" + host + URL_LOGIN);

        HttpsURLConnection http = CreateConnection(url);

        if (password != null) {
            // Setup HTTPS POST request
            String urlParams = "username=" + URLEncoder.encode(username, "UTF-8") + "&password="
                    + URLEncoder.encode(password, "UTF-8") + "&submit=submit";

            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            http.setRequestProperty("Content-Length", Integer.toString(urlParams.getBytes().length));

            // Write request
            DataOutputStream outputStream = new DataOutputStream(http.getOutputStream());
            outputStream.writeBytes(urlParams);
            outputStream.flush();
            outputStream.close();
        } else {
            http.setRequestMethod("GET");
            http.setRequestProperty("Cookie", token);
        }

        final StringBuffer response = ProcessRequest(http);

        // Process response
        JSONObject json_data = null;
        try {
            Log.i(TAG, "response: " + response.toString());
            json_data = new JSONObject(response.toString());
        } catch (JSONException e) {
            Log.e(TAG, "JSONException", e);
            return "";
        }
        if (json_data.has("result")) {
            final String cookie = http.getHeaderField("Set-Cookie");
            Integer result = RESULT_UNKNOWN;
            try {
                result = Integer.valueOf(json_data.getString("result"));
            } catch (NumberFormatException e) {
            }

            Log.i(TAG, "result: " + result.toString() + ", cookie: " + cookie);

            if (result == RESULT_SUCCESS) {
                if (cookie != null)
                    return cookie;
                else
                    return token;
            }

            // All other results are failures...
            return "";
        }
    } catch (Exception e) {
        Log.e(TAG, "Exception", e);
    }

    Log.i(TAG, "Malformed result");
    return "";
}

From source file:com.cloudera.impala.security.PersistedDelegationTokenSecretManager.java

public static String encodeWritable(Writable key) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    key.write(dos);//from  w w  w. ja  v a 2  s .  c om
    dos.flush();
    return Base64.encodeBase64URLSafeString(bos.toByteArray());
}

From source file:Main.java

public static String sudoForResult(String... strings) {
    String res = "";
    DataOutputStream outputStream = null;
    InputStream response = null;/*from ww w.  j av a2s  . 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:edu.harvard.iq.dvn.unf.Base64Encoding.java

/**
 *
 * @param digest byte array/*w w  w.j  a va 2 s .  c o m*/
 * @param enc String with final encoding
 * @return String the encoded base64 of digest
 * @throws UnsupportedEncodingException
 */
public static String tobase64(byte[] digest, String enc) throws UnsupportedEncodingException {

    ByteArrayOutputStream btstream = new ByteArrayOutputStream();
    //this make sure is written in big-endian

    DataOutputStream stream = new DataOutputStream(btstream);

    byte[] tobase64 = null;
    byte[] revdigest = new byte[digest.length];
    revdigest = changeByteOrder(digest, ByteOrder.nativeOrder());
    try {

        stream.write(revdigest);
        stream.flush();

        tobase64 = Base64.encodeBase64(btstream.toByteArray());

    } catch (IOException io) {
        tobase64 = Base64.encodeBase64(digest);
    }

    return new String(tobase64, enc);
}

From source file:com.chintans.venturebox.util.Utils.java

public static String exec(String command) {
    try {/*from w w  w .  ja va 2  s .c  o m*/
        Process p = Runtime.getRuntime().exec(command);
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        os.writeBytes("sync\n");
        os.writeBytes("exit\n");
        os.flush();
        p.waitFor();
        return getStreamLines(p.getInputStream());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static int execRootCmdForExitCode(String[] cmds) {
    int result = -1;
    DataOutputStream dos = null;
    DataInputStream dis = null;//from w  w  w .ja v  a2s .c o m

    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

public static String execRootCmd(String[] cmds) {
    String result = "";
    DataOutputStream dos = null;
    DataInputStream dis = null;//from  w  ww  .  j  a v  a  2 s  .  c  o  m

    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);
            result += line;
        }
        p.waitFor();
    } 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;
}