Example usage for java.io BufferedOutputStream close

List of usage examples for java.io BufferedOutputStream close

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

private static final void copyInputStream(InputStream in, String fileName) throws FileNotFoundException {
    File file = new File(fileName);
    File parentFile = file.getParentFile();
    parentFile.mkdirs();// w w  w .  j av a 2  s.  c  o m
    System.out.println("Creating parent directory... " + parentFile.getAbsolutePath());
    byte[] buffer = new byte[1024];
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream out = new BufferedOutputStream(fos);
    int len;
    try {
        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }
    } catch (IOException ex) {

    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

From source file:prince.app.sphotos.Gallery.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  . ja  va 2 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();
            Log.i(TAG, "written to cache at: " + fileC.getAbsolutePath());
        } catch (Exception e) {

        }
    }
}

From source file:au.org.ala.biocache.util.AlaFileUtils.java

/**
 * Creates a zip file at the specified path with the contents of the specified directory.
 * NB:/* ww w  .j a  va 2  s.  com*/
 *
 * @param directoryPath The path of the directory where the archive will be created. eg. c:/temp
 * @param zipPath The full path of the archive to create. eg. c:/temp/archive.zip
 * @throws IOException If anything goes wrong
 */
public static void createZip(String directoryPath, String zipPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    ZipArchiveOutputStream tOut = null;

    try {
        fOut = new FileOutputStream(new File(zipPath));
        bOut = new BufferedOutputStream(fOut);
        tOut = new ZipArchiveOutputStream(bOut);
        addFileToZip(tOut, directoryPath, "");
    } finally {
        tOut.finish();
        tOut.close();
        bOut.close();
        fOut.close();
    }
}

From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given archive into the given destination directory
 * //  www  .  j a  v  a 2  s .com
 * @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:Main.java

/**
 *  Move the file in oldLocation to newLocation.
 *///from   w w w  .j a  va  2 s.c o  m
public static void moveFile(String tempLocation, String newLocation) throws IOException {
    File oldLocation = new File(tempLocation);
    if (oldLocation != null && oldLocation.exists()) {
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation));
        BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false));
        try {
            byte[] buff = new byte[8192];
            int numChars;
            while ((numChars = reader.read(buff, 0, buff.length)) != -1) {
                writer.write(buff, 0, numChars);
            }
        } catch (IOException ex) {
        } finally {
            try {
                if (reader != null) {
                    writer.close();
                    reader.close();
                }
            } catch (IOException ex) {
            }
        }
    } else {
    }
}

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   www. j  av  a  2 s. c  om*/
            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:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static File unzip(byte[] zipData, File directory) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
    ZipInputStream zis = new ZipInputStream(bais);
    ZipEntry entry = zis.getNextEntry();
    File root = null;/* w  ww . ja  va2  s.  co m*/

    while (entry != null) {
        if (entry.isDirectory()) {
            File f = new File(directory, entry.getName());
            f.mkdir();

            if (root == null) {
                root = f;
            }
        } else {
            BufferedOutputStream out;
            out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())),
                    BUFFER_SIZE);

            // ZipInputStream can only give us one byte at a time...
            for (int data = zis.read(); data != -1; data = zis.read()) {
                out.write(data);
            }
            out.close();
        }

        zis.closeEntry();
        entry = zis.getNextEntry();
    }
    zis.close();

    return root;
}

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

/**
 * unzip CSAR packge//w w  w.  ja v  a 2 s. co  m
 * @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:Main.java

public static boolean byte2file(byte[] bytes, File file) {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;/*  w ww .j  ava 2 s  . c o m*/
    try {
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(bytes);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (bos != null)
                bos.close();
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

/**
 * Write ARC record content to output stream
 *
 * @param nativeArchiveRecord//from  w ww  . jav  a 2 s  .  c om
 * @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();
}