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:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java

public static void unzip(String zipname) throws IOException {

    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    //??    GZIPInputStream gzis = new GZIPInputStream(new BufferedInputStream(fis));

    // get directory of the zip file
    if (zipname.contains("\\"))
        zipname = zipname.replace("\\", "/");
    //   String zipDirectory = zipname.substring(0, zipname.lastIndexOf("/")+1) ;
    String zipDirectory = zipname.substring(0, zipname.lastIndexOf("."));
    new File(zipDirectory).mkdir();

    RunData.getInstance().setMetadataDirectory(zipDirectory);

    ZipEntry entry;//from  w ww  . j  a  v  a 2s  . c  o  m
    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());
        if (entry.getName().contains("metadata")) {
            RunData.getInstance().setMetadataFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("schemes")) {
            RunData.getInstance().setSchemesFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("access")) {
            RunData.getInstance().setTableAccessFile(zipDirectory + "/" + entry.getName());
        }

        int size;
        byte[] buffer = new byte[2048];

        FileOutputStream fos = new FileOutputStream(zipDirectory + "/" + entry.getName());
        BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }
    zis.close();
    fis.close();
}

From source file:Main.java

public static void unzip(URL _url, URL _dest, boolean _remove_top) {
    int BUFFER_SZ = 2048;
    File file = new File(_url.getPath());

    String dest = _dest.getPath();
    new File(dest).mkdir();

    try {/*from  w  w  w  . j  av  a2  s .  co m*/
        ZipFile zip = new ZipFile(file);
        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

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

            if (_remove_top)
                currentEntry = currentEntry.substring(currentEntry.indexOf('/'), currentEntry.length());

            File destFile = new File(dest, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

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

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

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER_SZ)) != -1) {
                    dst.write(data, 0, currentByte);
                }
                dst.flush();
                dst.close();
                is.close();
            }
            /*
            if (currentEntry.endsWith(".zip"))
            {
            // found a zip file, try to open
            extractFolder(destFile.getAbsolutePath());
            }*/
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static Bitmap loadBitmap(String url, BitmapFactory.Options opt) {
    Bitmap bitmap = null;//from  ww w. java 2s  .  com
    InputStream is = null;
    BufferedOutputStream bos = null;
    try {
        is = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos, IO_BUFFER_SIZE);
        copyStream(is, bos);
        bos.flush();
        final byte[] data = baos.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "IOException in loadBitmap");
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "OutOfMemoryError in loadBitmap");
        e.printStackTrace();
    } finally {
        closeStream(is);
        closeStream(bos);
    }
    return bitmap;
}

From source file:Main.java

/**
 * Load Bitmap from a url/*from ww w.  j  a va  2s .  c  o m*/
 * @param url   bitmap url to be loaded
 * @return Bitmap object, null if could not be loaded
 */
public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream is = null;
    BufferedOutputStream bos = null;
    try {
        is = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos, IO_BUFFER_SIZE);
        copyStream(is, bos);
        bos.flush();
        final byte[] data = baos.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "IOException in loadBitmap");
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "OutOfMemoryError in loadBitmap");
        e.printStackTrace();
    } finally {
        closeStream(is);
        closeStream(bos);
    }
    return bitmap;
}

From source file:org.eclipse.smila.binarystorage.persistence.efs.EFSUtils.java

/**
 * @param key/* w w  w  .j a  v a2  s. c  om*/
 * @param content
 * @throws BinaryStorageException
 */
public static void writeByteArrayToFile(final String path, final byte[] data) throws BinaryStorageException {
    final IFileStore store = mkdirForFileRecord(path);

    BufferedOutputStream output = null;
    try {
        // Buffering should be applied if desired.
        output = new BufferedOutputStream(store.openOutputStream(EFS.NONE, null));
        output.write(data);
        output.flush();
    } catch (final CoreException exception) {
        throw new BinaryStorageException(exception, "Could not write binary record to :" + path);
    } catch (final IOException exception) {
        throw new BinaryStorageException(exception,
                "IO stream exception. Could not write binary record to :" + path);
    } finally {
        IOUtils.closeQuietly(output);
    }
}

From source file:Main.java

