01: package org.bouncycastle.cms;
02:
03: import java.io.ByteArrayInputStream;
04: import java.io.ByteArrayOutputStream;
05: import java.io.IOException;
06: import java.io.InputStream;
07: import java.util.zip.InflaterInputStream;
08:
09: import org.bouncycastle.asn1.ASN1OctetString;
10: import org.bouncycastle.asn1.ASN1OutputStream;
11: import org.bouncycastle.asn1.cms.CompressedData;
12: import org.bouncycastle.asn1.cms.ContentInfo;
13:
14: /**
15: * containing class for an CMS Compressed Data object
16: */
17: public class CMSCompressedData {
18: ContentInfo contentInfo;
19:
20: public CMSCompressedData(byte[] compressedData) throws CMSException {
21: this (CMSUtils.readContentInfo(compressedData));
22: }
23:
24: public CMSCompressedData(InputStream compressedData)
25: throws CMSException {
26: this (CMSUtils.readContentInfo(compressedData));
27: }
28:
29: public CMSCompressedData(ContentInfo contentInfo)
30: throws CMSException {
31: this .contentInfo = contentInfo;
32: }
33:
34: public byte[] getContent() throws CMSException {
35: CompressedData comData = CompressedData.getInstance(contentInfo
36: .getContent());
37: ContentInfo content = comData.getEncapContentInfo();
38:
39: ASN1OctetString bytes = (ASN1OctetString) content.getContent();
40:
41: InflaterInputStream zIn = new InflaterInputStream(
42: new ByteArrayInputStream(bytes.getOctets()));
43: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
44:
45: byte[] buf = new byte[1024];
46: int len;
47:
48: try {
49: while ((len = zIn.read(buf, 0, buf.length)) > 0) {
50: bOut.write(buf, 0, len);
51: }
52: } catch (IOException e) {
53: throw new CMSException(
54: "exception reading compressed stream.", e);
55: }
56:
57: return bOut.toByteArray();
58: }
59:
60: /**
61: * return the ASN.1 encoded representation of this object.
62: */
63: public byte[] getEncoded() throws IOException {
64: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
65: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
66:
67: aOut.writeObject(contentInfo);
68:
69: return bOut.toByteArray();
70: }
71: }
|