1package org.bouncycastle.cms;
2
3import java.io.IOException;
4import java.io.OutputStream;
5import java.util.Iterator;
6
7import org.bouncycastle.asn1.ASN1Encodable;
8import org.bouncycastle.asn1.ASN1Encoding;
9import org.bouncycastle.asn1.ASN1ObjectIdentifier;
10import org.bouncycastle.asn1.ASN1Sequence;
11
12public class PKCS7ProcessableObject
13    implements CMSTypedData
14{
15    private final ASN1ObjectIdentifier type;
16    private final ASN1Encodable structure;
17
18    public PKCS7ProcessableObject(
19        ASN1ObjectIdentifier type,
20        ASN1Encodable structure)
21    {
22        this.type = type;
23        this.structure = structure;
24    }
25
26    public ASN1ObjectIdentifier getContentType()
27    {
28        return type;
29    }
30
31    public void write(OutputStream cOut)
32        throws IOException, CMSException
33    {
34        if (structure instanceof ASN1Sequence)
35        {
36            ASN1Sequence s = ASN1Sequence.getInstance(structure);
37
38            for (Iterator it = s.iterator(); it.hasNext();)
39            {
40                ASN1Encodable enc = (ASN1Encodable)it.next();
41
42                cOut.write(enc.toASN1Primitive().getEncoded(ASN1Encoding.DER));
43            }
44        }
45        else
46        {
47            byte[] encoded = structure.toASN1Primitive().getEncoded(ASN1Encoding.DER);
48            int index = 1;
49
50            while ((encoded[index] & 0xff) > 127)
51            {
52                index++;
53            }
54
55            index++;
56
57            cOut.write(encoded, index, encoded.length - index);
58        }
59    }
60
61    public Object getContent()
62    {
63        return structure;
64    }
65}
66