2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERConstructedSet.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.util.*;
5
6 public class DERConstructedSet
7     extends DERObject
8 {
9     private Vector set = new Vector();
10
11     public DERConstructedSet()
12     {
13     }
14
15     public void addObject(
16         DEREncodable obj)
17     {
18         set.addElement(obj);
19     }
20
21     public Enumeration getObjects()
22     {
23         return set.elements();
24     }
25
26     /**
27      * return the object at the set postion indicated by index.
28      *
29      * @param the set number (starting at zero) of the object
30      * @return the object at the set postion indicated by index.
31      */
32     public Object getObjectAt(
33         int index)
34     {
35         return set.elementAt(index);
36     }
37
38     /**
39      * return the number of objects in this set.
40      *
41      * @return the number of objects in this set.
42      */
43     public int getSize()
44     {
45         return set.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 SET,
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(SET | CONSTRUCTED, bytes);
76     }
77 }