~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/cms/CMSCompressedDataStreamGenerator.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.cms;
 
2
 
 
3
import java.io.IOException;
 
4
import java.io.OutputStream;
 
5
import java.util.zip.DeflaterOutputStream;
 
6
 
 
7
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
 
8
import org.bouncycastle.asn1.BERSequenceGenerator;
 
9
import org.bouncycastle.asn1.DERInteger;
 
10
import org.bouncycastle.asn1.DERObjectIdentifier;
 
11
import org.bouncycastle.asn1.DERSequenceGenerator;
 
12
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
 
13
import org.bouncycastle.operator.OutputCompressor;
 
14
 
 
15
/**
 
16
 * General class for generating a compressed CMS message stream.
 
17
 * <p>
 
18
 * A simple example of usage.
 
19
 * </p>
 
20
 * <pre>
 
21
 *      CMSCompressedDataStreamGenerator gen = new CMSCompressedDataStreamGenerator();
 
22
 *      
 
23
 *      OutputStream cOut = gen.open(outputStream, new ZlibCompressor());
 
24
 *      
 
25
 *      cOut.write(data);
 
26
 *      
 
27
 *      cOut.close();
 
28
 * </pre>
 
29
 */
 
30
public class CMSCompressedDataStreamGenerator
 
31
{
 
32
    public static final String  ZLIB    = "1.2.840.113549.1.9.16.3.8";
 
33
 
 
34
    private int _bufferSize;
 
35
    
 
36
    /**
 
37
     * base constructor
 
38
     */
 
39
    public CMSCompressedDataStreamGenerator()
 
40
    {
 
41
    }
 
42
 
 
43
    /**
 
44
     * Set the underlying string size for encapsulated data
 
45
     *
 
46
     * @param bufferSize length of octet strings to buffer the data.
 
47
     */
 
48
    public void setBufferSize(
 
49
        int bufferSize)
 
50
    {
 
51
        _bufferSize = bufferSize;
 
52
    }
 
53
 
 
54
    /**
 
55
     * @deprecated use open(OutputStream, ContentCompressor)
 
56
     */
 
57
    public OutputStream open(
 
58
        OutputStream out,
 
59
        String       compressionOID) 
 
60
        throws IOException
 
61
    {
 
62
        return open(out, CMSObjectIdentifiers.data.getId(), compressionOID);
 
63
    }
 
64
 
 
65
    /**
 
66
     * @deprecated use open(OutputStream, ASN1ObjectIdentifier, ContentCompressor)
 
67
     */
 
68
    public OutputStream open(
 
69
        OutputStream  out,        
 
70
        String        contentOID,
 
71
        String        compressionOID) 
 
72
        throws IOException
 
73
    {
 
74
        BERSequenceGenerator sGen = new BERSequenceGenerator(out);
 
75
        
 
76
        sGen.addObject(CMSObjectIdentifiers.compressedData);
 
77
        
 
78
        //
 
79
        // Compressed Data
 
80
        //
 
81
        BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
 
82
        
 
83
        cGen.addObject(new DERInteger(0));
 
84
        
 
85
        //
 
86
        // AlgorithmIdentifier
 
87
        //
 
88
        DERSequenceGenerator algGen = new DERSequenceGenerator(cGen.getRawOutputStream());
 
89
        
 
90
        algGen.addObject(new DERObjectIdentifier(ZLIB));
 
91
 
 
92
        algGen.close();
 
93
        
 
94
        //
 
95
        // Encapsulated ContentInfo
 
96
        //
 
97
        BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());
 
98
        
 
99
        eiGen.addObject(new DERObjectIdentifier(contentOID));
 
100
 
 
101
        OutputStream octetStream = CMSUtils.createBEROctetOutputStream(
 
102
            eiGen.getRawOutputStream(), 0, true, _bufferSize);
 
103
        
 
104
        return new CmsCompressedOutputStream(
 
105
            new DeflaterOutputStream(octetStream), sGen, cGen, eiGen);
 
106
    }
 
107
 
 
108
    public OutputStream open(
 
109
        OutputStream out,
 
110
        OutputCompressor compressor)
 
111
        throws IOException
 
112
    {
 
113
        return open(CMSObjectIdentifiers.data, out, compressor);
 
114
    }
 
115
 
 
116
    /**
 
117
     * Open a compressing output stream.
 
118
     *
 
119
     * @param contentOID
 
120
     * @param out
 
121
     * @param compressor
 
122
     * @return
 
123
     * @throws IOException
 
124
     */
 
125
    public OutputStream open(
 
126
        ASN1ObjectIdentifier contentOID,
 
127
        OutputStream out,
 
128
        OutputCompressor compressor)
 
129
        throws IOException
 
130
    {
 
131
        BERSequenceGenerator sGen = new BERSequenceGenerator(out);
 
132
 
 
133
        sGen.addObject(CMSObjectIdentifiers.compressedData);
 
134
 
 
135
        //
 
136
        // Compressed Data
 
137
        //
 
138
        BERSequenceGenerator cGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
 
139
 
 
140
        cGen.addObject(new DERInteger(0));
 
141
 
 
142
        //
 
143
        // AlgorithmIdentifier
 
144
        //
 
145
        cGen.addObject(compressor.getAlgorithmIdentifier());
 
146
 
 
147
        //
 
148
        // Encapsulated ContentInfo
 
149
        //
 
150
        BERSequenceGenerator eiGen = new BERSequenceGenerator(cGen.getRawOutputStream());
 
151
 
 
152
        eiGen.addObject(contentOID);
 
153
 
 
154
        OutputStream octetStream = CMSUtils.createBEROctetOutputStream(
 
155
            eiGen.getRawOutputStream(), 0, true, _bufferSize);
 
156
 
 
157
        return new CmsCompressedOutputStream(
 
158
            compressor.getOutputStream(octetStream), sGen, cGen, eiGen);
 
159
    }
 
160
 
 
161
    private class CmsCompressedOutputStream
 
162
        extends OutputStream
 
163
    {
 
164
        private OutputStream _out;
 
165
        private BERSequenceGenerator _sGen;
 
166
        private BERSequenceGenerator _cGen;
 
167
        private BERSequenceGenerator _eiGen;
 
168
        
 
169
        CmsCompressedOutputStream(
 
170
            OutputStream out,
 
171
            BERSequenceGenerator sGen,
 
172
            BERSequenceGenerator cGen,
 
173
            BERSequenceGenerator eiGen)
 
174
        {
 
175
            _out = out;
 
176
            _sGen = sGen;
 
177
            _cGen = cGen;
 
178
            _eiGen = eiGen;
 
179
        }
 
180
        
 
181
        public void write(
 
182
            int b)
 
183
            throws IOException
 
184
        {
 
185
            _out.write(b); 
 
186
        }
 
187
        
 
188
        
 
189
        public void write(
 
190
            byte[] bytes,
 
191
            int    off,
 
192
            int    len)
 
193
            throws IOException
 
194
        {
 
195
            _out.write(bytes, off, len);
 
196
        }
 
197
        
 
198
        public void write(
 
199
            byte[] bytes)
 
200
            throws IOException
 
201
        {
 
202
            _out.write(bytes);
 
203
        }
 
204
        
 
205
        public void close()
 
206
            throws IOException
 
207
        {
 
208
            _out.close();
 
209
            _eiGen.close();
 
210
            _cGen.close();
 
211
            _sGen.close();
 
212
        }
 
213
    }
 
214
}