Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

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

Prototype

@Override
public synchronized void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Usage

From source file:net.sf.jvifm.ui.ZipLister.java

private File extractToTemp(FileObject fileObject) {
    String basename = fileObject.getName().getBaseName();
    File tempFile = null;//from  ww w  . ja v  a  2 s.  co  m
    try {
        String tmpPath = System.getProperty("java.io.tmpdir");
        tempFile = new File(FilenameUtils.concat(tmpPath, basename));

        byte[] buf = new byte[4096];
        BufferedInputStream bin = new BufferedInputStream(fileObject.getContent().getInputStream());
        BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(tempFile));
        while (bin.read(buf, 0, 1) != -1) {
            bout.write(buf, 0, 1);
        }
        bout.close();
        bin.close(); // by barney
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return tempFile;
}

From source file:edu.stanford.epad.common.util.EPADFileUtils.java

/**
 * Unzip the specified file./*w ww. j av a  2s.  c  om*/
 * 
 * @param zipFilePath String path to zip file.
 * @throws IOException during zip or read process.
 */
public static void extractFolder(String zipFilePath) throws IOException {
    ZipFile zipFile = null;

    try {
        int BUFFER = 2048;
        File file = new File(zipFilePath);

        zipFile = new ZipFile(file);
        String newPath = zipFilePath.substring(0, zipFilePath.length() - 4);

        makeDirs(new File(newPath));
        Enumeration<?> zipFileEntries = zipFile.entries();

        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            File destFile = new File(newPath, currentEntry);
            File destinationParent = destFile.getParentFile();

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

            InputStream is = null;
            BufferedInputStream bis = null;
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;

            try {
                if (destFile.exists() && destFile.isDirectory())
                    continue;

                if (!entry.isDirectory()) {
                    int currentByte;
                    byte data[] = new byte[BUFFER];
                    is = zipFile.getInputStream(entry);
                    bis = new BufferedInputStream(is);
                    fos = new FileOutputStream(destFile);
                    bos = new BufferedOutputStream(fos, BUFFER);

                    while ((currentByte = bis.read(data, 0, BUFFER)) != -1) {
                        bos.write(data, 0, currentByte);
                    }
                    bos.flush();
                }
            } finally {
                IOUtils.closeQuietly(bis);
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(bos);
                IOUtils.closeQuietly(fos);
            }

            if (currentEntry.endsWith(".zip")) {
                extractFolder(destFile.getAbsolutePath());
            }
        }
    } catch (Exception e) {
        log.warning("Failed to unzip: " + zipFilePath, e);
        throw new IllegalStateException(e);
    } finally {
        if (zipFile != null)
            zipFile.close();
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

protected File unzip3(File zf) throws FileNotFoundException {
    //File f = null;
    String rename = zf.getAbsolutePath().replaceFirst("\\.zip", identifier + ".xml");//.replaceFirst("\\.gz", ".xml");
    File f = new File(rename);
    try {//  w w  w.  j av a 2 s .c  om
        FileInputStream fis = new FileInputStream(zf);

        ZipInputStream zin = new ZipInputStream(fis);
        ZipEntry ze;

        final byte[] content = new byte[BUFFER];

        while ((ze = zin.getNextEntry()) != null) {
            f = new File(getCalTempPath() + File.separator + ze.getName());
            FileOutputStream fos = new FileOutputStream(f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, content.length);

            int n = 0;
            while (-1 != (n = zin.read(content))) {
                bos.write(content, 0, n);
            }
            bos.flush();
            bos.close();
        }

        fis.close();
        zin.close();
    } catch (IOException ioe) {
        jlog.error("Error processing Zip " + zf + " Excluding! :: " + ioe);
        return null;
    }
    //try again... what could go wrong
    /*
    if (checkMinFileSize(f) && retry_counter<MAX_UNGZIP_RETRIES){
       retry_counter++;
       f.delete();
       f = unzip2(zf);
    }
    */
    return f;
}

From source file:com.nabla.dc.server.ImageService.java

private boolean exportImage(final String imageId, final HttpServletResponse response)
        throws IOException, SQLException, InternalErrorException {
    final Connection conn = db.getConnection();
    try {//from  ww w  . j av  a 2  s  . c  o m
        final PreparedStatement stmt = StatementFormat.prepare(conn, "SELECT * FROM image WHERE id=?;",
                imageId);
        try {
            final ResultSet rs = stmt.executeQuery();
            try {
                if (!rs.next()) {
                    if (log.isDebugEnabled())
                        log.debug("failed to find report ID= " + imageId);
                    return false;
                }
                if (log.isTraceEnabled())
                    log.trace("exporting image " + imageId);
                response.reset();
                response.setBufferSize(DEFAULT_BUFFER_SIZE);
                response.setContentType(rs.getString("content_type"));
                response.setHeader("Content-Length", String.valueOf(rs.getInt("length")));
                response.setHeader("Content-Disposition",
                        MessageFormat.format("inline; filename=\"{0}\"", rs.getString("name")));
                // to prevent images to be downloaded every time user hovers one image
                final Calendar cal = Calendar.getInstance();
                cal.setTime(new Date());
                cal.add(Calendar.MONTH, 2);
                response.setDateHeader("Expires", cal.getTime().getTime());
                final BufferedInputStream input = new BufferedInputStream(rs.getBinaryStream("content"),
                        DEFAULT_BUFFER_SIZE);
                try {
                    final BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream(),
                            DEFAULT_BUFFER_SIZE);
                    try {
                        final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
                        int length;
                        while ((length = input.read(buffer)) > 0)
                            output.write(buffer, 0, length);
                    } finally {
                        output.close();
                    }
                } finally {
                    input.close();
                }
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
    } finally {
        conn.close();
    }
    return true;
}

From source file:net.sf.sveditor.core.tests.utils.BundleUtils.java

public void unpackBundleZipToFS(String bundle_path, File fs_path) {
    URL zip_url = fBundle.getEntry(bundle_path);
    TestCase.assertNotNull(zip_url);//from ww w. j a  v a  2  s .  c  om

    if (!fs_path.isDirectory()) {
        TestCase.assertTrue(fs_path.mkdirs());
    }

    try {
        InputStream in = zip_url.openStream();
        TestCase.assertNotNull(in);
        byte tmp[] = new byte[4 * 1024];
        int cnt;

        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;

        while ((ze = zin.getNextEntry()) != null) {
            // System.out.println("Entry: \"" + ze.getName() + "\"");
            File entry_f = new File(fs_path, ze.getName());
            if (ze.getName().endsWith("/")) {
                // Directory
                continue;
            }
            if (!entry_f.getParentFile().exists()) {
                TestCase.assertTrue(entry_f.getParentFile().mkdirs());
            }
            FileOutputStream fos = new FileOutputStream(entry_f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, tmp.length);

            while ((cnt = zin.read(tmp, 0, tmp.length)) > 0) {
                bos.write(tmp, 0, cnt);
            }
            bos.flush();
            bos.close();
            fos.close();

            zin.closeEntry();
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
        TestCase.fail("Failed to unpack zip file: " + e.getMessage());
    }
}

From source file:net.sf.sveditor.core.tests.utils.BundleUtils.java

public void unpackBundleTarToFS(String bundle_path, File fs_path) {
    URL url = fBundle.getEntry(bundle_path);
    TestCase.assertNotNull(url);//from   w w  w.ja  v a  2s.c  o  m

    if (!fs_path.isDirectory()) {
        TestCase.assertTrue(fs_path.mkdirs());
    }
    InputStream in = null;
    TarArchiveInputStream tar_stream = null;

    try {
        in = url.openStream();
    } catch (IOException e) {
        TestCase.fail("Failed to open data file " + bundle_path + " : " + e.getMessage());
    }

    tar_stream = new TarArchiveInputStream(in);

    try {
        byte tmp[] = new byte[4 * 1024];
        int cnt;

        ArchiveEntry te;

        while ((te = tar_stream.getNextEntry()) != null) {
            // System.out.println("Entry: \"" + ze.getName() + "\"");
            File entry_f = new File(fs_path, te.getName());
            if (te.getName().endsWith("/")) {
                // Directory
                continue;
            }
            if (!entry_f.getParentFile().exists()) {
                TestCase.assertTrue(entry_f.getParentFile().mkdirs());
            }
            FileOutputStream fos = new FileOutputStream(entry_f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, tmp.length);

            while ((cnt = tar_stream.read(tmp, 0, tmp.length)) > 0) {
                bos.write(tmp, 0, cnt);
            }
            bos.flush();
            bos.close();
            fos.close();

            //            tar_stream.closeEntry();
        }
        tar_stream.close();
    } catch (IOException e) {
        e.printStackTrace();
        TestCase.fail("Failed to unpack tar file: " + e.getMessage());
    }
}

From source file:de.uni_potsdam.hpi.bpt.promnicat.importer.bpmai.BpmaiImporter.java

/**
 * Reads the given content and writes it into a  file with the given path.
 * @param in stream to read//from  w  w w  .jav a 2s  .com
 * @param targetPath path to write the read content to
 * @throws IOException if the specified path does not exists.
 */
private void copyInputStream(InputStream in, String targetPath) throws IOException {
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetPath));
    byte[] buffer = new byte[1024];
    int len;
    while ((len = in.read(buffer)) >= 0) {
        bufferedOutputStream.write(buffer, 0, len);
    }
    in.close();
    bufferedOutputStream.close();
}

From source file:net.sf.sveditor.core.tests.utils.BundleUtils.java

public void unpackBundleTgzToFS(String bundle_path, File fs_path) {
    URL url = fBundle.getEntry(bundle_path);
    TestCase.assertNotNull(url);/* w w  w.  j  av a  2  s  .c o  m*/

    if (!fs_path.isDirectory()) {
        TestCase.assertTrue(fs_path.mkdirs());
    }
    InputStream in = null;
    GzipCompressorInputStream gz_stream = null;
    TarArchiveInputStream tar_stream = null;

    try {
        in = url.openStream();
    } catch (IOException e) {
        TestCase.fail("Failed to open data file " + bundle_path + " : " + e.getMessage());
    }

    try {
        gz_stream = new GzipCompressorInputStream(in);
    } catch (IOException e) {
        TestCase.fail("Failed to uncompress data file " + bundle_path + " : " + e.getMessage());
    }

    tar_stream = new TarArchiveInputStream(gz_stream);

    try {
        byte tmp[] = new byte[4 * 1024];
        int cnt;

        ArchiveEntry te;

        while ((te = tar_stream.getNextEntry()) != null) {
            // System.out.println("Entry: \"" + ze.getName() + "\"");
            File entry_f = new File(fs_path, te.getName());
            if (te.getName().endsWith("/")) {
                // Directory
                continue;
            }
            if (!entry_f.getParentFile().exists()) {
                TestCase.assertTrue(entry_f.getParentFile().mkdirs());
            }
            FileOutputStream fos = new FileOutputStream(entry_f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, tmp.length);

            while ((cnt = tar_stream.read(tmp, 0, tmp.length)) > 0) {
                bos.write(tmp, 0, cnt);
            }
            bos.flush();
            bos.close();
            fos.close();

            //            tar_stream.closeEntry();
        }
        tar_stream.close();
    } catch (IOException e) {
        e.printStackTrace();
        TestCase.fail("Failed to unpack tar file: " + e.getMessage());
    }
}

From source file:com.sun.socialsite.util.Utilities.java

/**
 * Utility method to copy an input stream to an output stream.
 * Wraps both streams in buffers. Ensures right numbers of bytes copied.
 *///  w  ww. j  av a 2s.com
public static void copyInputToOutput(InputStream input, OutputStream output, long byteCount)
        throws IOException {
    int bytes;
    long length;

    BufferedInputStream in = new BufferedInputStream(input);
    BufferedOutputStream out = new BufferedOutputStream(output);

    byte[] buffer;
    buffer = new byte[8192];

    for (length = byteCount; length > 0;) {
        bytes = (int) (length > 8192 ? 8192 : length);

        try {
            bytes = in.read(buffer, 0, bytes);
        } catch (IOException ex) {
            try {
                in.close();
                out.close();
            } catch (IOException ex1) {
            }
            throw new IOException("Reading input stream, " + ex.getMessage());
        }

        if (bytes < 0)
            break;

        length -= bytes;

        try {
            out.write(buffer, 0, bytes);
        } catch (IOException ex) {
            try {
                in.close();
                out.close();
            } catch (IOException ex1) {
            }
            throw new IOException("Writing output stream, " + ex.getMessage());
        }
    }

    try {
        in.close();
        out.close();
    } catch (IOException ex) {
        throw new IOException("Closing file streams, " + ex.getMessage());
    }
}

From source file:com.flagleader.builder.BuilderConfig.java

public static void jarExtracting(String paramString1, String paramString2) {
    int i1 = 2048;
    BufferedOutputStream localBufferedOutputStream = null;
    BufferedInputStream localBufferedInputStream = null;
    JarEntry localJarEntry = null;
    JarFile localJarFile = null;/*ww  w  . j a  v  a 2 s .  com*/
    Enumeration localEnumeration = null;
    try {
        localJarFile = new JarFile(paramString2);
        localEnumeration = localJarFile.entries();
        while (localEnumeration.hasMoreElements()) {
            localJarEntry = (JarEntry) localEnumeration.nextElement();
            if (localJarEntry.isDirectory()) {
                new File(paramString1 + localJarEntry.getName()).mkdirs();
            } else {
                localBufferedInputStream = new BufferedInputStream(localJarFile.getInputStream(localJarEntry));
                byte[] arrayOfByte = new byte[i1];
                FileOutputStream localFileOutputStream = new FileOutputStream(
                        paramString1 + localJarEntry.getName());
                localBufferedOutputStream = new BufferedOutputStream(localFileOutputStream, i1);
                int i2;
                while ((i2 = localBufferedInputStream.read(arrayOfByte, 0, i1)) != -1)
                    localBufferedOutputStream.write(arrayOfByte, 0, i2);
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
                localBufferedInputStream.close();
            }
        }
    } catch (Exception localException1) {
        localException1.printStackTrace();
        try {
            if (localBufferedOutputStream != null) {
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
            }
            if (localBufferedInputStream != null)
                localBufferedInputStream.close();
        } catch (Exception localException2) {
            localException2.printStackTrace();
        }
    } finally {
        try {
            if (localBufferedOutputStream != null) {
                localBufferedOutputStream.flush();
                localBufferedOutputStream.close();
            }
            if (localBufferedInputStream != null)
                localBufferedInputStream.close();
        } catch (Exception localException3) {
            localException3.printStackTrace();
        }
    }
}