Example usage for java.io OutputStream flush

List of usage examples for java.io OutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:com.wx.kernel.util.HttpKit.java

/**
 * Send POST request/*from   w ww.  ja va  2  s  .c om*/
 */
public static String post(String url, Map<String, String> queryParas, String data,
        Map<String, String> headers) {
    HttpURLConnection conn = null;
    try {
        conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), POST, headers);
        conn.connect();

        OutputStream out = conn.getOutputStream();
        out.write(data.getBytes(CHARSET));
        out.flush();
        out.close();

        return readResponseString(conn);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:Main.java

private static boolean copyAsset(AssetManager assetManager, String fromAssetPath, String toPath) {
    InputStream in = null;/*from  ww  w .  j  ava 2 s  . c om*/
    OutputStream out = null;
    try {
        in = assetManager.open(fromAssetPath);
        new File(toPath).createNewFile();
        out = new FileOutputStream(toPath);
        copyFile2(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:FileHelper.java

/**
 * Move a file from one location to another.  An attempt is made to rename
 * the file and if that fails, the file is copied and the old file deleted.
 *
 * @param from file which should be moved.
 * @param to desired destination of the file.
 * @param overwrite If false, an exception will be thrown rather than overwrite a file.
 * @throws IOException if an error occurs.
 *
 * @since ostermillerutils 1.00.00/*from   ww w .  j a v a  2s .c o m*/
 */
public static void move(File from, File to, boolean overwrite) throws IOException {
    if (to.exists()) {
        if (overwrite) {
            if (!to.delete()) {
                throw new IOException(MessageFormat.format(labels.getString("deleteerror"),
                        (Object[]) new String[] { to.toString() }));
            }
        } else {
            throw new IOException(MessageFormat.format(labels.getString("alreadyexistserror"),
                    (Object[]) new String[] { to.toString() }));
        }
    }

    if (from.renameTo(to))
        return;

    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(from);
        out = new FileOutputStream(to);
        copy(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
        if (!from.delete()) {
            throw new IOException(MessageFormat.format(labels.getString("deleteoriginalerror"),
                    (Object[]) new String[] { from.toString(), to.toString() }));
        }
    } finally {
        if (in != null) {
            in.close();
            in = null;
        }
        if (out != null) {
            out.flush();
            out.close();
            out = null;
        }
    }
}

From source file:Main.java

public static boolean saveBitmap(Bitmap bitmap, String file, CompressFormat format) {
    OutputStream os = null;
    try {//from w  ww .j  a  v a 2s  . c o m
        File f = new File(file);
        if (!f.getParentFile().exists()) {
            f.getParentFile().mkdirs();
        }
        os = new BufferedOutputStream(new FileOutputStream(file));
        if (bitmap.compress(format, 100, os)) {
            os.flush();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
    return false;
}

From source file:be.i8c.sag.util.FileUtils.java

/**
 * Copy a directory and it's content//from  ww w . j a  v a2s .  c  o m
 *
 * @param from The directory to copy
 * @param to Where to copy it to
 * @param deleteOnExit If true, delete the files once the application has closed.
 * @throws IOException When failed to write to a file
 * @throws FileNotFoundException If a file could not be found
 */
public static void copyDirectory(File from, File to, boolean deleteOnExit) throws IOException {
    File[] children = from.listFiles();
    if (children != null) {
        for (File child : children) {
            // Extract the file
            InputStream in = new FileInputStream(child);
            File outputFile = createFile(new File(to, child.getName()));
            if (deleteOnExit)
                outputFile.deleteOnExit();

            OutputStream out = new FileOutputStream(outputFile);
            try {
                IOUtils.copy(in, out);
            } finally {
                out.flush();
                in.close();
                out.close();
            }
        }
    }
}

From source file:Main.java

public static void assetsDataToSD(String fileOutPutName, String fileInPutName, Context context)
        throws IOException {
    InputStream myInput;/*w w w .j  a v a 2  s. co m*/
    File file = new File(fileOutPutName);
    /*if (!file.exists()) {
    file.createNewFile();
       }else {
       return;
    }*/
    OutputStream myOutput = new FileOutputStream(fileOutPutName);
    myInput = context.getAssets().open(fileInPutName);
    byte[] buffer = new byte[1024];
    int length = myInput.read(buffer);
    while (length > 0) {
        myOutput.write(buffer, 0, length);
        length = myInput.read(buffer);
    }

    myOutput.flush();
    myInput.close();
    myOutput.close();
}

From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.Utilities.java

/**
 * Exports a JFreeChart to a SVG file./*from w ww .j  a  v a  2s  . com*/
 *
 * @param chart
 *            JFreeChart to export
 * @param bounds
 *            the dimensions of the viewport
 * @param svgFile
 *            the output file.
 * @throws IOException
 *             if writing the svgFile fails.
 */
public static void saveChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    try {
        Class<?> GDI = Class.forName("org.apache.batik.dom.GenericDOMImplementation");

        // Get a DOMImplementation and create an XML document
        Method getDOMImplementation = GDI.getMethod("getDOMImplementation", new Class<?>[0]);
        DOMImplementation domImpl = (DOMImplementation) getDOMImplementation.invoke(null, new Object[0]);

        org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null);

        // Create an instance of the SVG Generator
        Class<?> SG2D = Class.forName("org.apache.batik.svggen.SVGGraphics2D");
        Method streamMethod = SG2D.getMethod("stream", new Class<?>[] { Writer.class, boolean.class });
        Constructor<?> SG2DConstr = SG2D.getConstructor(new Class<?>[] { org.w3c.dom.Document.class });
        Object svgGenerator = SG2DConstr.newInstance(document);

        // draw the chart in the SVG generator
        chart.draw((Graphics2D) svgGenerator, bounds);

        // Write svg file
        OutputStream outputStream = new FileOutputStream(svgFile);
        Writer out = new OutputStreamWriter(outputStream, "UTF-8");
        streamMethod.invoke(svgGenerator, new Object[] { out, true /* use css */ });
        outputStream.flush();
        outputStream.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Reads data from the input and writes it to the output, until the end of the
 * input stream./*  w w  w. j a v a 2 s .  c  o m*/
 * 
 * @param in
 * @param out
 * @param bufSizeHint
 * @throws IOException
 */
public static void copyPipe(InputStream in, OutputStream out, int bufSizeHint) throws IOException {
    int read = -1;
    byte[] buf = new byte[bufSizeHint];
    while ((read = in.read(buf, 0, bufSizeHint)) >= 0) {
        out.write(buf, 0, read);
    }
    out.flush();
}

From source file:com.bagdemir.eboda.utils.HttpUtils.java

private static void writeToStream(final OutputStream outputStream, final byte[] content) {
    try {/*from   w ww .j a va  2s.c  o m*/
        outputStream.write(content);
        outputStream.flush();
    } catch (IOException e) {
        LOGGER.error(e);
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            LOGGER.error(e);
        }
    }
}

From source file:Main.java

/**
 * write file// w  w w.j  a  v a2s .  co  m
 *
 * @param filePath
 * @param stream
 * @return return true
 * @throws IOException
 *             if an error occurs while operator FileWriter
 */
public static boolean writeFile(String filePath, InputStream stream) {
    OutputStream o = null;
    try {
        o = new FileOutputStream(filePath);
        byte data[] = new byte[1024];
        int length = -1;
        while ((length = stream.read(data)) != -1) {
            o.write(data, 0, length);
        }
        o.flush();
        return true;
    } catch (FileNotFoundException e) {
        throw new RuntimeException("FileNotFoundException occurred. ", e);
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (o != null) {
            try {
                o.close();
                stream.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
}