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

public static boolean copyFile(File fromFile, File toFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {/*from   w  w w  .j  a  va 2  s .  c o  m*/
        FileInputStream is = new FileInputStream(fromFile);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(toFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:Main.java

public static boolean saveFileFromAssetsToSystem(Context context, String path, File destFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {//from   w  w w . j av a2 s.  c o m
        InputStream is = context.getAssets().open(path);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:org.apache.openjpa.lib.util.Files.java

/**
 * Copy a file. Return false if <code>from</code> does not exist.
 *///from   www.  ja va  2 s.  c  o m
public static boolean copy(File from, File to) throws IOException {
    if (from == null || to == null
            || !(AccessController.doPrivileged(J2DoPrivHelper.existsAction(from))).booleanValue())
        return false;

    FileInputStream in = null;
    FileOutputStream out = null;
    try {
        in = AccessController.doPrivileged(J2DoPrivHelper.newFileInputStreamAction(from));
        BufferedInputStream inbuf = new BufferedInputStream(in);
        out = AccessController.doPrivileged(J2DoPrivHelper.newFileOutputStreamAction(to));
        BufferedOutputStream outbuf = new BufferedOutputStream(out);
        for (int b; (b = inbuf.read()) != -1; outbuf.write(b))
            ;
        outbuf.flush();
        return true;
    } catch (PrivilegedActionException pae) {
        throw (FileNotFoundException) pae.getException();
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (Exception e) {
            }
        if (out != null)
            try {
                out.close();
            } catch (Exception e) {
            }
    }
}

From source file:org.deeplearning4j.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to//from  www.j ava  2s .  c o m
 * @param dest the destination directory
 * @throws IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(dest + File.separator + fileName);

            log.info("file unzip : " + newFile.getAbsoluteFile());

            //createComplex all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(data)) > 0) {
                fos.write(data, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

    }

    else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".gz")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }

    target.delete();

}

From source file:Main.java

public static void copyFile(File in, File out) throws IOException {
    // avoids copying a file to itself
    if (in.equals(out)) {
        return;/*from   w w w  . j a  v a2 s  .c o m*/
    }
    // ensure the output file location exists
    out.getCanonicalFile().getParentFile().mkdirs();

    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(in));
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(out));

    // a temporary buffer to read into
    byte[] tmpBuffer = new byte[8192];
    int len = 0;
    while ((len = fis.read(tmpBuffer)) != -1) {
        // add the temp data to the output
        fos.write(tmpBuffer, 0, len);
    }
    // close the input stream
    fis.close();
    // close the output stream
    fos.flush();
    fos.close();
}

From source file:Main.java

public static boolean copyFile(final File srcFile, final File saveFile) {
    File parentFile = saveFile.getParentFile();
    if (!parentFile.exists()) {
        if (!parentFile.mkdirs())
            return false;
    }//from  www  . ja  v  a2  s. co m

    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(srcFile));
        outputStream = new BufferedOutputStream(new FileOutputStream(saveFile));
        byte[] buffer = new byte[1024 * 4];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        close(inputStream, outputStream);
    }
    return true;
}

From source file:com.thruzero.common.core.utils.FileUtilsExt.java

public static final boolean unzipArchive(final File fromFile, final File toDir) throws IOException {
    boolean result = false; // assumes error

    logHelper.logBeginUnzip(fromFile, toDir);

    ZipFile zipFile = new ZipFile(fromFile);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    if (!toDir.exists()) {
        toDir.mkdirs();//from w  w  w .  j a  v a  2s  . c  om
        logHelper.logProgressCreatedToDir(toDir);
    }
    logHelper.logProgressToDirIsWritable(toDir);

    if (toDir.canWrite()) {
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();

            if (entry.isDirectory()) {
                File dir = new File(
                        toDir.getAbsolutePath() + EnvironmentHelper.FILE_PATH_SEPARATOR + entry.getName());

                if (!dir.exists()) {
                    dir.mkdirs();
                    logHelper.logProgressFilePathCreated(dir);
                }
            } else {
                File fosz = new File(
                        toDir.getAbsolutePath() + EnvironmentHelper.FILE_PATH_SEPARATOR + entry.getName());
                logHelper.logProgressCopyEntry(fosz);

                File parent = fosz.getParentFile();
                if (!parent.exists()) {
                    parent.mkdirs();
                }

                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fosz));
                IOUtils.copy(zipFile.getInputStream(entry), bos);
                bos.flush();
            }
        }
        zipFile.close();
        result = true; // success
    } else {
        logHelper.logFileWriteError(fromFile, null);
        zipFile.close();
    }

    return result;
}

From source file:com.pieframework.runtime.utils.Zipper.java

public static void unzip(String zipFile, String toDir) throws ZipException, IOException {

    int BUFFER = 2048;
    File file = new File(zipFile);

    ZipFile zip = new ZipFile(file);
    String newPath = toDir;//from  w ww.ja v a2  s. co  m

    File toDirectory = new File(newPath);
    if (!toDirectory.exists()) {
        toDirectory.mkdir();
    }
    Enumeration zipFileEntries = zip.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

        String currentEntry = entry.getName();
        File destFile = new File(newPath, FilenameUtils.separatorsToSystem(currentEntry));
        //System.out.println(currentEntry);
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    zip.close();

}

From source file:Main.java

public static void copyFile(String sourcePath, String toPath) {
    File sourceFile = new File(sourcePath);
    File targetFile = new File(toPath);
    createDipPath(toPath);/*from   ww w  . j  av  a 2s  . c  o  m*/
    try {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            outBuff.flush();
        } finally {
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:prince.app.sphotos.Gallery.UriTexture.java

public static void writeToCache(long crc64, Bitmap bitmap, int maxResolution) {
    String file = createFilePathFromCrc64(crc64, maxResolution);
    if (bitmap != null && file != null && crc64 != 0) {
        try {/*w  w  w .  j  ava 2 s  . co m*/
            File fileC = new File(file);
            fileC.createNewFile();
            final FileOutputStream fos = new FileOutputStream(fileC);
            final BufferedOutputStream bos = new BufferedOutputStream(fos, 16384);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
            bos.flush();
            bos.close();
            fos.close();
            Log.i(TAG, "written to cache at: " + fileC.getAbsolutePath());
        } catch (Exception e) {

        }
    }
}