List of usage examples for org.bouncycastle.cms CMSCompressedDataStreamGenerator open
public OutputStream open(OutputStream out, OutputCompressor compressor) throws IOException
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; }