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:frameworks.Masken.java

public static void uncompressTarGZ(File tarFile, File dest) throws IOException {
    dest.mkdir();//from   w  w  w  .  ja  v a  2  s .  c  o  m
    TarArchiveInputStream tarIn = null;

    tarIn = new TarArchiveInputStream(
            new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile))));

    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    // tarIn is a TarArchiveInputStream
    while (tarEntry != null) {// create a file with the same name as the tarEntry
        File destPath = new File(dest, tarEntry.getName());
        System.out.println("working: " + destPath.getCanonicalPath());
        if (tarEntry.isDirectory()) {
            destPath.mkdirs();
        } else {
            if (!destPath.getParentFile().exists()) {
                destPath.getParentFile().mkdirs();
            }
            destPath.createNewFile();
            //byte [] btoRead = new byte[(int)tarEntry.getSize()];
            byte[] btoRead = new byte[1024];
            //FileInputStream fin 
            //  = new FileInputStream(destPath.getCanonicalPath());
            BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
            int len = 0;

            while ((len = tarIn.read(btoRead)) != -1) {
                bout.write(btoRead, 0, len);
            }

            bout.close();
            btoRead = null;

        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}

From source file:de.jgoestl.massdatagenerator.MassDataGenerator.java

private static void writeOutput(String outputPath, StringBuilder output) {
    BufferedOutputStream bos = null;
    try {/* w  w  w . j  a va 2  s.c o  m*/
        File f = new File(outputPath);
        bos = new BufferedOutputStream(new FileOutputStream(f));
        bos.write(output.toString().getBytes());
    } catch (FileNotFoundException ex) {
        System.out.println("Output-File \"" + outputPath + "\" not found");
        try {
            System.in.read();
        } catch (IOException e) {
            Logger.getLogger(MassDataGenerator.class.getName()).log(Level.SEVERE, null, e);
        }
        System.exit(1);
    } catch (IOException ex) {
        Logger.getLogger(MassDataGenerator.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (bos != null)
                bos.close();
        } catch (IOException ex) {
            Logger.getLogger(MassDataGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java

/**
 * <p><em>Title: Create a temporary File from a Base64 encoded byteStream</em></p>
 * <p>Description: Method creates a temporary file from the bytestream 
 * representing the orginal PDF, that should be converted</p>
 * /*w ww  .j  av  a  2 s .c  om*/
 * @param stream <code>String</code> 
 * @return <code>String</code> Filename of newly created temporary File
 */
public static String saveStreamToTempFile(String fileName, String stream) {
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    try {
        //System.out.println("Base64 kodierter Stream: " + stream.length());
        inputFile = new File(Configuration.getTempDirPath() + fileName);
        log.debug(Configuration.getTempDirPath());
        fos = new FileOutputStream(inputFile);
        bos = new BufferedOutputStream(fos);
        bos.write(Base64.decodeBase64(stream.getBytes("UTF-8")));

    } catch (IOException ioExc) {
        log.error(ioExc);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
    }
    log.debug("File-Size: " + inputFile.length());
    return inputFile.getName();
}

From source file:com.eviware.soapui.integration.exporter.ProjectExporter.java

public static void unpackageAll(String archive, String path) {
    try {//  w w w. ja  v a2s.  c o  m
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(archive);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(path + File.separator + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch (Exception e) {
        SoapUI.logError(e);
    }
}

From source file:com.twemyeez.picklr.InstallManager.java

public static void unzip() {

    // Firstly get the working directory
    String workingDirectory = Minecraft.getMinecraft().mcDataDir.getAbsolutePath();

    // If it ends with a . then remove it
    if (workingDirectory.endsWith(".")) {
        workingDirectory = workingDirectory.substring(0, workingDirectory.length() - 1);
    }//  w w  w  .  ja  v a  2s  .com

    // If it doesn't end with a / then add it
    if (!workingDirectory.endsWith("/") && !workingDirectory.endsWith("\\")) {
        workingDirectory = workingDirectory + "/";
    }

    // Use a test file to see if libraries installed
    File file = new File(workingDirectory + "mods/mp3spi1.9.5.jar");

    // If the libraries are installed, return
    if (file.exists()) {
        System.out.println("Checking " + file.getAbsolutePath());
        System.out.println("Target file exists, so not downloading API");
        return;
    }

    // Now try to download the libraries
    try {

        String location = "http://www.javazoom.net/mp3spi/sources/mp3spi1.9.5.zip";

        // Define the URL
        URL url = new URL(location);

        // Get the ZipInputStream
        ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream((url).openStream()));

        // Use a temporary ZipEntry as a buffer
        ZipEntry zipFile;

        // While there are more file entries
        while ((zipFile = zipInput.getNextEntry()) != null) {
            // Check if it is one of the file names that we want to copy
            Boolean required = false;
            if (zipFile.getName().indexOf("mp3spi1.9.5.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("jl1.0.1.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("tritonus_share.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("LICENSE.txt") != -1) {
                required = true;
            }

            // If it is, then we shall now copy it
            if (!zipFile.getName().replace("MpegAudioSPI1.9.5/", "").equals("") && required) {

                // Get the file location
                String tempFile = new File(zipFile.getName()).getName();

                tempFile = tempFile.replace("LICENSE.txt", "MpegAudioLicence.txt");

                // Initialise the target file
                File targetFile = (new File(
                        workingDirectory + "mods/" + tempFile.replace("MpegAudioSPI1.9.5/", "")));

                // Print a debug/alert message
                System.out.println("Picklr is extracting to " + workingDirectory + "mods/"
                        + tempFile.replace("MpegAudioSPI1.9.5/", ""));

                // Make parent directories if required
                targetFile.getParentFile().mkdirs();

                // If the file does not exist, create it
                if (!targetFile.exists()) {
                    targetFile.createNewFile();
                }

                // Create a buffered output stream to the destination
                BufferedOutputStream destinationOutput = new BufferedOutputStream(
                        new FileOutputStream(targetFile, false), 2048);

                // Store the data read
                int bytesRead;

                // Data buffer
                byte dataBuffer[] = new byte[2048];

                // While there is still data to write
                while ((bytesRead = zipInput.read(dataBuffer, 0, 2048)) != -1) {
                    // Write it to the output stream
                    destinationOutput.write(dataBuffer, 0, bytesRead);
                }

                // Flush the output
                destinationOutput.flush();

                // Close the output stream
                destinationOutput.close();
            }

        }
        // Close the zip input
        zipInput.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Compress files to *.zip./*from  w w w.  ja  va2 s.c om*/
 * @param fileName
 *        file name to compress
 * @return compressed file.
 */
public static String compress(String fileName) {
    String targetFile = null;
    File sourceFile = new File(fileName);
    Vector<File> vector = getAllFiles(sourceFile);
    try {
        if (sourceFile.isDirectory()) {
            targetFile = fileName + ".zip";
        } else {
            char ch = '.';
            targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip";
        }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        ZipOutputStream zipos = new ZipOutputStream(bos);
        byte[] data = new byte[BUFFER];
        if (vector.size() > 0) {
            for (int i = 0; i < vector.size(); i++) {
                File file = vector.get(i);
                zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file)));
                bis = new BufferedInputStream(new FileInputStream(file));
                int count;
                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    zipos.write(data, 0, count);
                }
                bis.close();
                zipos.closeEntry();
            }
            zipos.close();
            bos.close();
            return targetFile;
        } else {
            File zipNullfile = new File(targetFile);
            zipNullfile.getParentFile().mkdirs();
            zipNullfile.mkdir();
            return zipNullfile.getAbsolutePath();
        }
    } catch (IOException e) {
        LOG.error("[compress]", e);
        return "error";
    }
}

From source file:edu.ku.brc.specify.web.HttpLargeFileTransfer.java

/**
 * @param infileName/*  w w  w  .j  a  v  a  2  s  . com*/
 * @param outFileName
 * @param changeListener
 * @return
 */
public static boolean uncompressFile(final String infileName, final String outFileName) {
    File file = new File(infileName);
    if (file.exists()) {
        long fileSize = file.length();
        if (fileSize > 0) {
            try {
                GZIPInputStream fis = new GZIPInputStream(new FileInputStream(infileName));
                BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outFileName));

                byte[] bytes = new byte[BUFFER_SIZE * 10];

                while (true) {
                    int len = fis.read(bytes);
                    if (len > 0) {
                        fos.write(bytes, 0, len);
                    } else {
                        break;
                    }
                }

                fis.close();
                fos.close();

                return true;

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            // file doesn't exist
        }
    } else {
        // file doesn't exist
    }
    return false;
}

From source file:com.glaf.core.util.FileUtils.java

public static void save(String filename, InputStream inputStream) {
    if (filename == null || inputStream == null) {
        return;/*from w ww .  j ava  2s . c o  m*/
    }
    String path = "";
    String sp = System.getProperty("file.separator");
    if (filename.indexOf(sp) != -1) {
        path = filename.substring(0, filename.lastIndexOf(sp));
    }
    if (filename.indexOf("/") != -1) {
        path = filename.substring(0, filename.lastIndexOf("/"));
    }
    path = getJavaFileSystemPath(path);
    java.io.File dir = new java.io.File(path + sp);
    mkdirsWithExistsCheck(dir);
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        bis = new BufferedInputStream(inputStream);
        bos = new BufferedOutputStream(new FileOutputStream(filename));
        int bytesRead = 0;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = bis.read(buffer, 0, BUFFER_SIZE)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }
        bos.flush();
        bis.close();
        bos.close();
        bis = null;
        bos = null;
    } catch (IOException ex) {
        bis = null;
        bos = null;
        throw new RuntimeException(ex);
    } finally {
        try {
            if (bis != null) {
                bis.close();
                bis = null;
            }
            if (bos != null) {
                bos.close();
                bos = null;
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:Main.java

public static boolean downloadFile(File file, URL url) throws IOException {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    HttpURLConnection conn = null;
    try {/* ww w  .ja v a 2  s.co  m*/
        conn = (HttpURLConnection) url.openConnection();
        bis = new BufferedInputStream(conn.getInputStream());
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int contentLength = conn.getContentLength();

        byte[] buffer = new byte[BUFFER_SIZE];
        int read = 0;
        int count = 0;
        while ((read = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
            count += read;
        }
        if (count < contentLength)
            return false;
        int responseCode = conn.getResponseCode();
        if (responseCode / 100 != 2) {
            // error
            return false;
        }
        // finished
        return true;
    } finally {
        try {
            bis.close();
            bos.close();
            conn.disconnect();
        } catch (Exception e) {
        }
    }
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Extracts a zip entry (file entry)./*from   w  ww. j  a  va  2 s . c  om*/
 *
 * @param zipIn the zip in
 * @param filePath the file path
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[4096];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}