001 package org.apache.commons.ssl.asn1;
002
003 import java.io.ByteArrayOutputStream;
004 import java.io.IOException;
005 import java.util.Enumeration;
006
007 public class DERSequence
008 extends ASN1Sequence {
009 /** create an empty sequence */
010 public DERSequence() {
011 }
012
013 /** create a sequence containing one object */
014 public DERSequence(
015 DEREncodable obj) {
016 this.addObject(obj);
017 }
018
019 /** create a sequence containing a vector of objects. */
020 public DERSequence(
021 DEREncodableVector v) {
022 for (int i = 0; i != v.size(); i++) {
023 this.addObject(v.get(i));
024 }
025 }
026
027 /** create a sequence containing an array of objects. */
028 public DERSequence(
029 ASN1Encodable[] a) {
030 for (int i = 0; i != a.length; i++) {
031 this.addObject(a[i]);
032 }
033 }
034
035 /*
036 * A note on the implementation:
037 * <p>
038 * As DER requires the constructed, definite-length model to
039 * be used for structured types, this varies slightly from the
040 * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
041 * we also have to specify CONSTRUCTED, and the objects length.
042 */
043 void encode(
044 DEROutputStream out)
045 throws IOException {
046 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
047 DEROutputStream dOut = new DEROutputStream(bOut);
048 Enumeration e = this.getObjects();
049
050 while (e.hasMoreElements()) {
051 Object obj = e.nextElement();
052
053 dOut.writeObject(obj);
054 }
055
056 dOut.close();
057
058 byte[] bytes = bOut.toByteArray();
059
060 out.writeEncoded(SEQUENCE | CONSTRUCTED, bytes);
061 }
062 }