Example usage for java.io BufferedOutputStream flush

List of usage examples for java.io BufferedOutputStream flush

Introduction

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

Prototype

@Override
public synchronized void flush() throws IOException 

Source Link

Document

Flushes this buffered output stream.

Usage

From source file:in.xebia.poc.FileUploadUtils.java

public static boolean bufferedCopyStream(InputStream inStream, OutputStream outStream) throws Exception {
    BufferedInputStream bis = new BufferedInputStream(inStream);
    BufferedOutputStream bos = new BufferedOutputStream(outStream);
    while (true) {
        int data = bis.read();
        if (data == -1) {
            break;
        }/*from  w ww.  ja va  2s. co m*/
        bos.write(data);
    }
    bos.flush();
    bos.close();
    return true;
}

From source file:com.ettrema.httpclient.Utils.java

/**
 * Wraps the outputstream in a bufferedoutputstream and writes to it
 *
 * the outputstream is closed and flushed before returning
 *
 * @param in/*from www.  j  a  va 2s .  c  o  m*/
 * @param out
 * @param listener
 * @throws IOException
 */
public static long writeBuffered(InputStream in, OutputStream out, final ProgressListener listener)
        throws IOException {
    BufferedOutputStream bout = null;
    try {
        bout = new BufferedOutputStream(out);
        long bytes = Utils.write(in, out, listener);
        bout.flush();
        out.flush();
        return bytes;
    } finally {
        Utils.close(bout);
        Utils.close(out);
    }

}

From source file:Main.java

private static void fileUnZip(ZipInputStream zis, File file) throws FileNotFoundException, IOException {
    java.util.zip.ZipEntry zip = null;
    while ((zip = zis.getNextEntry()) != null) {
        String name = zip.getName();
        File f = new File(file.getAbsolutePath() + File.separator + name);
        if (zip.isDirectory()) {
            f.mkdirs();/*from  w ww .  ja  va  2  s .c o  m*/
        } else {
            f.getParentFile().mkdirs();
            f.createNewFile();
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(f));
                byte b[] = new byte[2048];
                int aa = 0;
                while ((aa = zis.read(b)) != -1) {
                    bos.write(b, 0, aa);
                }
                bos.flush();
            } finally {
                bos.close();
            }
            bos.close();
        }
    }

}

From source file:com.ifs.megaprofiler.helper.FileExtractor.java

/**
 * Unpack data from the stream to specified directory.
 * /*from   w  w  w  .j  a v  a2s.  co m*/
 * @param in
 *            stream with tar data
 * @param outputDir
 *            destination directory
 * @return true in case of success, otherwise - false
 */
private static void extract(ArchiveInputStream in, File outputDir) {
    try {
        ArchiveEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            // replace : for windows OS.
            final File file = new File(outputDir, entry.getName().replaceAll(":", "_"));

            if (entry.isDirectory()) {

                if (!file.exists()) {
                    file.mkdirs();
                }

            } else {
                file.getParentFile().mkdirs();
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);

                try {

                    IOUtils.copy(in, out);
                    out.flush();

                } finally {
                    try {
                        out.close();

                    } catch (IOException e) {
                        // LOG.debug(
                        // "An error occurred while closing the output stream for file '{}'. Error: {}",
                        // file,
                        // e.getMessage() );
                    }
                }
            }
        }

    } catch (IOException e) {
        // LOG.debug("An error occurred while handling archive file. Error: {}",
        // e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // LOG.debug(
                // "An error occurred while closing the archive stream . Error: {}",
                // e.getMessage() );
            }
        }
    }
}

From source file:Main.java

/**
 * Loads a bitmap from the specified url. This can take a while, so it should not
 * be called from the UI thread.//from   w w  w. j  a  v a  2 s  .  c  om
 * 
 * @param url The location of the bitmap asset
 * 
 * @return The bitmap, or null if it could not be loaded
 */
public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

From source file:eu.scape_project.arc2warc.utils.ArcUtils.java

/**
 * Read the ARC record content into a byte array. Note that the record
 * content can be only read once, it is "consumed" afterwards.
 *
 * @param arcRecord ARC record./*from w  ww .j av  a  2s .  co  m*/
 * @return Content byte array.
 * @throws IOException If content is too large to be stored in a byte array.
 */
