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:net.shopxx.util.CompressUtils.java

public static void archive(File[] srcFiles, File destFile, String archiverName) {
    Assert.notNull(destFile);// w  w  w.  j  a  va2  s .  c o m
    Assert.state(!destFile.exists() || destFile.isFile());
    Assert.hasText(archiverName);

    File parentFile = destFile.getParentFile();
    if (parentFile != null) {
        parentFile.mkdirs();
    }
    ArchiveOutputStream archiveOutputStream = null;
    try {
        archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(archiverName,
                new BufferedOutputStream(new FileOutputStream(destFile)));
        if (ArrayUtils.isNotEmpty(srcFiles)) {
            for (File srcFile : srcFiles) {
                if (srcFile == null || !srcFile.exists()) {
                    continue;
                }
                Set<File> files = new HashSet<File>();
                if (srcFile.isFile()) {
                    files.add(srcFile);
                }
                if (srcFile.isDirectory()) {
                    files.addAll(FileUtils.listFilesAndDirs(srcFile, TrueFileFilter.INSTANCE,
                            TrueFileFilter.INSTANCE));
                }
                String basePath = FilenameUtils.getFullPath(srcFile.getCanonicalPath());
                for (File file : files) {
                    try {
                        String entryName = FilenameUtils.separatorsToUnix(
                                StringUtils.substring(file.getCanonicalPath(), basePath.length()));
                        ArchiveEntry archiveEntry = archiveOutputStream.createArchiveEntry(file, entryName);
                        archiveOutputStream.putArchiveEntry(archiveEntry);
                        if (file.isFile()) {
                            InputStream inputStream = null;
                            try {
                                inputStream = new BufferedInputStream(new FileInputStream(file));
                                IOUtils.copy(inputStream, archiveOutputStream);
                            } catch (FileNotFoundException e) {
                                throw new RuntimeException(e.getMessage(), e);
                            } catch (IOException e) {
                                throw new RuntimeException(e.getMessage(), e);
                            } finally {
                                IOUtils.closeQuietly(inputStream);
                            }
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    } finally {
                        archiveOutputStream.closeArchiveEntry();
                    }
                }
            }
        }
    } catch (ArchiveException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(archiveOutputStream);
    }
}

From source file:Main.java

private static void extractFile(ZipInputStream in, File outdir, String name) throws IOException {
    byte[] buffer = new byte[4096];

    final File file = new File(outdir, name);
    try (final FileOutputStream fos = new FileOutputStream(file)) {
        try (final BufferedOutputStream out = new BufferedOutputStream(fos)) {
            int count;
            while ((count = in.read(buffer)) != -1) {
                out.write(buffer, 0, count);
            }/*w w w . j a va 2 s  .co  m*/
        }
    }
}

From source file:Main.java

/**
 * Zips a file at a location and places the resulting zip file at the toLocation.
 * <p/>//from   w ww .  j  ava  2s.  co m
 * Example:
 * zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip");
 * <p/>
 * http://stackoverflow.com/a/14868161
 */
public static void zipFileAtPath(String sourcePath, String toLocation) throws IOException {
    final int BUFFER = 2048;

    File sourceFile = new File(sourcePath);
    FileOutputStream fos = new FileOutputStream(toLocation);
    ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(fos));

    if (sourceFile.isDirectory()) {
        zipSubFolder(zipOut, sourceFile, sourceFile.getParent().length() + 1); // ??

    } else {
        byte data[] = new byte[BUFFER];
        FileInputStream fis = new FileInputStream(sourcePath);
        BufferedInputStream bis = new BufferedInputStream(fis, BUFFER);

        String lastPathComponent = sourcePath.substring(sourcePath.lastIndexOf("/"));

        ZipEntry zipEntry = new ZipEntry(lastPathComponent);
        zipOut.putNextEntry(zipEntry);

        int count;
        while ((count = bis.read(data, 0, BUFFER)) != -1) {
            zipOut.write(data, 0, count);
        }
    }

    zipOut.close();
}

From source file:Main.java

/**
 * Export the given bitmap to the specified {@code File}
 * @param context the context//  w  ww. j  a  v  a 2  s.c  o  m
 * @param bitmap the bitmap to export
 * @param file the file in which to save the view
 */
