KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class DERSequence
25     extends ASN1Sequence
26 {
27     /**
28      * create an empty sequence
29      */

30     public DERSequence()
31     {
32     }
33
34     /**
35      * create a sequence containing one object
36      */

37     public DERSequence(
38         DEREncodable obj)
39     {
40         this.addObject(obj);
41     }
42
43     /**
44      * create a sequence containing a vector of objects.
45      */

46     public DERSequence(
47         DEREncodableVector v)
48     {
49         for (int i = 0; i != v.size(); i++)
50         {
51             this.addObject(v.get(i));
52         }
53     }
54
55     /**
56      * create a sequence containing an array of objects.
57      */

58     public DERSequence(
59         ASN1Encodable[] a)
60     {
61         for (int i = 0; i != a.length; i++)
62         {
63             this.addObject(a[i]);
64         }
65     }
66
67     /*
68      * A note on the implementation:
69      * <p>
70      * As DER requires the constructed, definite-length model to
71      * be used for structured types, this varies slightly from the
72      * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
73      * we also have to specify CONSTRUCTED, and the objects length.
74      */

75     void encode(
76         DEROutputStream out)
77         throws IOException JavaDoc
78     {
79         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
80         DEROutputStream dOut = new DEROutputStream(bOut);
81         Enumeration JavaDoc e = this.getObjects();
82
83         while (e.hasMoreElements())
84         {
85             Object JavaDoc obj = e.nextElement();
86
87             dOut.writeObject(obj);
88         }
89
90         dOut.close();
91
92         byte[] bytes = bOut.toByteArray();
93
94         out.writeEncoded(SEQUENCE | CONSTRUCTED, bytes);
95     }
96 }
97
Popular Tags