KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > DERSet


1 package com.lowagie.bc.asn1;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.util.Enumeration;
6
7 /**
8  * A DER encoded set object
9  */

10 public class DERSet
11     extends ASN1Set
12 {
13     /**
14      * create an empty set
15      */

16     public DERSet()
17     {
18     }
19
20     /**
21      * @param obj - a single object that makes up the set.
22      */

23     public DERSet(
24         DEREncodable obj)
25     {
26         this.addObject(obj);
27     }
28
29     /**
30      * @param v - a vector of objects making up the set.
31      */

32     public DERSet(
33         DEREncodableVector v)
34     {
35         for (int i = 0; i != v.size(); i++)
36         {
37             this.addObject(v.get(i));
38         }
39     }
40
41     /*
42      * A note on the implementation:
43      * <p>
44      * As DER requires the constructed, definite-length model to
45      * be used for structured types, this varies slightly from the
46      * ASN.1 descriptions given. Rather than just outputing SET,
47      * we also have to specify CONSTRUCTED, and the objects length.
48      */

49     void encode(
50         DEROutputStream out)
51         throws IOException
52     {
53         ByteArrayOutputStream bOut = new ByteArrayOutputStream();
54         DEROutputStream dOut = new DEROutputStream(bOut);
55         Enumeration e = this.getObjects();
56
57         while (e.hasMoreElements())
58         {
59             Object obj = e.nextElement();
60
61             dOut.writeObject(obj);
62         }
63
64         dOut.close();
65
66         byte[] bytes = bOut.toByteArray();
67
68         out.writeEncoded(SET | CONSTRUCTED, bytes);
69     }
70 }
71
Popular Tags