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

public static Bitmap getAndSetBitmapFromNet(String urlPath) {

    Bitmap bm = null;//from   w w  w .j av a 2 s  . co m

    if (urlPath != null) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new URL(urlPath).openStream(), 1024);
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024);
            copy(bis, out);
            out.flush();
            final byte[] data = dataStream.toByteArray();
            bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            Log.i(I, "data.length: " + data.length);
            out.close();
            dataStream.close();
            bis.close();
            bm = processBitmap(bm);
        } catch (IOException e) {
            Log.i(I, "URL Connection or Bitmap processing Exception");
            e.printStackTrace();
        }
    }
    return bm;
}

From source file:msec.org.TarUtil.java

private static void dearchiveFile(File destFile, TarArchiveInputStream tais) throws Exception {

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

    int count;// ww  w . j av a 2s.  co m
    byte data[] = new byte[BUFFERSZ];
    while ((count = tais.read(data, 0, BUFFERSZ)) != -1) {
        bos.write(data, 0, count);
    }

    bos.close();
}

From source file:downloadwolkflow.getWorkFlowList.java

private static void downloadFiles(String downloadUrl, CloseableHttpClient httpclient) {
    HttpGet httpget = new HttpGet(downloadUrl);
    HttpEntity entity = null;//w w w  .  ja  v a  2s  .  c o  m
    try {
        HttpResponse response = httpclient.execute(httpget);
        entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            String filename = downloadUrl.split("/")[downloadUrl.split("/").length - 1].split("\\?")[0];
            System.out.println(filename);
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File("data/" + filename)));
            int readedByte;
            while ((readedByte = bis.read()) != -1) {
                bos.write(readedByte);
            }
            bis.close();
            bos.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(DownloadFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Main.java

public static boolean bytesToFile(byte[] bfile, File file) {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;//from   w  w  w.ja  va 2 s.  c  o m
    try {
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(bfile);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return false;
}

From source file:io.crate.testing.Utils.java

static void uncompressTarGZ(File tarFile, File dest) throws IOException {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(
            new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));

    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    // tarIn is a TarArchiveInputStream
    while (tarEntry != null) {
        Path entryPath = Paths.get(tarEntry.getName());

        if (entryPath.getNameCount() == 1) {
            tarEntry = tarIn.getNextTarEntry();
            continue;
        }/*w w w . ja  v a2s  .co m*/

        Path strippedPath = entryPath.subpath(1, entryPath.getNameCount());
        File destPath = new File(dest, strippedPath.toString());

        if (tarEntry.isDirectory()) {
            destPath.mkdirs();
        } else {
            destPath.createNewFile();
            byte[] btoRead = new byte[1024];
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
            int len;
            while ((len = tarIn.read(btoRead)) != -1) {
                bout.write(btoRead, 0, len);
            }

            bout.close();
            if (destPath.getParent().equals(dest.getPath() + "/bin")) {
                destPath.setExecutable(true);
            }
        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}

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 w  w  .j ava 2 s.com*/
 * @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:at.illecker.sentistorm.commons.util.io.IOUtils.java

public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) {
    try {//from w w  w  .  j av  a  2 s  .co  m
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(inputTarGzStream);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        // read Tar entries
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            if (logging) {
                LOG.info("Extracting: " + outDir + File.separator + entry.getName());
            }
            if (entry.isDirectory()) { // create directory
                File f = new File(outDir + File.separator + entry.getName());
                f.mkdirs();
            } else { // decompress file
                int count;
                byte data[] = new byte[EXTRACT_BUFFER_SIZE];

                FileOutputStream fos = new FileOutputStream(outDir + File.separator + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, EXTRACT_BUFFER_SIZE);
                while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }

        // close input stream
        tarIn.close();

    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    }
}

From source file:Main.java

public static boolean unzipFile(InputStream fis, String destDir) {
    final byte[] buffer = new byte[4096];
    ZipInputStream zis = null;//from w ww  . j  a  v a  2s. c  om
    Log.e("Unzip", "destDir = " + destDir);
    try {
        // make sure the directory is existent
        File dstFile = new File(destDir);
        if (!dstFile.exists()) {
            dstFile.mkdirs();
        } else {
            int fileLenght = dstFile.listFiles().length;
            if (fileLenght >= 2) {
                return true;
            }
        }
        zis = new ZipInputStream(fis);
        ZipEntry entry;

        while ((entry = zis.getNextEntry()) != null) {
            String fileName = entry.getName();

            if (entry.isDirectory()) {
                new File(destDir, fileName).mkdirs();

            } else {
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(new File(destDir, fileName)));
                int lenRead;

                while ((lenRead = zis.read(buffer)) != -1) {
                    bos.write(buffer, 0, lenRead);
                }

                bos.close();
            }

            zis.closeEntry();
        }

        return true;

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

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

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

public static void decompress(InputStream tarGzInputStream, Path outDir) throws IOException {
    GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(tarGzInputStream);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream);
    TarArchiveEntry entry;/*from  w w w .  j  ava 2 s  .c  o  m*/
    int bufferSize = 1024;
    while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) {
        String entryName = entry.getName();
        // strip out the leading directory like the --strip tar argument
        String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1);
        if (entryNameWithoutLeadingDir.isEmpty()) {
            continue;
        }
        Path outFile = outDir.resolve(entryNameWithoutLeadingDir);
        if (entry.isDirectory()) {
            outFile.toFile().mkdirs();
            continue;
        }
        int count;
        byte data[] = new byte[bufferSize];
        BufferedOutputStream fios = new BufferedOutputStream(new FileOutputStream(outFile.toFile()),
                bufferSize);
        while ((count = tarInput.read(data, 0, bufferSize)) != -1) {
            fios.write(data, 0, count);
        }
        fios.close();
    }
    tarInput.close();
    gzipStream.close();
}

From source file:Util.java

/**
 * Writes the specified byte[] to the specified File path.
 * /*from   w  w w  . j a  va  2  s .co 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) {
            }
        }
    }
}