1    package org.bouncycastle.asn1;
2    
3    import java.io.*;
4    import java.util.*;
5    
6    public class DERConstructedSet
7        extends ASN1Set
8    {
9        public DERConstructedSet()
10       {
11       }
12   
13       /**
14        * @param obj - a single object that makes up the set.
15        */
16       public DERConstructedSet(
17           DEREncodable   obj)
18       {
19           this.addObject(obj);
20       }
21   
22       /**
23        * @param v - a vector of objects making up the set.
24        */
25       public DERConstructedSet(
26           DEREncodableVector   v)
27       {
28           for (int i = 0; i != v.size(); i++)
29           {
30               this.addObject(v.get(i));
31           }
32       }
33   
34       public void addObject(
35           DEREncodable    obj)
36       {
37           super.addObject(obj);
38       }
39   
40       public int getSize()
41       {
42           return size();
43       }
44   
45       /*
46        * A note on the implementation:
47        * <p>
48        * As DER requires the constructed, definite-length model to
49        * be used for structured types, this varies slightly from the
50        * ASN.1 descriptions given. Rather than just outputing SET,
51        * we also have to specify CONSTRUCTED, and the objects length.
52        */
53       void encode(
54           DEROutputStream out)
55           throws IOException
56       {
57           ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
58           DEROutputStream         dOut = new DEROutputStream(bOut);
59           Enumeration             e = this.getObjects();
60   
61           while (e.hasMoreElements())
62           {
63               Object    obj = e.nextElement();
64   
65               dOut.writeObject(obj);
66           }
67   
68           dOut.close();
69   
70           byte[]  bytes = bOut.toByteArray();
71   
72           out.writeEncoded(SET | CONSTRUCTED, bytes);
73       }
74   }
75