KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > DERSet


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23
24 /**
25  * A DER encoded set object
26  */

27 public class DERSet
28     extends ASN1Set
29 {
30     /**
31      * create an empty set
32      */

33     public DERSet()
34     {
35     }
36
37     /**
38      * @param obj - a single object that makes up the set.
39      */

40     public DERSet(
41         DEREncodable obj)
42     {
43         this.addObject(obj);
44     }
45
46     /**
47      * @param v - a vector of objects making up the set.
48      */

49     public DERSet(
50         DEREncodableVector v)
51     {
52         this(v, true);
53     }
54
55     /**
56      * create a set from an array of objects.
57      */

58     public DERSet(
59         ASN1Encodable[] a)
60     {
61         for (int i = 0; i != a.length; i++)
62         {
63             this.addObject(a[i]);
64         }
65
66         this.sort();
67     }
68
69     /**
70      * @param v - a vector of objects making up the set.
71      */

72     DERSet(
73         DEREncodableVector v,
74         boolean needsSorting)
75     {
76         for (int i = 0; i != v.size(); i++)
77         {
78             this.addObject(v.get(i));
79         }
80
81         if (needsSorting)
82         {
83             this.sort();
84         }
85     }
86
87     /*
88      * A note on the implementation:
89      * <p>
90      * As DER requires the constructed, definite-length model to
91      * be used for structured types, this varies slightly from the
92      * ASN.1 descriptions given. Rather than just outputing SET,
93      * we also have to specify CONSTRUCTED, and the objects length.
94      */

95     void encode(
96         DEROutputStream out)
97         throws IOException JavaDoc
98     {
99         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
100         DEROutputStream dOut = new DEROutputStream(bOut);
101         Enumeration JavaDoc e = this.getObjects();
102
103         while (e.hasMoreElements())
104         {
105             Object JavaDoc obj = e.nextElement();
106
107             dOut.writeObject(obj);
108         }
109
110         dOut.close();
111
112         byte[] bytes = bOut.toByteArray();
113
114         out.writeEncoded(SET | CONSTRUCTED, bytes);
115     }
116 }
117
Popular Tags