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:com.elastica.helper.FileUtility.java

public static void extractJar(final String storeLocation, final Class<?> clz) throws IOException {
    File firefoxProfile = new File(storeLocation);
    String location = clz.getProtectionDomain().getCodeSource().getLocation().getFile();

    JarFile jar = new JarFile(location);
    System.out.println("Extracting jar file::: " + location);
    firefoxProfile.mkdir();/*from w w w  . ja v a2  s.c om*/

    Enumeration<?> jarFiles = jar.entries();
    while (jarFiles.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) jarFiles.nextElement();
        String currentEntry = entry.getName();
        File destinationFile = new File(storeLocation, currentEntry);
        File destinationParent = destinationFile.getParentFile();

        // create the parent directory structure if required
        destinationParent.mkdirs();
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(jar.getInputStream(entry));
            int currentByte;

            // buffer for writing file
            byte[] data = new byte[BUFFER];

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

            // read and write till last byte
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                destination.write(data, 0, currentByte);
            }

            destination.flush();
            destination.close();
            is.close();
        }
    }

    FileUtils.deleteDirectory(new File(storeLocation + "\\META-INF"));
    if (OSUtility.isWindows()) {
        new File(storeLocation + "\\" + clz.getCanonicalName().replaceAll("\\.", "\\\\") + ".class").delete();
    } else {
        new File(storeLocation + "/" + clz.getCanonicalName().replaceAll("\\.", "/") + ".class").delete();
    }
}

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

/**
 * <p><em>Title: Save InputSream to an temporary File</em></p>
 * <p>Description: </p>// w  ww .j a v a 2 s  .  co  m
 * 
 * @return 
 */
public static void saveInputStreamToTempFile(InputStream is, String fileName) {

    File outputFile = new File(Configuration.getTempDirPath() + fileName);
    BufferedInputStream bis = new BufferedInputStream(is);
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFile);
        bos = new BufferedOutputStream(fos);
        int i = -1;
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
        bos.flush();
    } catch (Exception e) {
        log.error(e);
    } 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);
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }

    }

}

From source file:com.dnielfe.manager.utils.SimpleUtils.java

/**
 * //from   ww  w  .  j  a  v a2s .  co m
 * @param old
 *            the file to be copied/ moved
 * @param newDir
 *            the directory to copy/move the file to
 */
