Example usage for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream flush

List of usage examples for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream flush

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Usage

From source file:CompressTransfer.java

/**
 * ?//from  ww w.java  2  s . co m
 *
 * @param is
 * @param os
 * @throws Exception
 */
public static void compress(InputStream is, OutputStream os) throws Exception {

    BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(os);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = is.read(data, 0, BUFFER)) != -1) {
        gos.write(data, 0, count);
    }

    gos.finish();

    gos.flush();
    gos.close();
}

From source file:CompressTransfer.java

public static void transfer(FileInputStream srcIs, FileOutputStream destOut) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(destOut);
    int count;/*from   w w w .  ja va  2  s. c  o  m*/
    byte data[] = new byte[BUFFER];
    while ((count = srcIs.read(data, 0, BUFFER)) != -1) {
        //out.write(data, 0, count);
        gos.write(data, 0, count);
    }
    gos.flush();
    gos.close();
}

From source file:com.nextdoor.bender.ipc.s3.S3Transport.java

protected ByteArrayOutputStream compress(ByteArrayOutputStream raw) throws TransportException {
    ByteArrayOutputStream compressed = new ByteArrayOutputStream();
    BZip2CompressorOutputStream bcos = null;
    try {//  w w w  . j a  v a2  s.  c om
        bcos = new BZip2CompressorOutputStream(compressed);
    } catch (IOException e) {
        throw new TransportException("unable to open compressed stream", e);
    }

    try {
        raw.writeTo(bcos);
        bcos.flush();
    } catch (IOException e) {
        throw new TransportException("unable to compress data", e);
    } finally {
        try {
            bcos.close();
        } catch (IOException e) {
        }
    }

    return compressed;
}

From source file:freenet.support.compress.Bzip2Compressor.java

@Override
public long compress(InputStream is, OutputStream os, long maxReadLength, long maxWriteLength)
        throws IOException, CompressionOutputSizeException {
    if (maxReadLength <= 0)
        throw new IllegalArgumentException();
    BZip2CompressorOutputStream bz2os = null;
    try {/* w  ww .j a  v a  2 s  .c  o  m*/
        CountedOutputStream cos = new CountedOutputStream(os);
        bz2os = new BZip2CompressorOutputStream(HeaderStreams.dimOutput(BZ_HEADER, cos));
        long read = 0;
        // Bigger input buffer, so can compress all at once.
        // Won't hurt on I/O either, although most OSs will only return a page at a time.
        byte[] buffer = new byte[32768];
        while (true) {
            int l = (int) Math.min(buffer.length, maxReadLength - read);
            int x = l == 0 ? -1 : is.read(buffer, 0, buffer.length);
            if (x <= -1)
                break;
            if (x == 0)
                throw new IOException("Returned zero from read()");
            bz2os.write(buffer, 0, x);
            read += x;
            if (cos.written() > maxWriteLength)
                throw new CompressionOutputSizeException();
        }
        bz2os.flush();
        cos.flush();
        bz2os.close();
        bz2os = null;
        if (cos.written() > maxWriteLength)
            throw new CompressionOutputSizeException();
        return cos.written();
    } finally {
        if (bz2os != null) {
            bz2os.flush();
            bz2os.close();
        }

    }
}