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  public class DERSequence
26      extends ASN1Sequence
27  {
28      /**
29       * create an empty sequence
30       */
31      public DERSequence()
32      {
33      }
34  
35      /**
36       * create a sequence containing one object
37       */
38      public DERSequence(
39          DEREncodable    obj)
40      {
41          this.addObject(obj);
42      }
43  
44      /**
45       * create a sequence containing a vector of objects.
46       */
47      public DERSequence(
48          DEREncodableVector   v)
49      {
50          for (int i = 0; i != v.size(); i++)
51          {
52              this.addObject(v.get(i));
53          }
54      }
55  
56      /**
57       * create a sequence containing an array of objects.
58       */
59      public DERSequence(
60          ASN1Encodable[]   a)
61      {
62          for (int i = 0; i != a.length; i++)
63          {
64              this.addObject(a[i]);
65          }
66      }
67  
68      /*
69       * A note on the implementation:
70       * <p>
71       * As DER requires the constructed, definite-length model to
72       * be used for structured types, this varies slightly from the
73       * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
74       * we also have to specify CONSTRUCTED, and the objects length.
75       */
76      void encode(
77          DEROutputStream out)
78          throws IOException
79      {
80          ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
81          DEROutputStream         dOut = new DEROutputStream(bOut);
82          Enumeration             e = this.getObjects();
83  
84          while (e.hasMoreElements())
85          {
86              Object    obj = e.nextElement();
87  
88              dOut.writeObject(obj);
89          }
90  
91          dOut.close();
92  
93          byte[]  bytes = bOut.toByteArray();
94  
95          out.writeEncoded(SEQUENCE | CONSTRUCTED, bytes);
96      }
97  }