1 package org.bouncycastle.cms;
2 
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 
7 import org.bouncycastle.asn1.ASN1OctetString;
8 import org.bouncycastle.asn1.BEROctetString;
9 import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
10 import org.bouncycastle.asn1.cms.CompressedData;
11 import org.bouncycastle.asn1.cms.ContentInfo;
12 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
13 import org.bouncycastle.operator.OutputCompressor;
14 
15 /**
16  * General class for generating a compressed CMS message.
17  * <p>
18  * A simple example of usage.
19  * <p>
20  * <pre>
21  *      CMSCompressedDataGenerator  fact = new CMSCompressedDataGenerator();
22  *
23  *      CMSCompressedData           data = fact.generate(content, new ZlibCompressor());
24  * </pre>
25  */
26 public class CMSCompressedDataGenerator
27 {
28     public static final String  ZLIB    = "1.2.840.113549.1.9.16.3.8";
29 
30     /**
31      * base constructor
32      */
CMSCompressedDataGenerator()33     public CMSCompressedDataGenerator()
34     {
35     }
36 
37     /**
38      * generate an object that contains an CMS Compressed Data
39      */
generate( CMSTypedData content, OutputCompressor compressor)40     public CMSCompressedData generate(
41         CMSTypedData content,
42         OutputCompressor compressor)
43         throws CMSException
44     {
45         AlgorithmIdentifier     comAlgId;
46         ASN1OctetString         comOcts;
47 
48         try
49         {
50             ByteArrayOutputStream bOut = new ByteArrayOutputStream();
51             OutputStream zOut = compressor.getOutputStream(bOut);
52 
53             content.write(zOut);
54 
55             zOut.close();
56 
57             comAlgId = compressor.getAlgorithmIdentifier();
58             comOcts = new BEROctetString(bOut.toByteArray());
59         }
60         catch (IOException e)
61         {
62             throw new CMSException("exception encoding data.", e);
63         }
64 
65         ContentInfo     comContent = new ContentInfo(
66                                     content.getContentType(), comOcts);
67 
68         ContentInfo     contentInfo = new ContentInfo(
69                                     CMSObjectIdentifiers.compressedData,
70                                     new CompressedData(comAlgId, comContent));
71 
72         return new CMSCompressedData(contentInfo);
73     }
74 }
75