Home | History | Annotate | Download | only in asn1
      1 package org.bouncycastle.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         ASN1EncodableVector   v)
     31     {
     32         for (int i = 0; i != v.size(); i++)
     33         {
     34             this.addObject(v.get(i));
     35         }
     36     }
     37 
     38     /**
     39      * create a sequence containing an array of objects.
     40      */
     41     public DERSequence(
     42         ASN1Encodable[]   a)
     43     {
     44         for (int i = 0; i != a.length; i++)
     45         {
     46             this.addObject(a[i]);
     47         }
     48     }
     49 
     50     /*
     51      * A note on the implementation:
     52      * <p>
     53      * As DER requires the constructed, definite-length model to
     54      * be used for structured types, this varies slightly from the
     55      * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
     56      * we also have to specify CONSTRUCTED, and the objects length.
     57      */
     58     void encode(
     59         DEROutputStream out)
     60         throws IOException
     61     {
     62         // TODO Intermediate buffer could be avoided if we could calculate expected length
     63         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
     64         DEROutputStream         dOut = new DEROutputStream(bOut);
     65         Enumeration             e = this.getObjects();
     66 
     67         while (e.hasMoreElements())
     68         {
     69             Object    obj = e.nextElement();
     70 
     71             dOut.writeObject(obj);
     72         }
     73 
     74         dOut.close();
     75 
     76         byte[]  bytes = bOut.toByteArray();
     77 
     78         out.writeEncoded(SEQUENCE | CONSTRUCTED, bytes);
     79     }
     80 }
     81