2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERConstructedSequence.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.util.*;
5
6 public class DERConstructedSequence
7     extends DERObject
8 {
9     private Vector seq = new Vector();
10
11     public DERConstructedSequence()
12     {
13     }
14
15     public void addObject(
16         DEREncodable obj)
17     {
18         seq.addElement(obj);
19     }
20
21     public Enumeration getObjects()
22     {
23         return seq.elements();
24     }
25
26     /**
27      * return the object at the sequence postion indicated by index.
28      *
29      * @param the sequence number (starting at zero) of the object
30      * @return the object at the sequence postion indicated by index.
31      */
32     public Object getObjectAt(
33         int index)
34     {
35         return seq.elementAt(index);
36     }
37
38     /**
39      * return the number of objects in this sequence.
40      *
41      * @return the number of objects in this sequence.
42      */
43     public int getSize()
44     {
45         return seq.size();
46     }
47
48     /*
49      * A note on the implementation:
50      * <p>
51      * As DER requires the constructed, definite-length model to
52      * be used for structured types, this varies slightly from the
53      * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
54      * we also have to specify CONSTRUCTED, and the objects length.
55      */
56     void encode(
57         DEROutputStream out)
58         throws IOException
59     {
60         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
61         DEROutputStream         dOut = new DEROutputStream(bOut);
62         Enumeration             e = getObjects();
63
64         while (e.hasMoreElements())
65         {
66             Object    obj = e.nextElement();
67
68             dOut.writeObject(obj);
69         }
70
71         dOut.close();
72
73         byte[]  bytes = bOut.toByteArray();
74
75         out.writeEncoded(SEQUENCE | CONSTRUCTED, bytes);
76     }
77 }