public static void copyToDirectory(String old, String newDir) {
    File old_file = new File(old);
    File temp_dir = new File(newDir);
    byte[] data = new byte[BUFFER];
    int read = 0;

    if (old_file.isFile() && temp_dir.isDirectory() && temp_dir.canWrite()) {
        String file_name = old.substring(old.lastIndexOf("/"), old.length());
        File cp_file = new File(newDir + file_name);

        try {
            BufferedOutputStream o_stream = new BufferedOutputStream(new FileOutputStream(cp_file));
            BufferedInputStream i_stream = new BufferedInputStream(new FileInputStream(old_file));

            while ((read = i_stream.read(data, 0, BUFFER)) != -1)
                o_stream.write(data, 0, read);

            o_stream.flush();
            i_stream.close();
            o_stream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    } else if (old_file.isDirectory() && temp_dir.isDirectory() && temp_dir.canWrite()) {
        String files[] = old_file.list();
        String dir = newDir + old.substring(old.lastIndexOf("/"), old.length());
        int len = files.length;

        if (!new File(dir).mkdir())
            return;

        for (int i = 0; i < len; i++)
            copyToDirectory(old + "/" + files[i], dir);

    } else if (old_file.isFile() && !temp_dir.canWrite() && SimpleExplorer.rootAccess) {
        RootCommands.moveCopyRoot(old, newDir);
    } else if (!temp_dir.canWrite())
        return;

    return;
}

From source file:it.greenvulcano.util.bin.BinaryUtils.java

/**
 * Write the content of a <code>byte</code> array into a file on the local
 * filesystem./*from  w  ww.  j a va 2  s.  co m*/
 * 
 * @param contentArray
 *        the <code>byte</code> array to be written to file
 * @param file
 *        the file to be written to
 * @throws IOException
 *         if any I/O error occurs
 */
public static void writeBytesToFile(byte[] contentArray, File file, boolean append) throws IOException {
    BufferedOutputStream bufOut = null;
    try {
        bufOut = new BufferedOutputStream(new FileOutputStream(file, append), 10240);
        IOUtils.write(contentArray, bufOut);
        bufOut.flush();
    } finally {
        if (bufOut != null) {
            bufOut.close();
        }
    }
}

From source file:com.blazemeter.bamboo.plugin.ServiceManager.java

public static void unzip(String srcZipFileName, String destDirectoryName, BuildLogger logger) {
    try {//from   w  ww.  ja v  a2  s .  c o  m
        BufferedInputStream bufIS = null;
        // create the destination directory structure (if needed)
        File destDirectory = new File(destDirectoryName);
        destDirectory.mkdirs();

        // open archive for reading
        File file = new File(srcZipFileName);
        ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);

        //for every zip archive entry do
        Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            logger.addBuildLogEntry("\tExtracting jtl report: " + entry);

            //create destination file
            File destFile = new File(destDirectory, entry.getName());

            //create parent directories if needed
            File parentDestFile = destFile.getParentFile();
            parentDestFile.mkdirs();

            bufIS = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;

            // buffer for writing file
            byte data[] = new byte[BUFFER_SIZE];

            // write the current file to disk
            FileOutputStream fOS = new FileOutputStream(destFile);
            BufferedOutputStream bufOS = new BufferedOutputStream(fOS, BUFFER_SIZE);

            while ((currentByte = bufIS.read(data, 0, BUFFER_SIZE)) != -1) {
                bufOS.write(data, 0, currentByte);
            }

            // close BufferedOutputStream
            bufOS.flush();
            bufOS.close();
        }
        bufIS.close();
    } catch (Exception e) {
        logger.addErrorLogEntry("Failed to unzip report: check that it is downloaded");
    }
}

From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java

/**
 * Extracts the jarEntry from the jarFile to the target directory.
 *
 * @param jarFile/*from ww  w .ja  v  a 2  s.  c  om*/
 * @param jarEntry
 * @param targetDir
 * @return true if extraction was successful, false otherwise
 */
public static boolean extractJarEntry(JarFile jarFile, JarEntry jarEntry, String targetDir) {
    boolean extractSuccessful = false;
    File file = new File(targetDir);
    if (!file.exists()) {
        file.mkdir();
    }
    if (jarEntry != null) {
        InputStream inputStream = null;
        try {
            inputStream = jarFile.getInputStream(jarEntry);
            file = new File(targetDir + jarEntry.getName());
            if (jarEntry.isDirectory()) {
                file.mkdir();
            } else {
                int size;
                byte[] buffer = new byte[2048];

                FileOutputStream fileOutputStream = new FileOutputStream(file);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream,
                        buffer.length);

                try {
                    while ((size = inputStream.read(buffer, 0, buffer.length)) != -1) {
                        bufferedOutputStream.write(buffer, 0, size);
                    }
                    bufferedOutputStream.flush();
                } finally {
                    bufferedOutputStream.close();
                }
            }
            extractSuccessful = true;
        } catch (Exception e) {
            log.warn(e);
        } finally {
            close(inputStream);
        }
    }
    return extractSuccessful;
}

From source file:de.suse.swamp.util.FileUtils.java

/**
 * Decompress the provided Stream to "targetPath"
 *//*from  w  ww.  ja v  a 2s  .com*/
