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:com.ning.arecibo.util.timeline.times.TimesAndSamplesCoder.java

public static byte[] combineTimesAndSamples(final byte[] times, final byte[] samples) {
    final int totalSamplesSize = 4 + times.length + samples.length;
    final ByteArrayOutputStream baStream = new ByteArrayOutputStream(totalSamplesSize);
    final DataOutputStream outputStream = new DataOutputStream(baStream);
    try {//  w ww. j a  v  a2 s  . co m
        outputStream.writeInt(times.length);
        outputStream.write(times);
        outputStream.write(samples);
        outputStream.flush();
        return baStream.toByteArray();
    } catch (IOException e) {
        throw new IllegalStateException(String.format(
                "Exception reading timeByteCount in TimelineChunkMapper.map() for times %s, samples %s",
                Hex.encodeHex(times), Hex.encodeHex(samples)), e);
    }
}

From source file:PipedBytes.java

public static void writeStuff(OutputStream rawOut) {
    try {//from   w  w w.j  av  a2  s.c  o m
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(rawOut));

        int[] data = { 82, 105, 99, 104, 97, 114, 100, 32, 72, 121, 100, 101 };

        for (int i = 0; i < data.length; i++) {
            out.writeInt(data[i]);
        }

        out.flush();
        out.close();
    } catch (IOException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static String executeHttpsPost(String url, String data, InputStream key) {
    HttpsURLConnection localHttpsURLConnection = null;
    try {//from  www.  jav a  2 s. co  m
        URL localURL = new URL(url);
        localHttpsURLConnection = (HttpsURLConnection) localURL.openConnection();
        localHttpsURLConnection.setRequestMethod("POST");
        localHttpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        localHttpsURLConnection.setRequestProperty("Content-Length",
                "" + Integer.toString(data.getBytes().length));
        localHttpsURLConnection.setRequestProperty("Content-Language", "en-US");

        localHttpsURLConnection.setUseCaches(false);
        localHttpsURLConnection.setDoInput(true);
        localHttpsURLConnection.setDoOutput(true);

        localHttpsURLConnection.connect();
        Certificate[] arrayOfCertificate = localHttpsURLConnection.getServerCertificates();

        byte[] arrayOfByte1 = new byte[294];
        DataInputStream localDataInputStream = new DataInputStream(key);
        localDataInputStream.readFully(arrayOfByte1);
        localDataInputStream.close();

        Certificate localCertificate = arrayOfCertificate[0];
        PublicKey localPublicKey = localCertificate.getPublicKey();
        byte[] arrayOfByte2 = localPublicKey.getEncoded();

        for (int i = 0; i < arrayOfByte2.length; i++) {
            if (arrayOfByte2[i] != arrayOfByte1[i])
                throw new RuntimeException("Public key mismatch");
        }

        DataOutputStream localDataOutputStream = new DataOutputStream(
                localHttpsURLConnection.getOutputStream());
        localDataOutputStream.writeBytes(data);
        localDataOutputStream.flush();
        localDataOutputStream.close();

        InputStream localInputStream = localHttpsURLConnection.getInputStream();
        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream));

        StringBuffer localStringBuffer = new StringBuffer();
        String str1;
        while ((str1 = localBufferedReader.readLine()) != null) {
            localStringBuffer.append(str1);
            localStringBuffer.append('\r');
        }
        localBufferedReader.close();

        return localStringBuffer.toString();
    } catch (Exception localException) {
        byte[] arrayOfByte1;
        localException.printStackTrace();
        return null;
    } finally {
        if (localHttpsURLConnection != null)
            localHttpsURLConnection.disconnect();
    }
}

From source file:com.flexdesktop.connections.restfulConnection.java

public static void postRequest(HttpURLConnection connection, String data) {
    DataOutputStream wr = null;
    try {//from ww  w.  ja  va 2s.  c  o  m
        wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(data);
        wr.flush();
        wr.close();
    } catch (IOException ex) {
        Logger.getLogger(com.flexdesktop.connections.restfulConnection.class.getName()).log(Level.SEVERE, null,
                ex);
    }

}

From source file:Main.java

public static void execCommand(String paramString) {
    try {/*from  w w w .j  a v a  2  s  . c o  m*/
        String[] arrayOfString = new String[1];
        arrayOfString[0] = paramString;
        DataOutputStream localDataOutputStream = new DataOutputStream(
                Runtime.getRuntime().exec("su").getOutputStream());
        for (int i = 0;; i++) {
            if (i >= 1) {
                localDataOutputStream.writeBytes("exit\n");
                localDataOutputStream.flush();
                localDataOutputStream.wait();
                return;
            }
            localDataOutputStream.writeBytes(arrayOfString[i] + "\n");
            localDataOutputStream.flush();
        }
    } catch (Exception localException) {
    }
}

From source file:flexpos.restfulConnection.java

