Example usage for java.io BufferedOutputStream flush

List of usage examples for java.io BufferedOutputStream flush

Introduction

In this page you can find the example usage for java.io BufferedOutputStream flush.

Prototype

@Override
public synchronized void flush() throws IOException 

Source Link

Document

Flushes this buffered output stream.

Usage

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);//from w  ww  .  j  a v a 2s  .  co  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:Main.java

public static boolean OutPutImage(File file, Bitmap bitmap) {
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();//from   w  w  w  .j av a2 s . c  o m
    }
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        if (bos != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            fos.flush();
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        return false;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

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();/*w  w w .ja  v a 2  s .  c o m*/
    }
    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: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);//from w ww. j  a  v  a 2 s. c  om
    writer.flush();
    writer.close();

}

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  2s  .c  o  m
    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.oneops.cms.crypto.CmsCryptoDES.java

/**
 * Generate des key./*from w  ww  .  j a v a2 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  w w w. j  av a 2  s . c o m
 * @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:edu.caltech.ipac.firefly.server.network.HttpServices.java

static void readBody(OutputStream os, InputStream body) {
    BufferedInputStream bis = new BufferedInputStream(body);
    BufferedOutputStream bos = new BufferedOutputStream(os);
    try {// w w  w.jav  a 2s. c om
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }

        bos.flush();

    } catch (IOException e) {
        LOG.error(e, "Error while reading response body");
    }
}

From source file:com.petpet.c3po.gatherer.FileExtractor.java

/**
 * Unpack data from the stream to specified directory.
 * /*from   w  w w.  j a  v  a 2 s  .  c o  m*/
 * @param in
 *          stream with tar data
 * @param outputDir
 *          destination directory
 * @return true in case of success, otherwise - false
 */
private static void extract(ArchiveInputStream in, File outputDir) {
    try {
        ArchiveEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            // replace : for windows OS.
            final File file = new File(outputDir, entry.getName().replaceAll(":", "_"));

            if (entry.isDirectory()) {

                if (!file.exists()) {
                    file.mkdirs();
                }

            } else {
                file.getParentFile().mkdirs();
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);

                try {

                    IOUtils.copy(in, out);
                    out.flush();

                } finally {
                    try {
                        out.close();

                    } catch (IOException e) {
                        LOG.debug("An error occurred while closing the output stream for file '{}'. Error: {}",
                                file, e.getMessage());
                    }
                }
            }
        }

    } catch (IOException e) {
        LOG.debug("An error occurred while handling archive file. Error: {}", e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                LOG.debug("An error occurred while closing the archive stream . Error: {}", e.getMessage());
            }
        }
    }
}

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

/**
 * Uncompress a zip to files//from  ww w  .ja v  a  2s .  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;
}