public static void exportBitmapToFile(@NonNull final Context context, @NonNull final Bitmap bitmap,
        @NonNull final File file) {
    OutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(file, false));
        bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, outputStream);
        outputStream.flush();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            //No Worries
        }
    }
}

From source file:Main.java

/**
 * Do an HTTP POST and return the data as a byte array.
 *///w  w  w. j  a  v a2  s.c om
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());
            }
        }
        urlConnection.connect();
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } catch (IOException e) {
        String details;
        if (urlConnection != null) {
            details = "; code=" + urlConnection.getResponseCode() + " (" + urlConnection.getResponseMessage()
                    + ")";
        } else {
            details = "";
        }
        Log.e("ExoplayerUtil", "executePost: Request failed" + details, e);
        throw e;
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:com.ipcglobal.fredimport.util.FredUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the specified directory.
 *
 * @param directoryPath the directory path
 * @param tarGzPath the tar gz path// w  w w  .  j ava  2s .  co m
 * @throws IOException             If anything goes wrong
 */
public static void createTarGzOfDirectory(String directoryPath, String tarGzPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    try {
        fOut = new FileOutputStream(new File(tarGzPath));
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        addFileToTarGz(tOut, directoryPath, "/");
    } finally {
        tOut.finish();
        tOut.close();
        gzOut.close();
        bOut.close();
        fOut.close();
    }
}

From source file:com.oprisnik.semdroid.utils.FileUtils.java

public static void writeObjectToStream(Object object, OutputStream output) throws IOException {
    ObjectOutputStream out = null;
    try {/*from ww w.  j a va2 s.c  o  m*/
        out = new ObjectOutputStream(new BufferedOutputStream(output));
        out.writeObject(object);
        out.flush();
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:au.org.ala.biocache.util.AlaFileUtils.java

/**
 * Creates a zip file at the specified path with the contents of the specified directory.
 * NB://w w  w.  ja v a  2  s.co  m
 *
 * @param directoryPath The path of the directory where the archive will be created. eg. c:/temp
 * @param zipPath The full path of the archive to create. eg. c:/temp/archive.zip
 * @throws IOException If anything goes wrong
 */
public static void createZip(String directoryPath, String zipPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    ZipArchiveOutputStream tOut = null;

    try {
        fOut = new FileOutputStream(new File(zipPath));
        bOut = new BufferedOutputStream(fOut);
        tOut = new ZipArchiveOutputStream(bOut);
        addFileToZip(tOut, directoryPath, "");
    } finally {
        tOut.finish();
        tOut.close();
        bOut.close();
        fOut.close();
    }
}

From source file:eu.scape_project.arc2warc.utils.ArcUtils.java

/**
 * Read the ARC record content into a byte array. Note that the record
 * content can be only read once, it is "consumed" afterwards.
 *
 * @param arcRecord ARC record./*from   w  ww . j  av a 2s .co m*/
 * @return Content byte array.
 * @throws IOException If content is too large to be stored in a byte array.
 */
public static byte[] arcRecordPayloadToByteArray(ARCRecord arcRecord) throws IOException {
    // Byte point where the content of the ARC record begins
    int contentBegin = (int) arcRecord.getMetaData().getContentBegin();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedInputStream buffis = new BufferedInputStream(arcRecord);
    BufferedOutputStream buffos = new BufferedOutputStream(baos);
    byte[] tempBuffer = new byte[BUFFER_SIZE];
    int bytesRead;
    // skip header content
    buffis.skip(contentBegin);
    while ((bytesRead = buffis.read(tempBuffer)) != -1) {
        buffos.write(tempBuffer, 0, bytesRead);
    }
    buffis.close();
    buffos.flush();
    buffos.close();
    return baos.toByteArray();
}

From source file:Main.java

/**
 * Compress files to a zip/*from  w w  w  .  ja v  a2  s .  c  om*/
 * @param zip
 * @param files
 * @throws IOException 
 */
public static void compressToZip(File zip, File[] files) throws IOException {
    byte data[] = new byte[BUFFER];
    FileOutputStream fozip = new FileOutputStream(zip);
    ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip));
    for (int i = 0; i < files.length; i++) {
        System.out.println("Adding:" + files[i]);
        FileInputStream fi = new FileInputStream(files[i]);
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry zipentry = new ZipEntry(files[i].getName());
        zo.putNextEntry(zipentry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            zo.write(data, 0, count);
        }
        origin.close();
    }
    zo.close();
}