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

public static byte[] readFromInputStream(InputStream inputStream) throws IOException {

    if (inputStream == null)
        return null;

    BufferedInputStream bin = new BufferedInputStream(inputStream);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024 * 8];
    int len = 0;/*from ww w . j av a  2  s .  c  o m*/

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

    bin.close();

    return bos.toByteArray();
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;/*from  www  .j av  a  2s.c  o m*/
    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();

    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    key = keygen.generateKey();
    ObjectOutputStream keyFileout = new ObjectOutputStream(new FileOutputStream("DESKey.ser"));
    keyFileout.writeObject(key);
    keyFileout.close();
    Cipher cipher = null;
    cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f1));

    CipherOutputStream out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(f2)), cipher);
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i != -1);
    in.close();
    out.close();
}

From source file:com.github.wolfposd.jdpkg.deb.DpkgDeb.java

private static void compressArchive(File source, File destination, String compression)
        throws CompressorException, IOException {
    OutputStream archiveStream = new FileOutputStream(destination);
    CompressorOutputStream comp = new CompressorStreamFactory().createCompressorOutputStream(compression,
            archiveStream);//from  w ww  . jav  a 2  s .  c  om

    BufferedInputStream input = new BufferedInputStream(new FileInputStream(source));

    IOUtils.copy(input, comp);
    input.close();
    comp.close();

}

From source file:Main.java

public static byte[] readFile(File file) throws IOException {
    int len = (int) file.length();
    if (len == 0) {
        return new byte[0];
    }/* w  w  w.  j  a v a 2  s. c  o  m*/

    byte[] data = null;
    BufferedInputStream bis = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        data = new byte[len];
        bis.read(data);
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
            }
        }
    }

    return data;
}

From source file:com.github.wolfposd.jdpkg.deb.DpkgDeb.java

public static void fileEntryToDestination(File source, ArchiveOutputStream archive, boolean atRoot)
        throws IOException {

    TarArchiveEntry entry;//from w  ww.  j a  v  a  2s.c  o m
    if (atRoot) {
        entry = new TarArchiveEntry(source.getName());
    } else {
        entry = new TarArchiveEntry(source.getPath().replace(BuildFile, ""));
    }
    entry.setSize(source.length());
    entry.setMode(TarArchiveEntry.DEFAULT_FILE_MODE);

    archive.putArchiveEntry(entry);

    BufferedInputStream input = new BufferedInputStream(new FileInputStream(source));
    IOUtils.copy(input, archive);

    input.close();
    archive.closeArchiveEntry();
}

From source file:com.github.wolfposd.jdpkg.deb.DpkgDeb.java

public static void writeDebFile(File destination, File[] inputsfiles) throws ArchiveException, IOException {
    ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.AR,
            new FileOutputStream(destination));

    for (File file : inputsfiles) {
        ArArchiveEntry entry = new ArArchiveEntry(file, file.getName());
        archive.putArchiveEntry(entry);//w ww.ja  va2s. c  o m

        BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(input, archive);
        input.close();

        archive.closeArchiveEntry();
    }
    archive.finish();
    archive.close();

}

From source file:com.shopzilla.hadoop.repl.commands.util.ClusterStateManager.java

private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir)
        throws IOException {
    // Create an entry for the file
    taos.putArchiveEntry(new TarArchiveEntry(file, dir + File.separator + file.getName()));
    if (file.isFile()) {
        // Add the file to the archive
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);//from   w ww  .j av a 2s  .  co m
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        // close the archive entry
        taos.closeArchiveEntry();
        // go through all the files in the directory and using recursion, add them to the archive
        for (File childFile : file.listFiles()) {
            addFilesToCompression(taos, childFile, dir + File.separator + file.getName());
        }
    }
}

From source file:Main.java

/**
 * extracts a zip file to the given dir/*from  ww  w .j  ava 2  s. c  om*/
 * @param archive
 * @param destDir
 * @throws IOException 
 * @throws ZipException 
 * @throws Exception
 */
public static void extractZipArchive(File archive, File destDir) throws ZipException, 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()) {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destDir, entryFileName)));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
}

From source file:com.plotsquared.iserver.util.FileUtils.java

public static byte[] getBytes(final File file, final int buffer) {
    byte[] bytes = new byte[0];
    try {//from   w  w w  . j  a  va  2  s . com
        BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file), buffer);
        bytes = IOUtils.toByteArray(stream);
        stream.close();
    } catch (final Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:MainServer.UploadServlet.java

public static void inputStream2File(InputStream is, String savePath) throws Exception {
    System.out.println("Save path:" + savePath);
    File file = new File(savePath);
    InputStream inputStream = is;
    BufferedInputStream fis = new BufferedInputStream(inputStream);
    FileOutputStream fos = new FileOutputStream(file);
    int f;//from  w ww . j  a va2 s .co  m
    while ((f = fis.read()) != -1) {
        fos.write(f);
    }
    fos.flush();
    fos.close();
    fis.close();
    inputStream.close();
}