public static void postRequest(HttpURLConnection connection, String data) {
    DataOutputStream wr = null;
    try {//from  ww  w .j av  a  2 s  . co  m
        wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(data);
        wr.flush();
        wr.close();
    } catch (IOException ex) {
        Logger.getLogger(restfulConnection.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Main.java

public static void execCmd(String cmd) {
    DataOutputStream dos = null;
    DataInputStream dis = null;//from  ww  w  .j  a  va 2  s  .  c o m
    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        cmd += "\n";
        dos.writeBytes(cmd);
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();
        p.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dos != null)
                dos.close();
            if (dis != null)
                dis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:yodlee.ysl.api.io.HTTP.java

public static String doPutNew(String url, String param, Map<String, String> sessionTokens)
        throws IOException, URISyntaxException {
    String mn = "doIO(PUT :" + url + ", sessionTokens =  " + sessionTokens.toString() + " )";
    System.out.println(fqcn + " :: " + mn);
    //param=param.replace("\"", "%22").replace("{", "%7B").replace("}", "%7D").replace(",", "%2C").replace("[", "%5B").replace("]", "%5D").replace(":", "%3A").replace(" ", "+");
    String processedURL = url;//+"?MFAChallenge="+param;//"%7B%22loginForm%22%3A%7B%22formType%22%3A%22token%22%2C%22mfaTimeout%22%3A%2299380%22%2C%22row%22%3A%5B%7B%22id%22%3A%22token_row%22%2C%22label%22%3A%22Security+Key%22%2C%22form%22%3A%220001%22%2C%22fieldRowChoice%22%3A%220001%22%2C%22field%22%3A%5B%7B%22id%22%3A%22token%22%2C%22name%22%3A%22tokenValue%22%2C%22type%22%3A%22text%22%2C%22value%22%3A%22123456%22%2C%22isOptional%22%3Afalse%2C%22valueEditable%22%3Atrue%2C%22maxLength%22%3A%2210%22%7D%5D%7D%5D%7D%7D";
    URL myURL = new URL(processedURL);
    System.out.println(fqcn + " :: " + mn + ": Request URL=" + processedURL.toString());
    HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
    conn.setRequestMethod("PUT");
    conn.setRequestProperty("Accept-Charset", "UTF-8");
    conn.setRequestProperty("Content-Type", contentTypeJSON);
    conn.setRequestProperty("Authorization", sessionTokens.toString());
    conn.setDoOutput(true);/*from   www.  ja  v  a 2s.  c  om*/
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(param);
    wr.flush();
    wr.close();
    System.out.println(fqcn + " :: " + mn + " : " + "Sending 'HTTP PUT' request");
    int responseCode = conn.getResponseCode();
    System.out.println(fqcn + " :: " + mn + " : " + "Response Code : " + responseCode);
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuilder jsonResponse = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
        jsonResponse.append(inputLine);
    }
    in.close();
    System.out.println(fqcn + " :: " + mn + " : " + jsonResponse.toString());
    return new String(jsonResponse);
}

From source file:yodlee.ysl.api.io.HTTP.java

public static String doPost(String url, String requestBody) throws IOException {
    String mn = "doIO(POST : " + url + ", " + requestBody + " )";
    System.out.println(fqcn + " :: " + mn);
    URL restURL = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("User-Agent", userAgent);
    //conn.setRequestProperty("Content-Type", contentTypeURLENCODED);
    conn.setRequestProperty("Content-Type", contentTypeJSON);
    conn.setDoOutput(true);//  w ww. jav a2 s.  c  o m
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
    wr.writeBytes(requestBody);
    wr.flush();
    wr.close();
    int responseCode = conn.getResponseCode();
    System.out.println(fqcn + " :: " + mn + " : " + "Sending 'HTTP POST' request");
    System.out.println(fqcn + " :: " + mn + " : " + "Response Code : " + responseCode);
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuilder jsonResponse = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
        jsonResponse.append(inputLine);
    }
    in.close();
    System.out.println(fqcn + " :: " + mn + " : " + jsonResponse.toString());
    return new String(jsonResponse);
}

From source file:com.ant.myteam.gcm.POST2GCM.java

public static void post(String apiKey, Content content) {

    try {//from   w ww.  ja  v  a  2s. c o m

        // 1. URL
        URL url = new URL("https://android.googleapis.com/gcm/send");

        // 2. Open connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // 3. Specify POST method
        conn.setRequestMethod("POST");

        // 4. Set the headers
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);

        conn.setDoOutput(true);

        // 5. Add JSON data into POST request body

        //`5.1 Use Jackson object mapper to convert Contnet object into JSON
        ObjectMapper mapper = new ObjectMapper();

        // 5.2 Get connection output stream
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

        // 5.3 Copy Content "JSON" into
        mapper.writeValue(wr, content);

        // 5.4 Send the request
        wr.flush();

        // 5.5 close
        wr.close();

        // 6. Get the response
        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

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

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

        // 7. Print result
        System.out.println(response.toString());

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