Example usage for org.bouncycastle.cms CMSCompressedDataStreamGenerator open

List of usage examples for org.bouncycastle.cms CMSCompressedDataStreamGenerator open

Introduction

In this page you can find the example usage for org.bouncycastle.cms CMSCompressedDataStreamGenerator open.

Prototype

public OutputStream open(OutputStream out, OutputCompressor compressor) throws IOException 

Source Link

Document

Open a compressing output stream with the PKCS#7 content type OID of "data".

Usage

From source file:de.mendelson.util.security.BCCryptoHelper.java

/**
 * Compress a data stream/* w  w w .ja v a2  s . co m*/
 */
public void compressCMS(InputStream uncompressed, OutputStream compressed, boolean inMemory) throws Exception {
    //streamed compression does not work without a stream buffer in bc 1.45 and before
    CMSCompressedDataStreamGenerator generator = new CMSCompressedDataStreamGenerator();
    if (inMemory) {
        ByteArrayOutputStream memBuffer = new ByteArrayOutputStream();
        OutputStream cOut = generator.open(memBuffer, new ZlibCompressor());
        this.copyStreams(uncompressed, cOut);
        cOut.flush();
        cOut.close();
        compressed.write(memBuffer.toByteArray());
    } else {
        File tempFile = File.createTempFile("compress", ".temp");
        FileOutputStream fileBuffer = null;
        try {
            fileBuffer = new FileOutputStream(tempFile);
            OutputStream cOut = generator.open(fileBuffer, new ZlibCompressor());
            this.copyStreams(uncompressed, cOut);
            cOut.flush();
            cOut.close();
        } finally {
            if (fileBuffer != null) {
                fileBuffer.flush();
                fileBuffer.close();
            }
        }
        FileInputStream fileIn = null;
        try {
            fileIn = new FileInputStream(tempFile);
            this.copyStreams(fileIn, compressed);
        } finally {
            if (fileIn != null) {
                fileIn.close();
            }
        }
        boolean deleted = tempFile.delete();
    }
}

From source file:org.neociclo.odetteftp.util.EnvelopingUtil.java

License:Apache License

public static OutputStream openCompressedDataStreamGenerator(OutputStream outStream) throws IOException {

    CMSCompressedDataStreamGenerator gen = new CMSCompressedDataStreamGenerator();

    OutputStream compressed = gen.open(outStream, CMSCompressedDataGenerator.ZLIB);
    return compressed;

}