View Javadoc

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