Example usage for java.io BufferedInputStream close

List of usage examples for java.io BufferedInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

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

Usage

From source file:Main.java

/**
 * Compress files to a zip//  www  .  j  a v  a 2s .  co m
 * @param zip
 * @param files
 * @throws IOException 
 */
public static void compressToZip(File zip, File[] files) throws IOException {
    byte data[] = new byte[BUFFER];
    FileOutputStream fozip = new FileOutputStream(zip);
    ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip));
    for (int i = 0; i < files.length; i++) {
        System.out.println("Adding:" + files[i]);
        FileInputStream fi = new FileInputStream(files[i]);
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry zipentry = new ZipEntry(files[i].getName());
        zo.putNextEntry(zipentry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            zo.write(data, 0, count);
        }
        origin.close();
    }
    zo.close();
}

From source file:jeeves.utils.BLOB.java

private static void copy(InputStream in, OutputStream output) throws IOException {
    BufferedInputStream input = new BufferedInputStream(in);
    try {/*ww  w  .  ja  v  a  2 s  .  c  o m*/
        byte buffer[] = new byte[BUF_SIZE];
        int nRead;
        do {
            nRead = input.read(buffer, 0, BUF_SIZE);
            output.write(buffer, 0, nRead);

        } while (nRead == BUF_SIZE);
        input.close();
    } catch (IOException e) {
        input.close();
        throw e;
    }
}

From source file:msec.org.TarUtil.java

private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception {

    TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName());

    entry.setSize(file.length());//from www .ja  va  2 s  . c o m

    taos.putArchiveEntry(entry);

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    int count;
    byte data[] = new byte[BUFFERSZ];
    while ((count = bis.read(data, 0, BUFFERSZ)) != -1) {
        taos.write(data, 0, count);
    }

    bis.close();

    taos.closeArchiveEntry();
}

From source file:Main.java

public static boolean copyFile2(File src, File dst) {
    FileInputStream i;//from w  w  w .  ja va 2  s  . co m
    try {
        i = new FileInputStream(src);
        BufferedInputStream in = new BufferedInputStream(i);
        FileOutputStream o = new FileOutputStream(dst);
        BufferedOutputStream out = new BufferedOutputStream(o);
        byte[] b = new byte[1024 * 5];
        int len;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        in.close();
        out.close();
        o.close();
        i.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

private static boolean addEntryInputStream(ZipOutputStream zos, String entryName, InputStream inputStream) {
    final ZipEntry zipEntry = new ZipEntry(entryName);
    try {/*from   w  ww  .j  a  v  a  2 s  .c  om*/
        zos.putNextEntry(zipEntry);
    } catch (final ZipException e) {

        // Ignore duplicate entry - no overwriting
        return false;
    } catch (final IOException e) {
        throw new RuntimeException("Error while adding zip entry " + zipEntry, e);
    }
    final int buffer = 2048;
    final BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, buffer);
    int count;
    try {
        final byte data[] = new byte[buffer];
        while ((count = bufferedInputStream.read(data, 0, buffer)) != -1) {
            zos.write(data, 0, count);
        }
        bufferedInputStream.close();
        inputStream.close();
        zos.closeEntry();
        return true;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.mrbussy.pdfsplitter.Application.java

/**
 * Retrieve the license file from the resource stream.
 * @return a string containing the license file text
 *//*from w  w w  .  j  av a 2 s  .  co m*/
private static String getShortLicenseText() {
    String licenseText = null;
    BufferedInputStream inputStream = (BufferedInputStream) Application.class
            .getResourceAsStream("/files/license_short.txt"); //$NON-NLS-1$
    try {
        licenseText = IOUtils.toString(inputStream);
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return licenseText;
}

From source file:com.ibuildapp.romanblack.CataloguePlugin.imageloader.Utils.java

public static Bitmap processBitmap(String fileName, Bitmap.Config config, int widthLimit) {
    Bitmap bitmap = null;/*from w ww. ja  va2s . co  m*/

    try {
        File tempFile = new File(fileName);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(tempFile));

        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(fileInputStream, null, opts);
        fileInputStream.close();
        fileInputStream = new BufferedInputStream(new FileInputStream(tempFile));

        //Find the correct scale value. It should be the power of 2.
        int width = opts.outWidth;
        int scale = 1;

        while (true) {
            int halfWidth = width / 2;

            if (halfWidth < widthLimit && (widthLimit - halfWidth) > widthLimit / 4)
                break;

            width = halfWidth;
            scale *= 2;
        }

        opts = new BitmapFactory.Options();
        opts.inSampleSize = scale;
        opts.inPreferredConfig = config;

        try {
            System.gc();
            bitmap = BitmapFactory.decodeStream(fileInputStream, null, opts);

            if (bitmap != null)
                return bitmap;
        } catch (Exception ex) {
        } catch (OutOfMemoryError e) {
        }

        fileInputStream.close();
        fileInputStream = new BufferedInputStream(new FileInputStream(tempFile));

        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            System.gc();
            bitmap = BitmapFactory.decodeStream(fileInputStream, null, opts);

            if (bitmap != null)
                return bitmap;
        } catch (Exception ex) {
        } catch (OutOfMemoryError ex) {
        }

        fileInputStream.close();
        fileInputStream = new BufferedInputStream(new FileInputStream(tempFile));

        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            System.gc();
            bitmap = BitmapFactory.decodeStream(fileInputStream, null, opts);
        } catch (Exception ex) {
        } catch (OutOfMemoryError ex) {
        }

        fileInputStream.close();
    } catch (Exception exception) {
        exception.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException {
    File srcFile = new File(srcFilePath);
    if (!srcFile.exists() || srcFile.isDirectory()) {
        return;//from  w w  w  .ja  va  2s.  c  o m
    }
    BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
    FileOutputStream fos = context.openFileOutput(dictFileName, 0);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] b = new byte[1024 * 4];
    int len;
    while ((len = inBufferedInputStream.read(b)) != -1) {
        bos.write(b, 0, len);
        bos.flush();
    }
    inBufferedInputStream.close();
    bos.close();
}

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

/**
 * Extracts the given archive into the given destination directory
 * /*from w  w  w.j a  v a  2 s .  c o m*/
 * @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: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   ww  w . jav  a  2  s.c  om
 * @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();
}