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:com.geewhiz.pacify.utils.ArchiveUtils.java

private static Map<String, File> extractFiles(File archive, String archiveType, String searchFor,
        Boolean isRegExp) {/*  w ww  .  j ava  2  s . c o  m*/
    Map<String, File> result = new HashMap<String, File>();

    ArchiveInputStream ais = null;
    try {
        ArchiveStreamFactory factory = new ArchiveStreamFactory();

        ais = factory.createArchiveInputStream(archiveType, new FileInputStream(archive));

        ArchiveEntry entry;
        while ((entry = ais.getNextEntry()) != null) {
            if (isRegExp) {
                if (!matches(entry.getName(), searchFor)) {
                    continue;
                }
            } else if (!searchFor.equals(entry.getName())) {
                continue;
            }

            File physicalFile = FileUtils.createEmptyFileWithSamePermissions(archive,
                    archive.getName() + "!" + Paths.get(entry.getName()).getFileName().toString() + "_");

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

            byte[] content = new byte[2048];

            int len;
            while ((len = ais.read(content)) != -1) {
                bos.write(content, 0, len);
            }

            bos.close();
            content = null;

            result.put(entry.getName(), physicalFile);
        }
    } catch (ArchiveException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(ais);
    }

    return result;
}

From source file:Main.java

