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:io.hightide.TemplateFetcher.java

public static Path extractTemplate(Path tempFile, Path targetDir) throws IOException {
    try (FileInputStream fin = new FileInputStream(tempFile.toFile());
            BufferedInputStream in = new BufferedInputStream(fin);
            GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
            TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn)) {
        TarArchiveEntry entry;//from ww  w . jav a 2  s  .co m
        Path rootDir = null;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry.getName());
            if (entry.isDirectory()) {
                if (isNull(rootDir)) {
                    rootDir = targetDir.resolve(entry.getName());
                }
                Files.createDirectory(targetDir.resolve(entry.getName()));
            } else {
                int count;
                byte data[] = new byte[BUFFER];

                FileOutputStream fos = new FileOutputStream(targetDir.resolve(entry.getName()).toFile());
                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }

        return rootDir;
    }
}

From source file:Main.java

private static File saveFile(InputStream is, String destDir, String fileName) throws IOException {
    File dir = new File(destDir);
    fileName = getString(fileName);/* w w w  .jav  a2  s.  c  o  m*/
    if (!dir.exists()) {
        dir.mkdirs();
    }
    BufferedInputStream bis = new BufferedInputStream(is);
    BufferedOutputStream bos = null;
    try {
        File saveFile = new File(destDir, fileName);
        bos = new BufferedOutputStream(new FileOutputStream(saveFile));
        int len = -1;
        while ((len = bis.read()) != -1) {
            bos.write(len);
            bos.flush();
        }
        bos.close();
        bis.close();
        return saveFile;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:SigningProcess.java

public static void writeByteArraysToFile(String fileName, byte[] content) throws IOException {
    File file = new File(fileName);
    BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
    writer.write(content);/*w  ww  .  ja v  a2s. c  o  m*/
    writer.flush();
    writer.close();

}

From source file:Main.java

public static File saveBmpFile(Bitmap bmp, String filePath, String fileName) {
    File dirFile = new File(filePath);
    if (!dirFile.exists()) {
        dirFile.mkdir();//from w  w w  .  j  ava 2 s .  c  om
    }
    File wantSaveFile = new File(filePath + "/" + fileName);
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(wantSaveFile));
        if (fileName.contains(".jpg")) {
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        } else {
            bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
        }

        bos.flush();
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return wantSaveFile;
}

From source file:com.oneops.cms.crypto.CmsCryptoDES.java

/**
 * Generate des key.//  ww w.  j  a va2 s  .c o  m
 *
 * @param file the file
 * @throws java.io.IOException Signals that an I/O exception has occurred.
 */
public static void generateDESKey(String file) throws IOException {
    DESedeKeyGenerator kg = new DESedeKeyGenerator();
    KeyGenerationParameters kgp = new KeyGenerationParameters(new SecureRandom(),
            DESedeParameters.DES_EDE_KEY_LENGTH * 8);
    kg.init(kgp);
    byte[] key = kg.generateKey();
    BufferedOutputStream keystream = new BufferedOutputStream(new FileOutputStream(file));
    byte[] keyhex = Hex.encode(key);
    keystream.write(keyhex, 0, keyhex.length);
    keystream.flush();
    keystream.close();
}

From source file:Main.java

/**
 * Extract a zip resource into real files and directories
 * //from   www . j ava2s .c om
 * @param in typically given as getResources().openRawResource(R.raw.something)
 * @param directory target directory
 * @param overwrite indicates whether to overwrite existing files
 * @return list of files that were unpacked (if overwrite is false, this list won't include files
 *         that existed before)
 * @throws IOException
 */
public static List<File> extractZipResource(InputStream in, File directory, boolean overwrite)
        throws IOException {
    final int BUFSIZE = 2048;
    byte buffer[] = new byte[BUFSIZE];
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(in, BUFSIZE));
    List<File> files = new ArrayList<File>();
    ZipEntry entry;
    directory.mkdirs();
    while ((entry = zin.getNextEntry()) != null) {
        File file = new File(directory, entry.getName());
        files.add(file);
        if (overwrite || !file.exists()) {
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                file.getParentFile().mkdirs(); // Necessary because some zip files lack directory entries.
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFSIZE);
                int nRead;
                while ((nRead = zin.read(buffer, 0, BUFSIZE)) > 0) {
                    bos.write(buffer, 0, nRead);
                }
                bos.flush();
                bos.close();
            }
        }
    }
    zin.close();
    return files;
}

From source file:com.atlbike.etl.service.ClientFormLogin.java

private static void saveEntity(HttpEntity loginEntity) throws IOException, FileNotFoundException {
    InputStream inStream = loginEntity.getContent();
    BufferedInputStream bis = new BufferedInputStream(inStream);
    String path = "localFile.csv";
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
    int byteCount = 0;
    int inByte;//from  w  w  w .ja v a  2 s  . c  om
    while ((inByte = bis.read()) != -1) {
        bos.write(inByte);
        byteCount++;
    }
    bis.close();
    bos.close();
    System.out.println("Byte Count: " + byteCount);
}

From source file:Main.java

public static void saveISToFile(InputStream is, String fileName) throws IOException {
    File file = new File(fileName);
    file.getParentFile().mkdirs();//from   w w  w  . j  a  v  a  2 s . com
    File tempFile = new File(fileName + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    BufferedInputStream bis = new BufferedInputStream(is);
    byte[] barr = new byte[32768];
    int read = 0;
    while ((read = bis.read(barr)) > 0) {
        bos.write(barr, 0, read);
    }
    bis.close();
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    file.delete();
    tempFile.renameTo(file);
}

From source file:com.ccoe.build.utils.CompressUtils.java

/**
 * Uncompress a zip to files//from  w w w. ja  v  a  2 s  .c o  m
 * @param zip
 * @param unzipdir
 * @param isNeedClean
 * @return
 * @throws FileNotFoundException 
 * @throws IOException 
 */
public static List<File> unCompress(File zip, String unzipdir) throws IOException {
    ArrayList<File> unzipfiles = new ArrayList<File>();

    FileInputStream fi = new FileInputStream(zip);
    ZipInputStream zi = new ZipInputStream(new BufferedInputStream(fi));
    try {

        ZipEntry entry;
        while ((entry = zi.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            int count;
            byte data[] = new byte[BUFFER];
            File unzipfile = new File(unzipdir + File.separator + entry.getName());
            FileOutputStream fos = new FileOutputStream(unzipfile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((count = zi.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();

            unzipfiles.add(unzipfile);
        }

    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(zi);
    }

    return unzipfiles;
}

From source file:in.xebia.poc.FileUploadUtils.java

public static boolean bufferedCopyStream(InputStream inStream, OutputStream outStream) throws Exception {
    BufferedInputStream bis = new BufferedInputStream(inStream);
    BufferedOutputStream bos = new BufferedOutputStream(outStream);
    while (true) {
        int data = bis.read();
        if (data == -1) {
            break;
        }/*from  w  w  w  .j  a  va  2 s.  co m*/
        bos.write(data);
    }
    bos.flush();
    bos.close();
    return true;
}