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