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.glaf.core.entity.mybatis.MyBatisSessionFactory.java

public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream) {
    Map<String, byte[]> zipMap = new java.util.HashMap<String, byte[]>();
    java.util.zip.ZipEntry zipEntry = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    byte tmpByte[] = null;
    try {/*from  ww w .ja v  a 2 s  . c om*/
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String name = zipEntry.getName();
            if (StringUtils.endsWith(name, "Mapper.xml")) {
                tmpByte = new byte[BUFFER];
                baos = new ByteArrayOutputStream();
                bos = new BufferedOutputStream(baos, BUFFER);
                int i = 0;
                while ((i = zipInputStream.read(tmpByte, 0, BUFFER)) != -1) {
                    bos.write(tmpByte, 0, i);
                }
                bos.flush();
                byte[] bytes = baos.toByteArray();
                IOUtils.closeStream(baos);
                IOUtils.closeStream(baos);
                zipMap.put(zipEntry.getName(), bytes);
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(baos);
        IOUtils.closeStream(baos);
    }
    return zipMap;
}

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

/**
 * <p><em>Title: Create a temporary File from a file identified by URL</em></p>
 * <p>Description: Method creates a temporary file from a remote file addressed 
 * an by URL representing the orginal PDF, that should be converted</p>
 * //from w w w.  ja  v a2s .  c om
 * @param fileName
 * @param url
 * @return 
 */
public static String saveUrlToFile(String fileName, String url) {
    File inputFile = new File(Configuration.getTempDirPath() + "/" + fileName);
    log.debug(inputFile.getAbsolutePath());
    InputStream is = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;

    log.info(url);
    try {
        URL inputDocument = new URL(url);
        is = inputDocument.openStream();
        bis = new BufferedInputStream(is);

        fos = new FileOutputStream(inputFile);
        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);
            }
        }
    }
    return inputFile.getName();
}

From source file:com.twemyeez.picklr.InstallManager.java

public static void unzip() {

    // Firstly get the working directory
    String workingDirectory = Minecraft.getMinecraft().mcDataDir.getAbsolutePath();

    // If it ends with a . then remove it
    if (workingDirectory.endsWith(".")) {
        workingDirectory = workingDirectory.substring(0, workingDirectory.length() - 1);
    }/* w  ww .j a  v a 2s .  c  o  m*/

    // If it doesn't end with a / then add it
    if (!workingDirectory.endsWith("/") && !workingDirectory.endsWith("\\")) {
        workingDirectory = workingDirectory + "/";
    }

    // Use a test file to see if libraries installed
    File file = new File(workingDirectory + "mods/mp3spi1.9.5.jar");

    // If the libraries are installed, return
    if (file.exists()) {
        System.out.println("Checking " + file.getAbsolutePath());
        System.out.println("Target file exists, so not downloading API");
        return;
    }

    // Now try to download the libraries
    try {

        String location = "http://www.javazoom.net/mp3spi/sources/mp3spi1.9.5.zip";

        // Define the URL
        URL url = new URL(location);

        // Get the ZipInputStream
        ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream((url).openStream()));

        // Use a temporary ZipEntry as a buffer
        ZipEntry zipFile;

        // While there are more file entries
        while ((zipFile = zipInput.getNextEntry()) != null) {
            // Check if it is one of the file names that we want to copy
            Boolean required = false;
            if (zipFile.getName().indexOf("mp3spi1.9.5.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("jl1.0.1.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("tritonus_share.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("LICENSE.txt") != -1) {
                required = true;
            }

            // If it is, then we shall now copy it
            if (!zipFile.getName().replace("MpegAudioSPI1.9.5/", "").equals("") && required) {

                // Get the file location
                String tempFile = new File(zipFile.getName()).getName();

                tempFile = tempFile.replace("LICENSE.txt", "MpegAudioLicence.txt");

                // Initialise the target file
                File targetFile = (new File(
                        workingDirectory + "mods/" + tempFile.replace("MpegAudioSPI1.9.5/", "")));

                // Print a debug/alert message
                System.out.println("Picklr is extracting to " + workingDirectory + "mods/"
                        + tempFile.replace("MpegAudioSPI1.9.5/", ""));

                // Make parent directories if required
                targetFile.getParentFile().mkdirs();

                // If the file does not exist, create it
                if (!targetFile.exists()) {
                    targetFile.createNewFile();
                }

                // Create a buffered output stream to the destination
                BufferedOutputStream destinationOutput = new BufferedOutputStream(
                        new FileOutputStream(targetFile, false), 2048);

                // Store the data read
                int bytesRead;

                // Data buffer
                byte dataBuffer[] = new byte[2048];

                // While there is still data to write
                while ((bytesRead = zipInput.read(dataBuffer, 0, 2048)) != -1) {
                    // Write it to the output stream
                    destinationOutput.write(dataBuffer, 0, bytesRead);
                }

                // Flush the output
                destinationOutput.flush();

                // Close the output stream
                destinationOutput.close();
            }

        }
        // Close the zip input
        zipInput.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.sec.ose.osi.util.tools.FileOperator.java

public static void copyFile(File pSource, File pDest) throws FileNotFoundException {

    if (pSource == null)
        throw new FileNotFoundException("Source file is not found");

    if (pSource.exists() == false)
        throw new FileNotFoundException(pSource.getPath());

    if (pDest == null)
        throw new FileNotFoundException("Report file is not found");

    BufferedInputStream bi = null;
    BufferedOutputStream bo = null;

    try {//from   w w  w.  java2  s.  c  om
        bi = new BufferedInputStream(new FileInputStream(pSource)); // FileNotFoundExeption
        bo = new BufferedOutputStream(new FileOutputStream(pDest));

        byte buffer[] = new byte[BUF_SIZE];

        int readByte = 0;

        while ((readByte = bi.read(buffer)) != -1) { // IOException
            bo.write(buffer, 0, readByte);

        }

    } catch (FileNotFoundException e) {
        log.warn(e);
        throw e;
    } catch (IOException e) {
        log.warn(e);
    } finally {

        if (bo != null) {
            try {
                bo.flush();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.warn(e);
            }
            try {
                bo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.warn(e);
            }

            bo = null;
        }
        if (bi != null) {
            try {
                bi.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.warn(e);
            }
            bi = null;
        }
    }

}

From source file:org.docx4j.openpackaging.io.LoadFromZipNG.java

public static byte[] getBytesFromInputStream(InputStream is) throws Exception {

    BufferedInputStream bufIn = new BufferedInputStream(is);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);
    int c = bufIn.read();
    while (c != -1) {
        bos.write(c);/*from   w ww .  j  av a 2 s . c om*/
        c = bufIn.read();
    }
    bos.flush();
    baos.flush();
    //bufIn.close(); //don't do that, since it closes the ZipInputStream after we've read an entry!
    bos.close();
    return baos.toByteArray();
}

From source file:Main.java

/**
 * save the bitmap to local,add give a white bg color
 * @param bitmap/* ww  w. j  a v a2  s  .c o m*/
 * @param path
 * @return
 */
public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) {
    BufferedOutputStream bos = null;
    try {
        File file = new File(path);
        if (file.exists())
            file.delete();
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int w_new = w;
        int h_new = h;
        Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888);
        //            Paint paint = new Paint();
        //            paint.setColor(Color.WHITE);
        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null);
        resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        resultBitmap.recycle();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:de.uzk.hki.da.sb.restfulService.FileUtil.java

/**
 * <p><em>Title: Create a temporary File from a file identified by URL</em></p>
 * <p>Description: Method creates a temporary file from a remote file addressed 
 * an by URL representing the orginal PDF, that should be converted</p>
 * /* www. j  a v a  2s .c  o m*/
 * @param fileName
 * @param url
 * @return 
 */
public static String saveUrlToFile(String fileName, String url) {

    String path = fileName.substring(0, fileName.lastIndexOf("/"));
    File dir = new File(Configuration.getTempDirPath() + "/" + path);
    dir.mkdirs();

    File inputFile = new File(Configuration.getTempDirPath() + "/" + fileName);
    InputStream is = null;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;

    log.info(url);
    try {
        URL inputDocument = new URL(url);
        is = inputDocument.openStream();
        bis = new BufferedInputStream(is);

        fos = new FileOutputStream(inputFile);
        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);
            }
        }
    }

    return inputFile.getAbsolutePath();
}

