001: package org.bouncycastle.cms;
002:
003: import java.io.IOException;
004: import java.io.OutputStream;
005: import java.util.zip.DeflaterOutputStream;
006:
007: import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
008: import org.bouncycastle.asn1.BERSequenceGenerator;
009: import org.bouncycastle.asn1.DERInteger;
010: import org.bouncycastle.asn1.DERObjectIdentifier;
011: import org.bouncycastle.asn1.DERSequenceGenerator;
012: import org.bouncycastle.asn1.BEROctetStringGenerator;
013:
014: /**
015: * General class for generating a compressed CMS message stream.
016: * <p>
017: * A simple example of usage.
018: * </p>
019: * <pre>
020: * CMSCompressedDataStreamGenerator gen = new CMSCompressedDataStreamGenerator();
021: *
022: * OutputStream cOut = gen.open(outputStream, CMSCompressedDataStreamGenerator.ZLIB);
023: *
024: * cOut.write(data);
025: *
026: * cOut.close();
027: * </pre>
028: */
029: public class CMSCompressedDataStreamGenerator {
030: public static final String ZLIB = "1.2.840.113549.1.9.16.3.8";
031:
032: /**
033: * base constructor
034: */
035: public CMSCompressedDataStreamGenerator() {
036: }
037:
038: public OutputStream open(OutputStream out, String compressionOID)
039: throws IOException {
040: return open(out, CMSObjectIdentifiers.data.getId(),
041: compressionOID);
042: }
043:
044: public OutputStream open(OutputStream out, String contentOID,
045: String compressionOID) throws IOException {
046: BERSequenceGenerator sGen = new BERSequenceGenerator(out);
047:
048: sGen.addObject(CMSObjectIdentifiers.compressedData);
049:
050: //
051: // Compressed Data
052: //
053: BERSequenceGenerator cGen = new BERSequenceGenerator(sGen
054: .getRawOutputStream(), 0, true);
055:
056: cGen.addObject(new DERInteger(0));
057:
058: //
059: // AlgorithmIdentifier
060: //
061: DERSequenceGenerator algGen = new DERSequenceGenerator(cGen
062: .getRawOutputStream());
063:
064: algGen.addObject(new DERObjectIdentifier(ZLIB));
065:
066: algGen.close();
067:
068: //
069: // Encapsulated ContentInfo
070: //
071: BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen
072: .getRawOutputStream());
073:
074: eiGen.addObject(new DERObjectIdentifier(contentOID));
075:
076: BEROctetStringGenerator octGen = new BEROctetStringGenerator(
077: eiGen.getRawOutputStream(), 0, true);
078:
079: return new CmsCompressedOutputStream(new DeflaterOutputStream(
080: octGen.getOctetOutputStream()), sGen, cGen, eiGen);
081: }
082:
083: private class CmsCompressedOutputStream extends OutputStream {
084: private DeflaterOutputStream _out;
085: private BERSequenceGenerator _sGen;
086: private BERSequenceGenerator _cGen;
087: private BERSequenceGenerator _eiGen;
088:
089: CmsCompressedOutputStream(DeflaterOutputStream out,
090: BERSequenceGenerator sGen, BERSequenceGenerator cGen,
091: BERSequenceGenerator eiGen) {
092: _out = out;
093: _sGen = sGen;
094: _cGen = cGen;
095: _eiGen = eiGen;
096: }
097:
098: public void write(int b) throws IOException {
099: _out.write(b);
100: }
101:
102: public void write(byte[] bytes, int off, int len)
103: throws IOException {
104: _out.write(bytes, off, len);
105: }
106:
107: public void write(byte[] bytes) throws IOException {
108: _out.write(bytes);
109: }
110:
111: public void close() throws IOException {
112: _out.close();
113: _eiGen.close();
114: _cGen.close();
115: _sGen.close();
116: }
117: }
118: }
|