01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.crypto.asn1;
17:
18: import java.io.ByteArrayOutputStream;
19: import java.io.IOException;
20: import java.util.Enumeration;
21:
22: /**
23: * A DER encoded set object
24: */
25: public class DERSet extends ASN1Set {
26: /**
27: * create an empty set
28: */
29: public DERSet() {
30: }
31:
32: /**
33: * @param obj - a single object that makes up the set.
34: */
35: public DERSet(DEREncodable obj) {
36: this .addObject(obj);
37: }
38:
39: /**
40: * @param v - a vector of objects making up the set.
41: */
42: public DERSet(DEREncodableVector v) {
43: this (v, true);
44: }
45:
46: /**
47: * create a set from an array of objects.
48: */
49: public DERSet(ASN1Encodable[] a) {
50: for (int i = 0; i != a.length; i++) {
51: this .addObject(a[i]);
52: }
53:
54: this .sort();
55: }
56:
57: /**
58: * @param v - a vector of objects making up the set.
59: */
60: DERSet(DEREncodableVector v, boolean needsSorting) {
61: for (int i = 0; i != v.size(); i++) {
62: this .addObject(v.get(i));
63: }
64:
65: if (needsSorting) {
66: this .sort();
67: }
68: }
69:
70: /*
71: * A note on the implementation:
72: * <p>
73: * As DER requires the constructed, definite-length model to
74: * be used for structured types, this varies slightly from the
75: * ASN.1 descriptions given. Rather than just outputing SET,
76: * we also have to specify CONSTRUCTED, and the objects length.
77: */
78: void encode(DEROutputStream out) throws IOException {
79: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
80: DEROutputStream dOut = new DEROutputStream(bOut);
81: Enumeration e = this .getObjects();
82:
83: while (e.hasMoreElements()) {
84: Object obj = e.nextElement();
85:
86: dOut.writeObject(obj);
87: }
88:
89: dOut.close();
90:
91: byte[] bytes = bOut.toByteArray();
92:
93: out.writeEncoded(SET | CONSTRUCTED, bytes);
94: }
95: }
|