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 boolean bufferedCopyStream(InputStream inStream, OutputStream outStream, boolean closeStream)
        throws Exception {
    BufferedInputStream bis = new BufferedInputStream(inStream);
    BufferedOutputStream bos = new BufferedOutputStream(outStream);
    while (true) {
        int data = bis.read();
        if (data == -1) {
            break;
        }/* w w w.  j a v a  2s  . c  o m*/
        bos.write(data);
    }
    bos.flush();
    if (closeStream) {
        bos.close();
    }
    return true;
}

From source file:Main.java

/**
 * Method to save audio file to SD card//from   ww  w .ja  va  2s  . co  m
 * @param course_title, title of course
 * @param format, the file format, for example .mp3, .txt
 */
public static boolean copyToStorage(File source_file, String course_title, String format) {

    File output_file = getEmptyFileWithStructuredPath(course_title, format);

    try {
        byte[] buffer = new byte[4096];
        int bytesToHold;

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source_file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output_file));

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

        bos.flush();
        bos.close();

        return true;

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

    return false;
}

From source file:net.geoprism.SystemLogoSingletonDTO.java

/**
 * Uploads a banner file to the server for persistance. Calling this method will also populate the client-side cache
 * for future calls to getBannerFilePath.
 * /*  ww  w.ja  v  a 2  s.  c o m*/
 * @param clientRequest
 * @param fileStream
 * @param fileName
 */
public static void uploadBannerAndCache(com.runwaysdk.constants.ClientRequestIF clientRequest,
        java.io.InputStream fileStream, java.lang.String fileName) {
    String tempDir = LocalProperties.getJspDir() + "/../uploaded_images";
    new File(tempDir).mkdir();
    bannerCache = new File(tempDir, fileName);

    try {
        // Write the file locally to our cache
        FileOutputStream fos = new FileOutputStream(bannerCache);
        BufferedOutputStream buffer = new BufferedOutputStream(fos);
        IOUtils.copy(fileStream, buffer);
        buffer.close();
        fos.close();

        // Send the cache file to the server for vault persistance.
        FileInputStream serverInput = new FileInputStream(bannerCache);
        SystemLogoSingletonDTOBase.uploadBanner(clientRequest, serverInput, fileName);
        serverInput.close();
    } catch (IOException e) {
        logger.error("Error creating image file [" + fileName + "].", e);
        return;
    }
}

From source file:net.geoprism.SystemLogoSingletonDTO.java

/**
 * Uploads a mini logo file to the server for persistance. Calling this method will also populate the client-side
 * cache for future calls to getMiniLogoFilePath.
 * //from   w w w .jav a  2 s .com
 * @param clientRequest
 * @param fileStream
 * @param fileName
 */
public static void uploadMiniLogoAndCache(com.runwaysdk.constants.ClientRequestIF clientRequest,
        java.io.InputStream fileStream, java.lang.String fileName) {
    String tempDir = LocalProperties.getJspDir() + "/../uploaded_images";
    new File(tempDir).mkdir();
    miniLogoCache = new File(tempDir, fileName);

    try {
        // Write the file locally to our cache
        FileOutputStream fos = new FileOutputStream(miniLogoCache);
        BufferedOutputStream buffer = new BufferedOutputStream(fos);
        IOUtils.copy(fileStream, buffer);
        buffer.close();
        fos.close();

        // Send the cache file to the server for vault persistance.
        FileInputStream serverInput = new FileInputStream(miniLogoCache);
        SystemLogoSingletonDTOBase.uploadMiniLogo(clientRequest, serverInput, fileName);
        serverInput.close();
    } catch (IOException e) {
        logger.error("Error creating image file [" + fileName + "].", e);
        return;
    }
}

From source file:Main.java

public static boolean copyFile(File src, File tar) throws Exception {
    if (src.isFile()) {
        InputStream is = new FileInputStream(src);
        OutputStream op = new FileOutputStream(tar);
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(op);
        byte[] bt = new byte[1024 * 8];
        int len = bis.read(bt);
        while (len != -1) {
            bos.write(bt, 0, len);//from ww w  .java2 s.  co m
            len = bis.read(bt);
        }
        bis.close();
        bos.close();
    }
    if (src.isDirectory()) {
        File[] f = src.listFiles();
        tar.mkdir();
        for (int i = 0; i < f.length; i++) {
            copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile() + File.separator + f[i].getName()));
        }
    }
    return true;
}

From source file:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();/* w ww. ja v  a2 s.  c  o  m*/

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // buffer for writing file
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

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

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}

From source file:com.microsoft.azure.hdinsight.util.HDInsightJobViewUtils.java

private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);/*www  .ja  v a2  s .  c om*/
    }
    bos.close();
}

From source file:Main.java

/**
 * Method write./*from   w  ww. j a  v a 2  s. com*/
 * 
 * @param filepath
 *            String
 * @param theProperties
 *            Properties
 * @param propComments
 *            String
 * @throws IOException
 */
public static void write(String filepath, Properties theProperties, String propComments) throws IOException {
    BufferedOutputStream bos = null;

    try {
        File file = new File(filepath);

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

        theProperties.store(bos, propComments);
    } finally {
        if (bos != null) {
            bos.close();

            bos = null;
        }
    }
}

From source file:io.vertx.config.vault.utils.VaultDownloader.java

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);// w ww . j  ava2  s . com
    }
    bos.close();
}

From source file:org.slc.sli.ingestion.IngestionTest.java

public static File createTestFile(String prefix, String suffix, String fileContents) throws IOException {
    File file = createTempFile(prefix, suffix);
    BufferedOutputStream outputStream = null;

    try {/*from www .  j a  v  a  2s . c o m*/
        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        outputStream.write(fileContents.getBytes());
    } finally {

        if (outputStream != null) {
            outputStream.close();
        }
    }

    return file;
}