01: package org.bouncycastle.asn1;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05: import java.util.Enumeration;
06:
07: public class DERSequence extends ASN1Sequence {
08: /**
09: * create an empty sequence
10: */
11: public DERSequence() {
12: }
13:
14: /**
15: * create a sequence containing one object
16: */
17: public DERSequence(DEREncodable obj) {
18: this .addObject(obj);
19: }
20:
21: /**
22: * create a sequence containing a vector of objects.
23: */
24: public DERSequence(DEREncodableVector v) {
25: for (int i = 0; i != v.size(); i++) {
26: this .addObject(v.get(i));
27: }
28: }
29:
30: /**
31: * create a sequence containing an array of objects.
32: */
33: public DERSequence(ASN1Encodable[] a) {
34: for (int i = 0; i != a.length; i++) {
35: this .addObject(a[i]);
36: }
37: }
38:
39: /*
40: * A note on the implementation:
41: * <p>
42: * As DER requires the constructed, definite-length model to
43: * be used for structured types, this varies slightly from the
44: * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
45: * we also have to specify CONSTRUCTED, and the objects length.
46: */
47: void encode(DEROutputStream out) throws IOException {
48: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
49: DEROutputStream dOut = new DEROutputStream(bOut);
50: Enumeration e = this .getObjects();
51:
52: while (e.hasMoreElements()) {
53: Object obj = e.nextElement();
54:
55: dOut.writeObject(obj);
56: }
57:
58: dOut.close();
59:
60: byte[] bytes = bOut.toByteArray();
61:
62: out.writeEncoded(SEQUENCE | CONSTRUCTED, bytes);
63: }
64: }
|