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.zotoh.maedr.device.netty.NettpHplr.java

/**
 * @param cbuf/* www  .j a  v  a  2s.c  om*/
 * @param os
 * @throws IOException
 */
public static void getContent(ChannelBuffer cbuf, OutputStream os) throws IOException {

    int len, clen = cbuf.readableBytes();
    //int pos= cbuf.readerIndex();
    byte[] bits = new byte[4096];

    while (clen > 0) {
        len = Math.min(4096, clen);
        cbuf.readBytes(bits, 0, len);
        os.write(bits, 0, len);
        clen = clen - len;
    }
    os.flush();
}

From source file:com.futureplatforms.kirin.internal.attic.IOUtils.java

public static long copy(InputStream in, OutputStream out) throws IOException {

    byte[] b = new byte[1024];
    int count;/*from w w  w.j  a va 2 s.  c om*/
    long total = 0l;
    while ((count = in.read(b)) >= 0) {
        out.write(b, 0, count);
        total += count;
    }
    out.flush();
    return total;
}

From source file:ca.frozen.curlingtv.classes.Utils.java

public static String saveImage(ContentResolver contentResolver, Bitmap source, String title,
        String description) {//from ww w .ja v a2 s . co m
    File snapshot = null;
    Uri url = null;
    try {
        // get/create the snapshots folder
        File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File rpi = new File(pictures, App.getStr(R.string.app_name));
        if (!rpi.exists()) {
            rpi.mkdir();
        }

        // save the file within the snapshots folder
        snapshot = new File(rpi, title);
        OutputStream stream = new FileOutputStream(snapshot);
        source.compress(Bitmap.CompressFormat.JPEG, 90, stream);
        stream.flush();
        stream.close();

        // create the content values
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, title);
        values.put(MediaStore.Images.Media.DISPLAY_NAME, title);
        if (description != null) {
            values.put(MediaStore.Images.Media.DESCRIPTION, description);
        }
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(MediaStore.Images.ImageColumns.BUCKET_ID,
                snapshot.toString().toLowerCase(Locale.US).hashCode());
        values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
                snapshot.getName().toLowerCase(Locale.US));
        values.put("_data", snapshot.getAbsolutePath());

        // insert the image into the database
        url = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    } catch (Exception ex) {
        return null;
    }

    // return the URL
    return (url != null) ? url.toString() : null;
}

From source file:Main.java

private static void copyFile(AssetManager assetManager, String filename, String destination) {
    InputStream in = null;/*from   w w  w.  jav a 2s .co m*/
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        String newFileName = destination + File.separator + filename;
        out = new FileOutputStream(newFileName);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }
}

From source file:Main.java

private static boolean handleFile(int mode, byte[] key, byte[] iv, String sourceFilePath, String destFilePath) {

    File sourceFile = new File(sourceFilePath);
    File destFile = new File(destFilePath);

    try {/*from  w  w w  . j av  a 2s . c o  m*/
        if (sourceFile.exists() && sourceFile.isFile()) {
            if (!destFile.getParentFile().exists())
                destFile.getParentFile().mkdirs();
            destFile.createNewFile();

            InputStream in = new FileInputStream(sourceFile);
            OutputStream out = new FileOutputStream(destFile);
            Cipher cipher = initCipher(mode, key, iv, AES_CFB_NOPADDING);
            CipherInputStream cin = new CipherInputStream(in, cipher);

            byte[] b = new byte[1024];
            int read = 0;
            while ((read = cin.read(b)) != -1) {
                out.write(b, 0, read);
                out.flush();
            }

            cin.close();
            in.close();
            out.close();

            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static File bitmap2File(Bitmap bitmap, String filename) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;

    File file = new File(extStorageDirectory, filename + ".png");
    if (file.exists()) {
        file.delete();/* ww  w.  j av a  2s. co  m*/
        file = new File(extStorageDirectory, filename + ".png");
        Log.e("file exist", "" + file + ",Bitmap= " + filename);
    }
    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file;

}

From source file:com.baasbox.android.HttpUrlConnectionClient.java

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

From source file:org.jboss.seam.forge.shell.util.PluginUtil.java

private static File saveFile(String fileName, InputStream stream) throws IOException {
    File file = new File(fileName);
    new File(fileName.substring(0, fileName.lastIndexOf('/'))).mkdirs();

    file.createNewFile();/*from ww w  .  j a  v a2  s  .  c  o  m*/

    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));

    byte[] buf = new byte[127];
    int read;
    while ((read = stream.read(buf)) != -1) {
        outputStream.write(buf, 0, read);
    }

    outputStream.flush();
    outputStream.close();

    return file;
}

From source file:FileUtil.java

/**
 *  Just copies all bytes from <I>in</I> to <I>out</I>.  The copying is
 *  performed using a buffer of bytes.//from w w w  .  ja  va 2  s .  c  o m
 *
 *  @since 1.9.31
 *  @param in The inputstream to copy from
 *  @param out The outputstream to copy to
 *  @throws IOException In case reading or writing fails.
 */
public static void copyContents(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[BUFFER_SIZE];
    int bytesRead = 0;

    while ((bytesRead = in.read(buf)) > 0) {
        out.write(buf, 0, bytesRead);
    }

    out.flush();
}

From source file:com.google.enterprise.adaptor.experimental.Sim.java

/** Send some response body. */
private static void respond(HttpExchange ex, int code, String contentType, byte response[]) throws IOException {
    ex.getResponseHeaders().set("Content-Type", contentType);
    ex.sendResponseHeaders(code, 0);/*  w w  w.j  av  a 2 s  .c om*/
    OutputStream responseBody = ex.getResponseBody();
    log.finest("before writing response");
    responseBody.write(response);
    responseBody.flush();
    responseBody.close();
    ex.close();
    log.finest("after closing exchange");
}