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:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given archive into the given destination directory
 * //from   www.  j a  v a  2s .c  om
 * @param archive
 *            - the file to extract
 * @param dest
 *            - the destination directory
 * @throws Exception
 */
public static void extractArchive(File archive, File destDir) throws IOException {

    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {

            File file = new File(destDir, entryFileName);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
    zipFile.close();
}

From source file:org.dita2indesign.cmdline.Docx2Xml.java

/**
* @param zipInFile//from w w w  .j  a v a  2  s . c o  m
*            Zip file to be unzipped
* @param outputDir
*            Directory to which the zipped files will be unpacked.
* @throws Exception 
* @throws ZipException 
*/
public static void unzip(File zipInFile, File outputDir) throws Exception {

    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile = new ZipFile(zipInFile);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipInFile));
    ZipEntry entry = (ZipEntry) zipInputStream.getNextEntry();

    File curOutDir = outputDir;
    while (entry != null) {

        if (entry.isDirectory()) {
            // This is not robust, just for demonstration purposes.
            curOutDir = new File(curOutDir, entry.getName());
            curOutDir.mkdirs();
            continue;
        }

        File outFile = new File(curOutDir, entry.getName());
        File tempDir = outFile.getParentFile();
        if (!tempDir.exists())
            tempDir.mkdirs();
        outFile.createNewFile();
        BufferedOutputStream outstream = new BufferedOutputStream(new FileOutputStream(outFile));

        int n;
        byte[] buf = new byte[1024];
        while ((n = zipInputStream.read(buf, 0, 1024)) > -1)
            outstream.write(buf, 0, n);
        outstream.flush();
        outstream.close();
        zipInputStream.closeEntry();
        entry = zipInputStream.getNextEntry();
    }
    zipInputStream.close();

    zipFile.close();
}

From source file:Main.java

public static void copyDatabase2FileDir(Context context, String dbName) {
    InputStream stream = null;//from  w w  w . j av a  2 s . c  om
    BufferedOutputStream outputStream = null;
    try {

        stream = context.getAssets().open(dbName);
        File file = new File(context.getFilesDir(), dbName);

        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = stream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
            outputStream.flush();
        }

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

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

From source file:com.cooliris.media.UriTexture.java

public static void writeToCache(long crc64, Bitmap bitmap, int maxResolution) {
    String file = createFilePathFromCrc64(crc64, maxResolution);
    if (bitmap != null && file != null && crc64 != 0) {
        try {//from   w  ww. j av a2  s. c  o  m
            File fileC = new File(file);
            fileC.createNewFile();
            final FileOutputStream fos = new FileOutputStream(fileC);
            final BufferedOutputStream bos = new BufferedOutputStream(fos, 16384);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            bos.close();
            fos.close();
        } catch (Exception e) {

        }
    }
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java

/**
 * unzip CSAR packge// w w w .ja  v  a2  s .  c  om
 * @param fileName filePath
 * @return
 */
public static int unzipCSAR(String fileName, String filePath) {
    final int BUFFER = 2048;
    int status = 0;

    try {
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration emu = zipFile.entries();
        int i = 0;
        while (emu.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) emu.nextElement();
            //read directory as file first,so only need to create directory
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            //Because that is random to read zipfile,maybe the file is read first
            //before the directory is read,so we need to create directory first.
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bos.flush();
            bos.close();
            bis.close();

            if (entry.getName().endsWith(".zip")) {
                File subFile = new File(filePath + entry.getName());
                if (subFile.exists()) {
                    int subStatus = unzipCSAR(filePath + entry.getName(), subFile.getParent() + "/");
                    if (subStatus != 0) {
                        LOG.error("sub file unzip fail!" + subFile.getName());
                        status = Constant.UNZIP_FAIL;
                        return status;
                    }
                }
            }
        }
        status = Constant.UNZIP_SUCCESS;
        zipFile.close();
    } catch (Exception e) {
        status = Constant.UNZIP_FAIL;
        e.printStackTrace();
    }
    return status;
}

From source file:nz.co.kakariki.networkutils.reader.ExtractArchive.java

/**
 * Top unzip method./*from   ww w . java2 s  .  c o m*/
 */
protected static void unzip(File zipfile) throws FileNotFoundException {
    File path = zipfile.getParentFile();
    try {
        ZipArchiveInputStream zais = new ZipArchiveInputStream(new FileInputStream(zipfile));
        ZipArchiveEntry z1 = null;
        while ((z1 = zais.getNextZipEntry()) != null) {
            String fn = z1.getName();
            if (fn.contains("/")) {
                fn = fn.substring(z1.getName().lastIndexOf("/"));
            }
            File f = new File(path + File.separator + fn);
            FileOutputStream fos = new FileOutputStream(f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

            int n = 0;
            byte[] content = new byte[BUFFER];
            while (-1 != (n = zais.read(content))) {
                fos.write(content, 0, n);
            }

            bos.flush();
            bos.close();
            fos.close();
        }

        zais.close();
        zipfile.delete();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

From source file:com.dc.util.file.FileSupport.java

public static void writeBinaryFile(File file, byte[] data) throws IOException {
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    try {/*from  www .  ja va 2s. c o m*/
        fileOutputStream = new FileOutputStream(file);
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        bufferedOutputStream.write(data, 0, data.length);
        bufferedOutputStream.flush();
    } finally {
        if (bufferedOutputStream != null) {
            fileOutputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
    }
}

From source file:org.dspace.core.Utils.java

/**
 * Copy stream-data from source to destination, with buffering. This is
 * equivalent to passing {@link #copy}a//from  w w  w  . j  a v  a  2  s .com
 * <code>java.io.BufferedInputStream</code> and
 * <code>java.io.BufferedOutputStream</code> to {@link #copy}, and
 * flushing the output stream afterwards. The streams are not closed after
 * the copy.
 * 
 * @param source
 *            The InputStream to obtain data from.
 * @param destination
 *            The OutputStream to copy data to.
 * @throws IOException if IO error
 */
public static void bufferedCopy(final InputStream source, final OutputStream destination) throws IOException {
    final BufferedInputStream input = new BufferedInputStream(source);
    final BufferedOutputStream output = new BufferedOutputStream(destination);
    copy(input, output);
    output.flush();
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String urlString) {
    Bitmap bitmap = null;//  w  w w.  jav  a2 s  .  com
    InputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(urlString).openStream(), 1024 * 4);
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, 1024 * 4);
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
        byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        data = null;
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:eu.scape_project.spacip.ContainerProcessing.java

/**
 * Write ARC record content to output stream
 *
 * @param nativeArchiveRecord/*  w  ww.  ja va 2  s .c  o  m*/
 * @param outputStream Output stream
 * @throws IOException
 */
public static void recordToOutputStream(ArchiveRecord nativeArchiveRecord, OutputStream outputStream)
        throws IOException {
    ARCRecord arcRecord = (ARCRecord) nativeArchiveRecord;
    ARCRecordMetaData metaData = arcRecord.getMetaData();
    long contentBegin = metaData.getContentBegin();
    BufferedInputStream bis = new BufferedInputStream(arcRecord);
    BufferedOutputStream bos = new BufferedOutputStream(outputStream);
    byte[] tempBuffer = new byte[BUFFER_SIZE];
    int bytesRead;
    // skip record header
    bis.skip(contentBegin);
    while ((bytesRead = bis.read(tempBuffer)) != -1) {
        bos.write(tempBuffer, 0, bytesRead);
    }
    bos.flush();
    bis.close();
    bos.close();
}