public static boolean saveFileFromAssetsToSystem(Context context, String path, File destFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {//from   w  ww . j av  a 2  s.c o m
        InputStream is = context.getAssets().open(path);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:Main.java

/**
 * Base64 to File/*ww w  .  j  ava  2s .c  o m*/
 * @param base64Str
 * @param filePath
 * @param fileName
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void saveBase64StringToFile(String base64Str, String filePath, String fileName)
        throws FileNotFoundException, IOException {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    File file = null;
    try {
        File dir = new File(filePath);
        if (!dir.exists() && dir.isDirectory()) {
            dir.mkdirs();
        }
        file = new File(filePath, fileName);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        byte[] bfile = Base64.decode(base64Str, Base64.DEFAULT);
        bos.write(bfile);
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

From source file:com.blazemeter.bamboo.plugin.ServiceManager.java

public static void unzip(String srcZipFileName, String destDirectoryName, BuildLogger logger) {
    try {//w ww .java2  s  .c  o m
        BufferedInputStream bufIS = null;
        // create the destination directory structure (if needed)
        File destDirectory = new File(destDirectoryName);
        destDirectory.mkdirs();

        // open archive for reading
        File file = new File(srcZipFileName);
        ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);

        //for every zip archive entry do
        Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            logger.addBuildLogEntry("\tExtracting jtl report: " + entry);

            //create destination file
            File destFile = new File(destDirectory, entry.getName());

            //create parent directories if needed
            File parentDestFile = destFile.getParentFile();
            parentDestFile.mkdirs();

            bufIS = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;

            // buffer for writing file
            byte data[] = new byte[BUFFER_SIZE];

            // write the current file to disk
            FileOutputStream fOS = new FileOutputStream(destFile);
            BufferedOutputStream bufOS = new BufferedOutputStream(fOS, BUFFER_SIZE);

            while ((currentByte = bufIS.read(data, 0, BUFFER_SIZE)) != -1) {
                bufOS.write(data, 0, currentByte);
            }

            // close BufferedOutputStream
            bufOS.flush();
            bufOS.close();
        }
        bufIS.close();
    } catch (Exception e) {
        logger.addErrorLogEntry("Failed to unzip report: check that it is downloaded");
    }
}

From source file:Main.java

public static boolean bitmapToOutPutStream(final Bitmap bitmap, OutputStream outputStream) {

    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {//from  ww w. ja  v a  2 s.  c o  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

        in = new BufferedInputStream(inputStream, 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);

        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:JarMaker.java

/**
 * Unpack the jar file to a directory, till teaf files
 * @param file The source file name/*from  w  w w .jav a  2 s  .  c  o m*/
 * @param file1 The target directory
 * @author wangxp
 * @version 2003/11/07
 */
public static void unpackAppJar(String file, String file1) throws IOException {

    // m_log.debug("unpackAppJar begin. file="+file+"|||file1="+file1);

    BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
    byte abyte0[] = new byte[1024];
    for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream
            .getNextEntry()) {
        File file2 = new File(file1, zipentry.getName());
        if (zipentry.isDirectory()) {
            if (!file2.exists() && !file2.mkdirs())
                throw new IOException("Could not make directory " + file2.getPath());
        } else {
            File file3 = file2.getParentFile();
            if (file3 != null && !file3.exists() && !file3.mkdirs())
                throw new IOException("Could not make directory " + file3.getPath());
            BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file2));
            int i;
            try {
                while ((i = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
                    bufferedoutputstream.write(abyte0, 0, i);
            } catch (IOException ie) {
                ie.printStackTrace();
                // m_logger.error(ie);
                throw ie;
            }
            bufferedoutputstream.close();
        }
    }
    zipinputstream.close();
    bufferedinputstream.close();
    // m_log.debug("unpackAppJar end.");
}

From source file:com.dnielfe.manager.utils.SimpleUtils.java

/**
 * /*from w w w . j av a2s  .c o m*/
 * @param old
 *            the file to be copied/ moved
 * @param newDir
 *            the directory to copy/move the file to
 */
public static void copyToDirectory(String old, String newDir) {
    File old_file = new File(old);
    File temp_dir = new File(newDir);
    byte[] data = new byte[BUFFER];
    int read = 0;

    if (old_file.isFile() && temp_dir.isDirectory() && temp_dir.canWrite()) {
        String file_name = old.substring(old.lastIndexOf("/"), old.length());
        File cp_file = new File(newDir + file_name);

        try {
            BufferedOutputStream o_stream = new BufferedOutputStream(new FileOutputStream(cp_file));
            BufferedInputStream i_stream = new BufferedInputStream(new FileInputStream(old_file));

            while ((read = i_stream.read(data, 0, BUFFER)) != -1)
                o_stream.write(data, 0, read);

            o_stream.flush();
            i_stream.close();
            o_stream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    } else if (old_file.isDirectory() && temp_dir.isDirectory() && temp_dir.canWrite()) {
        String files[] = old_file.list();
        String dir = newDir + old.substring(old.lastIndexOf("/"), old.length());
        int len = files.length;

        if (!new File(dir).mkdir())
            return;

        for (int i = 0; i < len; i++)
            copyToDirectory(old + "/" + files[i], dir);

    } else if (old_file.isFile() && !temp_dir.canWrite() && SimpleExplorer.rootAccess) {
        RootCommands.moveCopyRoot(old, newDir);
    } else if (!temp_dir.canWrite())
        return;

    return;
}

From source file:abfab3d.io.input.ModelLoader.java

private static void unzipEntry(ZipFile zipFile, ZipArchiveEntry entry, File dest) throws IOException {

    if (entry.isDirectory()) {
        createDir(new File(dest, entry.getName()));
        return;/*  www .ja v a 2 s .  c  om*/
    }

    File outputFile = new File(dest, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        if (outputStream != null)
            outputStream.close();
        if (inputStream != null)
            inputStream.close();
    }
}

From source file:emperior.Main.java

public static void updateResumeTask(String task) {
    if (!adminmode) {
        Properties properties = new Properties();
        try {//from w  w w  . ja  v  a  2  s .  co  m
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream("Emperior.properties"));
            properties.load(stream);
            properties.setProperty("resumetask", task);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Emperior.properties"));
            properties.store(out, "");
            out.close();
            stream.close();
        } catch (Exception e) {

        }
    }
}

From source file:emperior.Main.java

public static void updateStartedWithTask(String task) {
    if (!adminmode) {
        Properties properties = new Properties();
        try {/*from w  w w  . j  a  va2 s .  c o  m*/
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream("Emperior.properties"));
            properties.load(stream);
            properties.setProperty("startedwith", task);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("Emperior.properties"));
            properties.store(out, "");
            out.close();
            stream.close();
        } catch (Exception e) {

        }
    }
}