Example usage for java.io BufferedWriter flush

List of usage examples for java.io BufferedWriter flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:com.raphfrk.craftproxyclient.net.auth.AuthManager.java

public static JSONObject sendRequest(JSONObject request, String endpoint) {
    URL url;//from  w ww .ja v  a  2 s  . c  o  m
    try {
        url = new URL(authServer + "/" + endpoint);
    } catch (MalformedURLException e) {
        return null;
    }
    try {
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        con.setDoOutput(true);
        con.setInstanceFollowRedirects(false);
        con.setReadTimeout(5000);
        con.setConnectTimeout(5000);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.connect();

        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(con.getOutputStream(), StandardCharsets.UTF_8));
        try {
            request.writeJSONString(writer);
            writer.flush();
            writer.close();

            if (con.getResponseCode() != 200) {
                return null;
            }

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
            try {
                JSONParser parser = new JSONParser();

                try {
                    return (JSONObject) parser.parse(reader);
                } catch (ParseException e) {
                    return null;
                }
            } finally {
                reader.close();
            }
        } finally {
            writer.close();
            con.disconnect();
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static void writeContentToFile(String fileName, String contents) throws IOException {
    Log.d("writeContentToFile", fileName);
    File f = new File(fileName);
    f.getParentFile().mkdirs();//w  ww .j  av  a 2s .  c o m
    File tempFile = new File(fileName + ".tmp");
    FileWriter fw = new FileWriter(tempFile);
    BufferedWriter bw = new BufferedWriter(fw);
    int length = contents.length();
    if (length > 0) {
        bw.write(contents);
        //         int apart =  Math.min(length, 65536);
        //         int times = length / apart;
        //         for (int i = 0; i < times; i++) {
        //            bw.write(contents, i * apart, apart);
        //         }
        //         if (length % apart != 0) {
        //            bw.write(contents, times * apart, length - times * apart);
        //         }
        bw.flush();
        fw.flush();
        bw.close();
        fw.close();
        f.delete();
        tempFile.renameTo(f);
    }
}

From source file:gov.nih.nci.restgen.util.GeneratorUtil.java

public static void writeFile(String _outputDir, String _fileName, String _content) {
    BufferedWriter bufferedWriter = null;

    try {/*ww  w  . j  a v a 2s .c  o  m*/
        createOutputDir(_outputDir);
        File file = new File(_outputDir, _fileName);
        FileWriter fileWriter = new FileWriter(file);
        bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(_content);
        bufferedWriter.flush();
    } catch (Throwable t) {
        throw new RuntimeException(t);
    } finally {
        if (bufferedWriter != null) {
            try {
                bufferedWriter.close();
            } catch (Throwable t) {
            }
        }
    }
}

From source file:Messenger.TorLib.java

public static void postToURL(String hostname, int port, String postKey, String data) throws IOException {
    Socket socket = TorSocket(hostname, port);
    SSLSocketFactory sslSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null, socket.getPort(), false);
    sslSocket.setUseClientMode(true);/*w  w w .j av a2s .c  om*/
    sslSocket.startHandshake();
    String path = "/" + postKey;
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");

    wr.write(data);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
    sslSocket.close();
}

From source file:com.liferay.util.FileUtil.java

public static void write(File file, String s) throws IOException {
    if (file.getParent() != null) {
        mkdirs(file.getParent());/*from  w  w w.j ava  2  s . c o m*/
    }

    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

    bw.flush();
    bw.write(s);

    bw.close();
}

From source file:com.murrayc.galaxyzoo.app.provider.client.ZooniverseClient.java

private static void writeParamsToHttpPost(final HttpURLConnection conn,
        final List<NameValuePair> nameValuePairs) throws IOException {
    OutputStream out = null;//from  ww w. j a va2 s .  co  m
    try {
        out = conn.getOutputStream();

        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(out, Utils.STRING_ENCODING));
            writer.write(getPostDataBytes(nameValuePairs));
            writer.flush();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (final IOException e) {
                Log.error("writeParamsToHttpPost(): Exception while closing out", e);
            }
        }
    }
}

From source file:com.thoughtmetric.tl.TLLib.java

private static TagNode TagNodeFromURLHelper(InputStream is, String fullTag, Handler handler, Context context,
        HtmlCleaner cleaner) throws IOException {
    SharedPreferences settings = context.getSharedPreferences(Settings.SETTINGS_FILE_NAME, 0);
    boolean disableSmartParsing = settings.getBoolean(Settings.DISABLE_SMART_PARSING, false);
    if (fullTag != null && !disableSmartParsing) {
        FileOutputStream fos = context.openFileOutput(TEMP_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        TagParser.extractTagToFile(fullTag, is, bw);
        bw.flush();
        bw.close();//from w ww .  j a  va  2s  . c o  m
        if (handler != null)
            handler.sendEmptyMessage(PROGRESS_PARSING);

        return cleaner.clean(context.openFileInput(TEMP_FILE_NAME));
    } else {
        if (handler != null)
            handler.sendEmptyMessage(PROGRESS_PARSING);
        return cleaner.clean(is);
    }
}

From source file:canreg.client.analysis.Tools.java

public static void exportChartAsCSV(JFreeChart jFreeChart, File file) throws IOException {
    BufferedWriter bos = new BufferedWriter(new FileWriter(file));
    bos.append(getChartData(jFreeChart, ",", true));
    bos.flush();
    bos.close();/*from   w  w w.  ja v a2s .  c  om*/
}

From source file:com.google.dart.tools.core.utilities.io.FileUtilities.java

/**
 * Overwrite the contents of the given file to the given contents.
 * /*from www .  j  a va 2s  .c o  m*/
 * @param file the file whose contents are to be written
 * @param contents the new contents for the file
 * @throws IOException if the file contents could not be written
 */
public static void setContents(File file, String contents) throws IOException {
    FileWriter fileWriter = null;
    BufferedWriter writer;

    try {
        fileWriter = new FileWriter(file);
        writer = new BufferedWriter(fileWriter);
        writer.write(contents);
        writer.flush();
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
    }
}

From source file:com.wakatime.intellij.plugin.WakaTime.java

private static void sendHeartbeat(final Heartbeat heartbeat, final ArrayList<Heartbeat> extraHeartbeats) {
    final String[] cmds = buildCliCommand(heartbeat, extraHeartbeats);
    log.debug("Executing CLI: " + Arrays.toString(obfuscateKey(cmds)));
    try {//from w  w  w.  j av a2s . c o m
        Process proc = Runtime.getRuntime().exec(cmds);
        if (extraHeartbeats.size() > 0) {
            String json = toJSON(extraHeartbeats);
            log.debug(json);
            try {
                BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
                stdin.write(json);
                stdin.write("\n");
                try {
                    stdin.flush();
                    stdin.close();
                } catch (IOException e) {
                    /* ignored because wakatime-cli closes pipe after receiving \n */ }
            } catch (IOException e) {
                log.warn(e);
            }
        }
        if (WakaTime.DEBUG) {
            BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            proc.waitFor();
            String s;
            while ((s = stdout.readLine()) != null) {
                log.debug(s);
            }
            while ((s = stderr.readLine()) != null) {
                log.debug(s);
            }
            log.debug("Command finished with return value: " + proc.exitValue());
        }
    } catch (Exception e) {
        log.warn(e);
    }
}