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 DEREncodableVector 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 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 63 DEROutputStream dOut = new DEROutputStream(bOut); 64 Enumeration e = this.getObjects(); 65 66 while (e.hasMoreElements()) 67 { 68 Object obj = e.nextElement(); 69 70 dOut.writeObject(obj); 71 } 72 73 dOut.close(); 74 75 byte[] bytes = bOut.toByteArray(); 76 77 out.writeEncoded(SEQUENCE | CONSTRUCTED, bytes); 78 } 79 } 80