public static void uncompress(InputStream inputFile, String targetPath) throws Exception {
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(inputFile));
    BufferedOutputStream dest = null;
    ZipEntry entry;
    while ((entry = zin.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[2048];
        // write the files to the disk
        if (entry.isDirectory()) {
            org.apache.commons.io.FileUtils.forceMkdir(new File(targetPath + "/" + entry.getName()));
        } else {
            FileOutputStream fos = new FileOutputStream(targetPath + "/" + entry.getName());
            dest = new BufferedOutputStream(fos, 2048);
            while ((count = zin.read(data, 0, 2048)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
    }
}

From source file:org.apache.asterix.event.service.AsterixEventServiceUtil.java

public static void unzip(String sourceFile, String destDir) throws IOException {
    final FileInputStream fis = new FileInputStream(sourceFile);
    final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    final File destDirFile = new File(destDir);
    final byte[] data = new byte[BUFFER_SIZE];

    ZipEntry entry;/*from www. j a va2 s .  c o m*/
    Set<String> visitedDirs = new HashSet<>();
    createDir(destDir);
    while ((entry = zis.getNextEntry()) != null) {
        createDir(destDirFile, entry, visitedDirs);
        if (entry.isDirectory()) {
            continue;
        }
        int count;

        // write the file to the disk
        File dst = new File(destDir, entry.getName());
        FileOutputStream fos = new FileOutputStream(dst);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        // close the output streams
        dest.flush();
        dest.close();
    }

    zis.close();
}

From source file:net.duckling.ddl.util.FileUtil.java

/**
 * Brief Intro Here/*w  w  w  .ja v a 2  s  .  c  o m*/
 * ?
 * @param
 */
public static void copyFile(File sourceFile, File targetFile) throws IOException {
    // ?
    FileInputStream input = new FileInputStream(sourceFile);
    BufferedInputStream inBuff = new BufferedInputStream(input);

    // ?
    FileOutputStream output = new FileOutputStream(targetFile);
    BufferedOutputStream outBuff = new BufferedOutputStream(output);

    // 
    byte[] b = new byte[1024 * 5];
    int len;
    while ((len = inBuff.read(b)) != -1) {
        outBuff.write(b, 0, len);
    }
    // ?
    outBuff.flush();

    // ?
    inBuff.close();
    outBuff.close();
    output.close();
    input.close();
}

From source file:com.ibm.amc.FileManager.java

public static File decompress(URI temporaryFileUri) {
    final File destination = new File(getUploadDirectory(), temporaryFileUri.getSchemeSpecificPart());
    if (!destination.mkdirs()) {
        throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR, "CWZBA2001E_DIRECTORY_CREATION_FAILED",
                destination.getPath());/*from w w  w  .  j  av  a 2s . c  o  m*/
    }

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(getFileForUri(temporaryFileUri));

        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File newDirOrFile = new File(destination, entry.getName());
            if (newDirOrFile.getParentFile() != null && !newDirOrFile.getParentFile().exists()) {
                if (!newDirOrFile.getParentFile().mkdirs()) {
                    throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR,
                            "CWZBA2001E_DIRECTORY_CREATION_FAILED", newDirOrFile.getParentFile().getPath());
                }
            }
            if (entry.isDirectory()) {
                if (!newDirOrFile.mkdir()) {
                    throw new AmcRuntimeException(Status.INTERNAL_SERVER_ERROR,
                            "CWZBA2001E_DIRECTORY_CREATION_FAILED", newDirOrFile.getPath());
                }
            } else {
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                int size;
                byte[] buffer = new byte[ZIP_BUFFER];
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newDirOrFile),
                        ZIP_BUFFER);
                while ((size = bis.read(buffer, 0, ZIP_BUFFER)) != -1) {
                    bos.write(buffer, 0, size);
                }
                bos.flush();
                bos.close();
                bis.close();
            }
        }
    } catch (Exception e) {
        throw new AmcRuntimeException(e);
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                logger.debug("decompress", "close failed with " + e);
            }
        }
    }

    return destination;
}