Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

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

Prototype

public BufferedOutputStream(OutputStream out) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream.

Usage

From source file:Main.java

public static void extractFile(ZipInputStream in, File outdir, String name) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outdir, name)));
    int count = -1;
    while ((count = in.read(buffer)) != -1)
        out.write(buffer, 0, count);//  w w w  . j  a va2  s  . c om
    out.close();
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

public static void makeZip(String archiveFileName, String sDir, String sRelativeDir) throws Exception {
    FileOutputStream fos = new FileOutputStream(archiveFileName);
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
    int nbZipEntries = ZipUtils.putEntries(zos, sDir, sRelativeDir, Collections.<File>emptyList());
    if (nbZipEntries > 0)
        zos.close();/*ww  w. jav  a 2s  .c  o  m*/
}

From source file:Main.java

public static boolean unzipFile(InputStream fis, String destDir) {
    final byte[] buffer = new byte[4096];
    ZipInputStream zis = null;//  w ww  .j  av a  2 s  . c  o  m
    Log.e("Unzip", "destDir = " + destDir);
    try {
        // make sure the directory is existent
        File dstFile = new File(destDir);
        if (!dstFile.exists()) {
            dstFile.mkdirs();
        } else {
            int fileLenght = dstFile.listFiles().length;
            if (fileLenght >= 2) {
                return true;
            }
        }
        zis = new ZipInputStream(fis);
        ZipEntry entry;

        while ((entry = zis.getNextEntry()) != null) {
            String fileName = entry.getName();

            if (entry.isDirectory()) {
                new File(destDir, fileName).mkdirs();

            } else {
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(new File(destDir, fileName)));
                int lenRead;

                while ((lenRead = zis.read(buffer)) != -1) {
                    bos.write(buffer, 0, lenRead);
                }

                bos.close();
            }

            zis.closeEntry();
        }

        return true;

    } catch (IOException e) {
        e.printStackTrace();

    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

/**
 * marshal document object into outputStream.
 * //w w  w.j  a va 2s .  c o  m
 * @param sourceDocument
 * @param targetFile
 * @throws ParserConfigurationException
 * @throws TransformerConfigurationException
 * @throws TransformerException
 * @throws IOException
 */
public static void marshal(Document sourceDocument, File targetFile) throws ParserConfigurationException,
        TransformerConfigurationException, TransformerException, IOException {
    BufferedOutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
        marshal(sourceDocument, outputStream);
    } finally {
        outputStream.close();
    }
}

From source file:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.util.SerializationUtils.java

/**
 * @param object// w w  w  . j a v  a  2s.c o m
 *          object to be serialized
 * @param filePath
 *          the object will be written at this location, directories will be
 *          created according to path
 * @throws Exception exception
 */
public static void serializeToDisk(Serializable object, String filePath) throws Exception {
    File dir = new File(FilenameUtils.getFullPathNoEndSeparator(filePath));
    if (!dir.exists()) {
        FileUtils.forceMkdir(dir);
    } else {
        if (dir.isFile()) {
            throw new IOException("Path to dir is a file!");
        }
    }
    ObjectOutputStream objOut = null;
    objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filePath)));
    objOut.writeObject(object);
    objOut.flush();
    objOut.close();
}

From source file:com.yqboots.fss.util.ZipUtils.java

/**
 * Compresses the specified directory to a zip file
 *
 * @param dir the directory to compress/* w ww  .jav a2  s  .c o  m*/
 * @return the compressed file
 * @throws IOException
 */
public static Path compress(Path dir) throws IOException {
    Assert.isTrue(Files.exists(dir), "The directory does not exist: " + dir.toAbsolutePath());
    Assert.isTrue(Files.isDirectory(dir), "Should be a directory: " + dir.toAbsolutePath());

    Path result = Paths.get(dir.toAbsolutePath() + FileType.DOT_ZIP);
    try (final ZipOutputStream out = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(result.toFile())))) {
        // out.setMethod(ZipOutputStream.DEFLATED);
        final byte data[] = new byte[BUFFER];
        // get a list of files from current directory
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs)
                    throws IOException {
                final File file = path.toFile();

                // compress to relative directory, not absolute
                final String root = StringUtils.substringAfter(file.getParent(), dir.toString());
                try (final BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file),
                        BUFFER)) {
                    final ZipEntry entry = new ZipEntry(root + File.separator + path.getFileName());
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                    }
                }

                return FileVisitResult.CONTINUE;
            }
        });
    }

    return result;
}

From source file:com.qwazr.utils.SerializationUtils.java

/**
 * Write an object to a file using a buffered stream and GZIP compression.
 *
 * @param obj  the object to write//from   w  ww  .jav a2  s  .  c o m
 * @param file the destination file
 * @throws IOException
 */
public static void serialize(Serializable obj, File file) throws IOException {
    FileOutputStream os = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(os);
    GZIPOutputStream zos = new GZIPOutputStream(bos);
    try {
        SerializationUtils.serialize(obj, zos);
    } finally {
        IOUtils.close(zos, bos, os);
    }
}

From source file:Main.java

/**
 * Do an HTTP POST and return the data as a byte array.
 *//*from  ww w .  ja  va 2  s  . c o  m*/
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws MalformedURLException, IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:com.mc.printer.model.utils.ZipHelper.java

public static void unZip(String sourceZip, String outDirName) throws IOException {
    log.info("unzip source:" + sourceZip);
    log.info("unzip to :" + outDirName);
    ZipFile zfile = new ZipFile(sourceZip);
    System.out.println(zfile.getName());
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;/*from w w  w  .  java2  s  .  co m*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        //ZipFileZipEntry
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        //ZipEntry?InputStreamOutputStream
        File fil = getRealFileName(outDirName, ze.getName());

        OutputStream os = new BufferedOutputStream(new FileOutputStream(fil));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
        log.debug("Extracted: " + ze.getName());
    }
    zfile.close();

}

From source file:GenericServer.java

public void run() {
    try {//w ww .  j a va2s . c  o  m
        ServiceOutputStream outStream = new ServiceOutputStream(
                new BufferedOutputStream(client.getOutputStream()));
        ServiceInputStream inStream = new ServiceInputStream(client.getInputStream());
        ServiceRequest request = inStream.getRequest();
        while (processRequest(outStream)) {
        }
        client.close();
    } catch (IOException ex) {
        System.exit(0);
    }
}