b8a3c2d57b30fcbfa6b51c5cb91fa5fc072ad496
[org.ibex.core.git] / src / org / bouncycastle / asn1 / BERConstructedSequence.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.util.*;
5
6 public class BERConstructedSequence
7     extends DERConstructedSequence
8 {
9     /*
10      * A note on the implementation:
11      * <p>
12      * As DER requires the constructed, definite-length model to
13      * be used for structured types, this varies slightly from the
14      * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
15      * we also have to specify CONSTRUCTED, and the objects length.
16      */
17     void encode(
18         DEROutputStream out)
19         throws IOException
20     {
21         if (out instanceof BEROutputStream)
22         {
23             ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
24             BEROutputStream         dOut = new BEROutputStream(bOut);
25             Enumeration             e = getObjects();
26
27             while (e.hasMoreElements())
28             {
29                 Object    obj = e.nextElement();
30
31                 dOut.writeObject(obj);
32             }
33
34             dOut.close();
35
36             byte[]  bytes = bOut.toByteArray();
37
38             out.write(SEQUENCE | CONSTRUCTED);
39             out.write(0x80);
40             out.write(bytes);
41             out.write(0x00);
42             out.write(0x00);
43         }
44         else
45         {
46             super.encode(out);
47         }
48     }
49 }