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:Main.java

/**
 * <pre>/*from  www .  ja va2 s  .c  o  m*/
 * Convenient method to copy file.
 * </pre>
 * @param in {@link InputStream} of source file
 * @param out {@link OutputStream} of destination file
 */
public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }

    in.close();
    out.flush();
    out.close();
}

From source file:Main.java

public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;//from  w ww  .  j av a  2 s  .c o  m
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    out.flush();
}

From source file:Main.java

/**
 * The following method creates a File from a given Bitmap.
 * /*from  w  w  w.j a  v a2 s. c  om*/
 * @param context
 *            - The application context.
 * @param bitmap
 *            - The bitmap to be converted into a File.
 * @return
 */
public static File getFilefromBitmap(Context context, Bitmap bitmap) {
    File file = null;
    try {
        String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        file = new File(path, "1" + ".jpg");
        fOut = new FileOutputStream(file);

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(),
                file.getName(), file.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return file;

}

From source file:brickhouse.udf.bloom.BloomFactory.java

public static void WriteBloomToStream(OutputStream stream, Filter bloom) throws IOException {
    stream.write(WriteBloomToString(bloom).getBytes());
    stream.flush();
}

From source file:org.jfreechart.SVGExporter.java

public static void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    // Get a DOMImplementation and create an XML document
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // draw the chart in the SVG generator
    chart.draw(svgGenerator, bounds);//from  ww w  .  j a v a2  s.  c o m

    // Write svg file
    OutputStream outputStream = new FileOutputStream(svgFile);
    Writer out = new OutputStreamWriter(outputStream, "UTF-8");
    svgGenerator.stream(out, true /* use css */);
    outputStream.flush();
    outputStream.close();
}

From source file:Main.java

public static File saveTmpFile(InputStream is) throws Exception {
    File file = getTmpFile();//from   w w  w  .  ja va2  s  .com
    OutputStream os = new FileOutputStream(file);

    byte[] buf = new byte[1024];
    int len = 0;

    while ((len = is.read(buf)) > 0) {
        os.write(buf, 0, len);
    }

    os.flush();
    os.close();

    return file;
}

From source file:Main.java

/**
 * Performs execution of the shell command on remote host.
 * @param host ip address or name of the host.
 * @param port telnet port/*from w  w w.  j  a  v  a  2s .  co  m*/
 * @param command shell command to be executed.
 * @return true if success, false on error.
 */
public static final boolean executeRemotely(String host, int port, String command) {
    Socket socket = null;
    OutputStream os = null;

    try {
        socket = new Socket(host, port);

        os = socket.getOutputStream();

        os.write(command.getBytes());
        os.flush();

        return true;
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {

        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (socket != null && !socket.isClosed()) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.usefullc.platform.common.utils.IOUtils.java

/**
 * //from  ww  w. j ava  2s . c  om
 * 
 * @param content
 * @param fileName
 * @param response
 */
public static void download(byte[] content, String fileName, HttpServletResponse response) {
    try {
        // response
        response.reset();

        // ??linux ?  linux utf-8,windows  GBK)
        String defaultEncoding = System.getProperty("file.encoding");
        if (defaultEncoding != null && defaultEncoding.equals("UTF-8")) {

            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso-8859-1"));
        } else {
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1"));
        }

        // responseHeader
        response.addHeader("Content-Length", "" + content.length);
        response.setContentType("application/octet-stream");
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        toClient.write(content);
        toClient.flush();
        toClient.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
    InputStream in = new ByteArrayInputStream(bytes);
    File destFile = new File(filePath);
    if (!destFile.getParentFile().exists()) {
        destFile.getParentFile().mkdirs();
    }/*  www .  j a va2  s . co  m*/
    destFile.createNewFile();
    OutputStream out = new FileOutputStream(destFile);
    byte[] cache = new byte[CACHE_SIZE];
    int nRead = 0;
    while ((nRead = in.read(cache)) != -1) {
        out.write(cache, 0, nRead);
        out.flush();
    }
    out.close();
    in.close();
}

From source file:Main.java

public static void writeStream(InputStream inputStream, OutputStream outputStream) {
    byte[] buffer = new byte[1024];
    int len = 0;/*from  w  w  w. j  a v  a2  s .c o m*/
    try {
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

}