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.esri.geoevent.test.performance.bds.BdsEventConsumer.java

private int getMsLayerCount(String url) {
    int cnt = -1;

    try {//from  w ww. ja  v a  2 s  .  com
        URL obj = new URL(url + "/query");
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

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

        con.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        con.setRequestProperty("Accept", "text/plain");

        String urlParameters = "where=" + URLEncoder.encode("1=1", "UTF-8") + "&returnCountOnly="
                + URLEncoder.encode("true", "UTF-8") + "&f=" + URLEncoder.encode("json", "UTF-8");

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

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

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

        //print result
        String jsonString = response.toString();

        JSONTokener tokener = new JSONTokener(jsonString);

        JSONObject root = new JSONObject(tokener);

        cnt = root.getInt("count");

    } catch (Exception e) {
        cnt = -2;
    } finally {
        return cnt;
    }

}

From source file:org.wso2.carbon.appmgt.sampledeployer.http.HttpHandler.java

public String doPostHttp(String backEnd, String payload, String your_session_id, String contentType)
        throws IOException {
    URL obj = new URL(backEnd);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    if (!your_session_id.equals("") && !your_session_id.equals("none")) {
        con.setRequestProperty("Cookie", "JSESSIONID=" + your_session_id);
    }//from w  w  w .  j a  v  a  2 s .  co m
    con.setRequestProperty("Content-Type", contentType);
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(payload);
    wr.flush();
    wr.close();
    int responseCode = con.getResponseCode();
    if (responseCode == 200) {
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        if (your_session_id.equals("")) {
            String session_id = response.substring((response.lastIndexOf(":") + 3),
                    (response.lastIndexOf("}") - 2));
            return session_id;
        } else if (your_session_id.equals("appmSamlSsoTokenId")) {
            return con.getHeaderField("Set-Cookie").split(";")[0].split("=")[1];
        } else if (your_session_id.equals("header")) {
            return con.getHeaderField("Set-Cookie").split("=")[1].split(";")[0];
        } else {
            return response.toString();
        }
    }
    return null;
}

From source file:de.ingrid.communication.authentication.BasicSchemeConnector.java

public boolean connect(Socket socket, String host, int port) throws IOException {
    DataInputStream dataInput = createInput(socket);
    DataOutputStream dataOutput = createOutput(socket);
    StringBuffer errorBuffer = new StringBuffer();
    String command = createConnectCommand(host, port);
    errorBuffer.append(command);/*w  w  w  .ja  va  2  s  . c o m*/
    dataOutput.write(command.getBytes());
    dataOutput.flush();
    boolean success = readMessageFromHttpProxy(dataInput, errorBuffer);
    if (!success) {
        if (LOG.isEnabledFor(Level.WARN)) {
            LOG.error(errorBuffer);
        }
    }
    return success;
}

From source file:org.wso2.carbon.appmgt.sampledeployer.http.HttpHandler.java

public String doPostHttps(String backEnd, String payload, String your_session_id, String contentType)
        throws IOException {
    URL obj = new URL(backEnd);

    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    if (!your_session_id.equals("")) {
        con.setRequestProperty("Cookie", "JSESSIONID=" + your_session_id);
    }// w  w  w .  j a  va 2 s.  com
    if (!contentType.equals("")) {
        con.setRequestProperty("Content-Type", contentType);
    }
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(payload);
    wr.flush();
    wr.close();
    int responseCode = con.getResponseCode();
    if (responseCode == 200) {
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        if (your_session_id.equals("")) {
            String session_id = response.substring((response.lastIndexOf(":") + 3),
                    (response.lastIndexOf("}") - 2));
            return session_id;
        } else if (your_session_id.equals("header")) {
            return con.getHeaderField("Set-Cookie");
        }

        return response.toString();
    }
    return null;
}