public static byte[] arcRecordPayloadToByteArray(ARCRecord arcRecord) throws IOException {
    // Byte point where the content of the ARC record begins
    int contentBegin = (int) arcRecord.getMetaData().getContentBegin();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedInputStream buffis = new BufferedInputStream(arcRecord);
    BufferedOutputStream buffos = new BufferedOutputStream(baos);
    byte[] tempBuffer = new byte[BUFFER_SIZE];
    int bytesRead;
    // skip header content
    buffis.skip(contentBegin);
    while ((bytesRead = buffis.read(tempBuffer)) != -1) {
        buffos.write(tempBuffer, 0, bytesRead);
    }
    buffis.close();
    buffos.flush();
    buffos.close();
    return baos.toByteArray();
}

From source file:Util.java

/**
 * Writes the specified byte[] to the specified File path.
 * //  www  . j a  v a 2  s  . c  o m
 * @param theFile File Object representing the path to write to.
 * @param bytes The byte[] of data to write to the File.
 * @throws IOException Thrown if there is problem creating or writing the 
 * File.
 */
public static void writeBytesToFile(File theFile, byte[] bytes) throws IOException {
    BufferedOutputStream bos = null;

    try {
        FileOutputStream fos = new FileOutputStream(theFile);
        bos = new BufferedOutputStream(fos);
        bos.write(bytes);
    } finally {
        if (bos != null) {
            try {
                //flush and close the BufferedOutputStream
                bos.flush();
                bos.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:de.lazyzero.kkMulticopterFlashTool.utils.Zip.java

public static void unzip2folder(File zipFile, File toFolder) throws FileExistsException {
    if (!toFolder.exists()) {
        toFolder.mkdirs();/*from w  w  w. j  a v  a  2  s  .  c  om*/
    } else if (toFolder.isFile()) {
        throw new FileExistsException(toFolder.getName());
    }

    try {
        ZipEntry entry;
        @SuppressWarnings("resource")
        ZipFile zipfile = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = e.nextElement();

            //            String newDir;
            //            if (entry.getName().indexOf("/") == -1) {
            //               newDir = zipFile.getName().substring(0, zipFile.getName().indexOf("."));
            //            } else {
            //               newDir = entry.getName().substring(0, entry.getName().indexOf("/"));
            //            }
            //            String folder = toFolder + (File.separatorChar+"") + newDir;
            //            System.out.println(folder);
            //            if ((new File(folder).mkdir())) {
            //               System.out.println("Done.");
            //            }
            System.out.println("Extracting: " + entry);
            if (entry.isDirectory()) {
                new File(toFolder + (File.separatorChar + "") + entry.getName()).mkdir();
            } else {
                BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry));
                int count;
                byte data[] = new byte[2048];
                String fileName = toFolder + (File.separatorChar + "") + entry.getName();
                FileOutputStream fos = new FileOutputStream(fileName);
                BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
                while ((count = is.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                is.close();
                System.out.println("extracted to: " + fileName);
            }

        }
    } catch (ZipException e1) {
        zipFile.delete();
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } finally {

    }

}

From source file:org.camunda.bpm.cycle.util.IoUtil.java

public static void writeStringToFile(String content, String filePath) {
    BufferedOutputStream outputStream = null;
    try {//from w  w w. j a  va2s .c om
        outputStream = new BufferedOutputStream(new FileOutputStream(getFile(filePath)));
        outputStream.write(content.getBytes());
        outputStream.flush();
    } catch (Exception e) {
        throw new CycleException("Couldn't write file " + filePath, e);
    } finally {
        IoUtil.closeSilently(outputStream);
    }
}

From source file:Main.java

/**
 * Loads a bitmap from the specified url. This can take a while, so it should not
 * be called from the UI thread./*  w  w w. j  a  v  a 2  s  .c o m*/
 *
 * @param url The location of the bitmap asset
 *
 * @return The bitmap, or null if it could not be loaded
 */
public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        //            BitmapFactory.Options options = new BitmapFactory.Options();
        //            options.inSampleSize = 2;
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}