Example usage for java.io OutputStreamWriter flush

List of usage examples for java.io OutputStreamWriter flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:CopyUtils.java

/**
 * Serialize chars from a <code>Reader</code> to bytes on an
 * <code>OutputStream</code>, and flush the <code>OutputStream</code>.
 * @param input the <code>Reader</code> to read from
 * @param output the <code>OutputStream</code> to write to
 * @throws IOException In case of an I/O problem
 *///w  w w .jav  a 2s .  c  o  m
public static void copy(Reader input, OutputStream output) throws IOException {
    OutputStreamWriter out = new OutputStreamWriter(output);
    copy(input, out);
    // XXX Unless anyone is planning on rewriting OutputStreamWriter, we have to flush here.
    out.flush();
}

From source file:com.shvet.poi.util.HexDump.java

/**
 * dump an array of bytes to an OutputStream
 *
 * @param data   the byte array to be dumped
 * @param offset its offset, whatever that might mean
 * @param stream the OutputStream to which the data is to be
 *               written//from w  ww .  j a  v a2s . c  o m
 * @param index  initial index into the byte array
 * @param length number of characters to output
 * @throws IOException                    is thrown if anything goes wrong writing
 *                                        the data to stream
 * @throws ArrayIndexOutOfBoundsException if the index is
 *                                        outside the data array's bounds
 * @throws IllegalArgumentException       if the output stream is
 *                                        null
 */
public static void dump(final byte[] data, final long offset, final OutputStream stream, final int index,
        final int length) throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException {
    if (stream == null) {
        throw new IllegalArgumentException("cannot write to nullstream");
    }

    OutputStreamWriter osw = new OutputStreamWriter(stream, UTF8);
    osw.write(dump(data, offset, index, length));
    osw.flush();
}

From source file:de.akquinet.android.androlog.reporter.PostReporter.java

/**
 * Executes the given request as a HTTP POST action.
 *
 * @param url//w ww.  j av a  2 s  .co m
 *            the url
 * @param params
 *            the parameter
 * @return the response as a String.
 * @throws IOException
 *             if the server cannot be reached
 */
public static void post(URL url, String params) throws IOException {
    URLConnection conn = url.openConnection();
    if (conn instanceof HttpURLConnection) {
        ((HttpURLConnection) conn).setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
    }
    OutputStreamWriter writer = null;
    try {
        conn.setDoOutput(true);
        writer = new OutputStreamWriter(conn.getOutputStream(), Charset.forName("UTF-8"));
        // write parameters
        writer.write(params);
        writer.flush();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:Main.java

public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor)
        throws Exception {

    Process proc = null;//from   w  w  w.  ja  va  2 s .  c  om
    int exitCode = -1;

    if (runAsRoot) {
        proc = Runtime.getRuntime().exec("su");
    } else {
        proc = Runtime.getRuntime().exec("sh");
    }

    OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());

    for (int i = 0; i < cmds.length; i++) {
        // TorService.logMessage("executing shell cmd: " + cmds[i] +
        // "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);

        out.write(cmds[i]);
        out.write("\n");
    }

    out.flush();
    out.write("exit\n");
    out.flush();

    if (waitFor) {

        final char buf[] = new char[10];

        // Consume the "stdout"
        InputStreamReader reader = new InputStreamReader(proc.getInputStream());
        int read = 0;
        while ((read = reader.read(buf)) != -1) {
            if (log != null) {
                log.append(buf, 0, read);
            }
        }

        // Consume the "stderr"
        reader = new InputStreamReader(proc.getErrorStream());
        read = 0;
        while ((read = reader.read(buf)) != -1) {
            if (log != null) {
                log.append(buf, 0, read);
            }
        }

        exitCode = proc.waitFor();

    }

    return exitCode;

}

From source file:Main.java