From source file:J2MEWriteReadMixedDataTypesExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);//from   w  w  w . j  ava2 s .co  m
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            byte[] outputRecord;
            String outputString = "First Record";
            int outputInteger = 15;
            boolean outputBoolean = true;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            outputDataStream.writeUTF(outputString);
            outputDataStream.writeBoolean(outputBoolean);
            outputDataStream.writeInt(outputInteger);
            outputDataStream.flush();
            outputRecord = outputStream.toByteArray();
            recordstore.addRecord(outputRecord, 0, outputRecord.length);
            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
            String inputString = null;
            int inputInteger = 0;
            boolean inputBoolean = false;
            byte[] byteInputData = new byte[100];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            for (int x = 1; x <= recordstore.getNumRecords(); x++) {
                recordstore.getRecord(x, byteInputData, 0);
                inputString = inputDataStream.readUTF();
                inputBoolean = inputDataStream.readBoolean();
                inputInteger = inputDataStream.readInt();
                inputStream.reset();
            }
            inputStream.close();
            inputDataStream.close();
            alert = new Alert("Reading", inputString + " " + inputInteger + " " + inputBoolean, null,
                    AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                RecordStore.deleteRecordStore("myRecordStore");
            }
        } catch (Exception error) {
            alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
    }
}

From source file:org.fosstrak.ale.server.type.FileSubscriberOutputChannel.java

/**
 * This method writes ec reports to a file.
 * /*  w ww . j a  va  2 s  .c om*/
 * @param reports to write to the file
 * @throws ImplementationException if an implementation exception occures
 */
private void writeNotificationToFile(ECReports reports) throws ImplementationException {
    // append reports as xml to file
    LOG.debug("Append reports '" + reports.getSpecName() + "' as xml to file '" + getPath() + "'.");

    File file = getFile();

    // create file if it does not already exists
    if (!file.exists() || !file.isFile()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            throw new ImplementationException("Could not create new file '" + getPath() + "'.", e);
        }
    }

    try {

        // open streams
        FileOutputStream fileOutputStream = new FileOutputStream(file, true);
        DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);

        // append reports as xml to file
        dataOutputStream.writeBytes(getPrettyXml(reports));
        dataOutputStream.writeBytes("\n\n");
        dataOutputStream.flush();

        // close streams
        dataOutputStream.close();
        fileOutputStream.close();

    } catch (IOException e) {
        throw new ImplementationException("Could not write to file '" + getPath() + "'.", e);
    }
}

From source file:cms.service.util.ItemUtility.java

public String sendPost(String url, String urlParams) throws Exception {

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

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

    // Send post request
    con.setDoOutput(true);/*from w  ww . java2  s.  com*/
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParams);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    logger.info("\nSending 'POST' request to URL : " + url);
    logger.info("Post parameters : " + urlParams);
    logger.info("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
    return (response.toString());

}

From source file:com.cisco.gerrit.plugins.slack.client.WebhookClient.java

/**
 * Initiates an HTTP POST to the provided Webhook URL.
 *
 * @param message The message payload./*from   w ww  . ja va2 s  .  c  o  m*/
 * @param webhookUrl The URL to post to.
 * @return The response payload from Slack.
 */
private String postRequest(String message, String webhookUrl) {
    String response;

    HttpURLConnection connection;
    connection = null;
    try {
        connection = openConnection(webhookUrl);
        try {
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("charset", "utf-8");

            connection.setDoInput(true);
            connection.setDoOutput(true);

            DataOutputStream request;
            request = new DataOutputStream(connection.getOutputStream());

            request.write(message.getBytes(StandardCharsets.UTF_8));
            request.flush();
            request.close();
        } catch (IOException e) {
            throw new RuntimeException("Error posting message to Slack: [" + e.getMessage() + "].", e);
        }

        response = getResponse(connection);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }

    return response;
}

From source file:buildhappy.tools.DownloadFile.java

/**
 * ?filepath???/*from   www.ja v  a  2 s .co  m*/
 */
private void saveToLocal(byte[] data, String filePath) {
    FileOutputStream fileOut = null;
    DataOutputStream dataOut = null;
    try {
        fileOut = new FileOutputStream(new File(filePath));
        dataOut = new DataOutputStream(fileOut);
        for (int i = 0; i < data.length; i++) {
            dataOut.write(data[i]);
        }
        dataOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (dataOut != null) {
            try {
                dataOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}