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.runwaysdk.controller.ErrorUtility.java

private static String compress(String message) {
    try {//from   ww  w  . j  a  v  a  2  s .  com
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BufferedOutputStream bufos = new BufferedOutputStream(new GZIPOutputStream(bos));
        bufos.write(message.getBytes("UTF-8"));
        bufos.close();
        bos.close();

        byte[] bytes = bos.toByteArray();

        return Base64.encodeToString(bytes, false);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static File getFileFromBytes(String name, String path) {
    byte[] b = name.getBytes();
    BufferedOutputStream stream = null;
    File file = null;/*w  w  w. j av  a 2s .co  m*/
    try {
        file = new File(Environment.getExternalStorageDirectory() + path);
        FileOutputStream fstream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return file;
}

From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java

public static void download(String workingDir, String URL, String filename) throws IOException {
    workingDir = workingDir + filename;//ww  w. ja  v  a  2  s.c om

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(URL);

    HttpResponse response = httpclient.execute(httpget);

    //      System.out.println(response.getStatusLine());
    HttpEntity entity = response.getEntity();
    if (entity != null) {

        InputStream instream = entity.getContent();

        try {
            BufferedInputStream bis = new BufferedInputStream(instream);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(workingDir)));
            int inByte;
            while ((inByte = bis.read()) != -1) {
                bos.write(inByte);
            }
            bis.close();
            bos.close();
            //       unzip("ICD10.zip");
        } catch (IOException ex) {
            throw ex;
        } catch (RuntimeException ex) {
            httpget.abort();
            throw ex;
        } finally {
            instream.close();
        }
        httpclient.getConnectionManager().shutdown();

    }
}

From source file:com.ibm.jaggr.core.util.ZipUtil.java

/**
 * Extracts the file entry to the location specified by {@code file}
 *
 * @param entry/*from   ww  w  .  j av  a2 s .com*/
 *            the {@link ZipEntry} object for the directory being extracted
 * @param zipIn
 *            the zip input stream to read from
 * @param file
 *            the {@link File} object for the target file
 * @throws IOException
 */
private static void extractFile(ZipEntry entry, ZipInputStream zipIn, File file) throws IOException {
    final String sourceMethod = "extractFile"; //$NON-NLS-1$
    final boolean isTraceLogging = log.isLoggable(Level.FINER);
    if (isTraceLogging) {
        log.entering(sourceClass, sourceMethod, new Object[] { entry, zipIn, file });
    }

    Files.createParentDirs(file);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
    try {
        IOUtils.copy(zipIn, bos);
    } finally {
        bos.close();
    }
    if (!file.setLastModified(entry.getTime())) {
        throw new IOException("Failed to set last modified time for " + file.getAbsolutePath()); //$NON-NLS-1$
    }

    if (isTraceLogging) {
        log.exiting(sourceClass, sourceMethod);
    }
}

From source file:Main.java

public static void copyFile(File in, File out) throws IOException {
    // avoids copying a file to itself
    if (in.equals(out)) {
        return;/*from  w w w.  java2  s.c  o  m*/
    }
    // ensure the output file location exists
    out.getCanonicalFile().getParentFile().mkdirs();

    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(in));
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(out));

    // a temporary buffer to read into
    byte[] tmpBuffer = new byte[8192];
    int len = 0;
    while ((len = fis.read(tmpBuffer)) != -1) {
        // add the temp data to the output
        fos.write(tmpBuffer, 0, len);
    }
    // close the input stream
    fis.close();
    // close the output stream
    fos.flush();
    fos.close();
}

From source file:Main.java

public static File getFileFromBytes(byte[] b, String outputFile) {
    File ret = null;/*from  w w w .ja  v  a2s.  c o  m*/
    BufferedOutputStream stream = null;
    try {
        ret = new File(outputFile);
        FileOutputStream fstream = new FileOutputStream(ret);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        // log.error("helper:get file from byte process error!");
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // log.error("helper:get file from byte process error!");
                e.printStackTrace();
            }
        }
    }
    return ret;
}

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

/**
 * Unpack data from the stream to specified directory.
 * /*from   w ww  . jav a 2 s  . com*/
 * @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:com.almarsoft.GroundhogReader.lib.FSUtils.java

public static long writeByteArrayToDiskFileAndGetSize(byte[] data, String directory, String fileName)
        throws IOException {

    File outDir = new File(directory);
    if (!outDir.exists())
        outDir.mkdirs();/*from  w w  w  . jav  a 2s. co m*/

    File outFile = new File(directory, fileName);

    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outFile));

    try {
        fos.write(data);
    } finally {
        fos.close();
    }

    return outFile.length();
}

From source file:Main.java

public static void downLoadFile(final String strUrl, final String fileURL, final int bufferLength) {
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {/*from  w w w.j  a v a  2 s. c o  m*/

        in = new BufferedInputStream(new URL(strUrl).openStream());
        File img = new File(fileURL);
        out = new BufferedOutputStream(new FileOutputStream(img));
        byte[] buf = new byte[bufferLength];
        int count = in.read(buf);
        while (count != -1) {
            out.write(buf, 0, count);
            count = in.read(buf);
        }
        in.close();
        out.close();

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

    }

}

From source file:com.pieframework.runtime.utils.Zipper.java

public static void unzip(String zipFile, String toDir) throws ZipException, IOException {

    int BUFFER = 2048;
    File file = new File(zipFile);

    ZipFile zip = new ZipFile(file);
    String newPath = toDir;/*from   w  w  w . ja va 2 s .c o  m*/

    File toDirectory = new File(newPath);
    if (!toDirectory.exists()) {
        toDirectory.mkdir();
    }
    Enumeration zipFileEntries = zip.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

        String currentEntry = entry.getName();
        File destFile = new File(newPath, FilenameUtils.separatorsToSystem(currentEntry));
        //System.out.println(currentEntry);
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    zip.close();

}