public static File writeToSDFromInput(String path, String fileName, String data) {

    File file = null;//w  ww .  j a  v  a  2 s. c om
    OutputStreamWriter outputWriter = null;
    OutputStream outputStream = null;
    try {
        creatSDDir(path);
        file = createFileInSDCard(fileName, path);
        outputStream = new FileOutputStream(file, false);
        outputWriter = new OutputStreamWriter(outputStream);
        outputWriter.write(data);
        outputWriter.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return file;
}

From source file:edu.jhu.cvrg.timeseriesstore.opentsdb.TimeSeriesUtility.java

protected static int insertDataPoints(String urlString, List<IncomingDataPoint> points) throws IOException {
    int code = 0;
    Gson gson = new Gson();

    HttpURLConnection httpConnection = TimeSeriesUtility.openHTTPConnectionPOST(urlString);
    OutputStreamWriter wr = new OutputStreamWriter(httpConnection.getOutputStream());

    String json = gson.toJson(points);

    wr.write(json);/* w w  w .j  a  v a 2s.co  m*/
    wr.flush();
    wr.close();

    code = httpConnection.getResponseCode();

    httpConnection.disconnect();

    return code;
}

From source file:com.memetix.mst.MicrosoftTranslatorAPI.java

/**
 * Gets the OAuth access token.//from w  w  w  .java 2 s. c  om
 * @param clientId The Client key.
 * @param clientSecret The Client Secret
 */
public static String getToken(final String clientId, final String clientSecret) throws Exception {
    final String params = "grant_type=client_credentials&scope=http://api.microsofttranslator.com"
            + "&client_id=" + URLEncoder.encode(clientId, ENCODING) + "&client_secret="
            + URLEncoder.encode(clientSecret, ENCODING);

    final URL url = new URL(DatamarketAccessUri);
    final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    if (referrer != null)
        uc.setRequestProperty("referer", referrer);
    uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + ENCODING);
    uc.setRequestProperty("Accept-Charset", ENCODING);
    uc.setRequestMethod("POST");
    uc.setDoOutput(true);

    OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream());
    wr.write(params);
    wr.flush();

    try {
        final int responseCode = uc.getResponseCode();
        final String result = inputStreamToString(uc.getInputStream());
        if (responseCode != 200) {
            throw new Exception("Error from Microsoft Translator API: " + result);
        }
        return result;
    } finally {
        if (uc != null) {
            uc.disconnect();
        }
    }
}

From source file:net.mindengine.oculus.frontend.web.controllers.display.FileDisplayController.java

public static void showTextFile(HttpServletResponse response, String path, String fileName, String contentType)
        throws IOException {

    OutputStream os = response.getOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(os);
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    response.setContentType(contentType);
    response.setCharacterEncoding("UTF-8");
    String content = readFileAsString(path);
    w.write(content);//from   w  ww  .j  av  a 2  s .  c o  m
    w.flush();
    os.flush();
    os.close();
}

From source file:com.buglabs.dragonfly.util.WSHelper.java

/**
 * Post contents of input stream to URL.
 * //from www  .j a  v a2s  . com
 * @param url
 * @param stream
 * @return
 * @throws IOException
 */
protected static String postBase64(URL url, InputStream stream) throws IOException {
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    byte[] buff = streamToByteArray(stream);

    String em = Base64.encodeBytes(buff);
    OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream());
    osr.write(em);
    osr.flush();

    stream.close();
    conn.getOutputStream().flush();
    conn.getOutputStream().close();

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line, resp = new String("");
    while ((line = rd.readLine()) != null) {
        resp = resp + line + "\n";
    }
    rd.close();

    return resp;
}

From source file:at.jku.rdfstats.hist.builder.HistogramCodec.java

public static void writeString(ByteArrayOutputStream stream, String string) {
    try {//from  w  ww  .  j  av a2s.co m
        OutputStreamWriter out = new OutputStreamWriter(stream);
        if (string == null)
            out.write((int) EMPTY_STRING);
        else {
            out.write(string);
            out.write((int) END_OF_STRING);
        }
        out.flush();
    } catch (IOException e) {
        throw new RuntimeException("Unexpected error: cannot write String into ByteArrayOutputStream.", e);
    }
}