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