public static void unzip(String strZipFile) {

    try {/*from   w w  w  . j  a  va 2s .c  om*/
        /*
         * STEP 1 : Create directory with the name of the zip file
         * 
         * For e.g. if we are going to extract c:/demo.zip create c:/demo directory where we can extract all the zip entries
         */
        File fSourceZip = new File(strZipFile);
        String zipPath = strZipFile.substring(0, strZipFile.length() - 4);
        File temp = new File(zipPath);
        temp.mkdir();
        System.out.println(zipPath + " created");

        /*
         * STEP 2 : Extract entries while creating required sub-directories
         */
        ZipFile zipFile = new ZipFile(fSourceZip);
        Enumeration<? extends ZipEntry> e = zipFile.entries();

        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            File destinationFilePath = new File(zipPath, entry.getName());

            // create directories if required.
            destinationFilePath.getParentFile().mkdirs();

            // if the entry is directory, leave it. Otherwise extract it.
            if (entry.isDirectory()) {
                continue;
            } else {
                // System.out.println("Extracting " + destinationFilePath);

                /*
                 * Get the InputStream for current entry of the zip file using
                 * 
                 * InputStream getInputStream(Entry entry) method.
                 */
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

                int b;
                byte buffer[] = new byte[1024];

                /*
                 * read the current entry from the zip file, extract it and write the extracted file.
                 */
                FileOutputStream fos = new FileOutputStream(destinationFilePath);
                BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);

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

                // flush the output stream and close it.
                bos.flush();
                bos.close();

                // close the input stream.
                bis.close();
            }

        }
        zipFile.close();
    } catch (IOException ioe) {
        System.out.println("IOError :" + ioe);
    }

}

From source file:Main.java

public static void copyFile(File sourceFile, File targetFile) throws IOException {
    BufferedInputStream inBuff = null;
    BufferedOutputStream outBuff = null;
    try {//  w  ww . j  a va  2  s.c om
        inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
        outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
        byte[] buffer = new byte[BUFFER];
        int length;
        while ((length = inBuff.read(buffer)) != -1) {
            outBuff.write(buffer, 0, length);
        }
        outBuff.flush();
    } finally {
        if (inBuff != null) {
            inBuff.close();
        }
        if (outBuff != null) {
            outBuff.close();
        }
    }
}

From source file:Main.java

/**
 * extracts a zip file to the given dir/*from   ww w . j  a  v  a 2 s  .c o  m*/
 * @param archive
 * @param destDir
 * @throws IOException 
 * @throws ZipException 
 * @throws Exception
 */
public static void extractZipArchive(File archive, File destDir) throws ZipException, IOException {
    if (!destDir.exists()) {
        destDir.mkdir();
    }

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

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destDir, entryFileName)));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
}

From source file:com.manning.androidhacks.hack037.MediaUtils.java

public static void saveRaw(Context context, int raw, String path) {
    File completePath = new File(Environment.getExternalStorageDirectory(), path);

    try {//from   w  ww .j ava2 s . co  m
        completePath.getParentFile().mkdirs();
        completePath.createNewFile();

        BufferedOutputStream bos = new BufferedOutputStream((new FileOutputStream(completePath)));
        BufferedInputStream bis = new BufferedInputStream(context.getResources().openRawResource(raw));
        byte[] buff = new byte[32 * 1024];
        int len;
        while ((len = bis.read(buff)) > 0) {
            bos.write(buff, 0, len);
        }
        bos.flush();
        bos.close();

    } catch (IOException io) {
        Log.e(TAG, "Error: " + io);
    }
}

From source file:org.datavyu.util.NativeLoader.java

private static File copyFileToTmp(final String destName, final URL u) throws Exception {
    System.err.println("Attempting to load: " + u.toString());

    InputStream in = u.openStream();
    File outfile = new File(System.getProperty("java.io.tmpdir"), destName);

    // Create a temporary output location for the library.
    FileOutputStream out = new FileOutputStream(outfile);
    BufferedOutputStream dest = new BufferedOutputStream(out, BUFFER);

    int count;//from w ww. jav a 2s  .c o m
    byte[] data = new byte[BUFFER];
    while ((count = in.read(data, 0, BUFFER)) != -1) {
        dest.write(data, 0, count);
    }

    dest.flush();
    dest.close();
    out.close();
    in.close();

    loadedLibs.add(outfile);
    System.err.println("Temp File:" + outfile);
    System.load(outfile.toString());
    System.err.println("Extracted lib: " + outfile);
    return outfile;
}