From source file:com.headswilllol.basiclauncher.Launcher.java

public static void unzip(ZipFile zip, ZipEntry entry, File dest) { // convenience method for unzipping from archive
    if (dest.exists())
        dest.delete();//from  w ww .j ava  2s  .c  om
    dest.getParentFile().mkdirs();
    try {
        BufferedInputStream bIs = new BufferedInputStream(zip.getInputStream(entry));
        int b;
        byte buffer[] = new byte[1024];
        FileOutputStream fOs = new FileOutputStream(dest);
        BufferedOutputStream bOs = new BufferedOutputStream(fOs, 1024);
        while ((b = bIs.read(buffer, 0, 1024)) != -1)
            bOs.write(buffer, 0, b);
        bOs.flush();
        bOs.close();
        bIs.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        createExceptionLog(ex);
        progress = "Failed to unzip " + entry.getName();
        fail = "Errors occurred; see log file for details";
        launcher.paintImmediately(0, 0, width, height);
    }
}

From source file:com.eviware.soapui.integration.exporter.ProjectExporter.java

public static void unpackageAll(String archive, String path) {
    try {//from   w  w  w. ja  v a 2  s .  c  o m
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(archive);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(path + File.separator + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch (Exception e) {
        SoapUI.logError(e);
    }
}

From source file:org.bigmouth.nvwa.network.http.HttpClientHelper.java

public static File getResponseBodyAsFile(HttpResponse httpResponse, String filePath)
        throws IllegalStateException, IOException {
    if (null == httpResponse) {
        return null;
    }// ww  w. ja  v a2 s.com
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    InputStream is = null;
    File file = null;
    try {
        is = httpResponse.getEntity().getContent();
        bis = new BufferedInputStream(is);
        file = new File(filePath);
        bos = new BufferedOutputStream(new FileOutputStream(file));
        byte[] bytes = new byte[2048];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.flush();
    } finally {
        IOUtils.closeQuietly(bos);
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(is);
    }
    return file;
}