KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.lowagie.bc.asn1;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.util.Enumeration;
6
7 public class DERSequence
8     extends ASN1Sequence
9 {
10     /**
11      * create an empty sequence
12      */

13     public DERSequence()
14     {
15     }
16
17     /**
18      * create a sequence containing one object
19      */

20     public DERSequence(
21         DEREncodable obj)
22     {
23         this.addObject(obj);
24     }
25
26     /**
27      * create a sequence containing a vector of objects.
28      */

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

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