KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 /**
24  * DER TaggedObject - in ASN.1 nottation this is any object proceeded by
25  * a [n] where n is some number - these are assume to follow the construction
26  * rules (as with sequences).
27  */

28 public class DERTaggedObject
29     extends ASN1TaggedObject
30 {
31     /**
32      * @param tagNo the tag number for this object.
33      * @param obj the tagged object.
34      */

35     public DERTaggedObject(
36         int tagNo,
37         DEREncodable obj)
38     {
39         super(tagNo, obj);
40     }
41
42     /**
43      * @param explicit true if an explicitly tagged object.
44      * @param tagNo the tag number for this object.
45      * @param obj the tagged object.
46      */

47     public DERTaggedObject(
48         boolean explicit,
49         int tagNo,
50         DEREncodable obj)
51     {
52         super(explicit, tagNo, obj);
53     }
54
55     /**
56      * create an implicitly tagged object that contains a zero
57      * length sequence.
58      */

59     public DERTaggedObject(
60         int tagNo)
61     {
62         super(false, tagNo, new DERSequence());
63     }
64
65     void encode(
66         DEROutputStream out)
67         throws IOException JavaDoc
68     {
69         if (!empty)
70         {
71             ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
72             DEROutputStream dOut = new DEROutputStream(bOut);
73
74             dOut.writeObject(obj);
75             dOut.close();
76
77             byte[] bytes = bOut.toByteArray();
78
79             if (explicit)
80             {
81                 out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, bytes);
82             }
83             else
84             {
85                 //
86
// need to mark constructed types...
87
//
88
if ((bytes[0] & CONSTRUCTED) != 0)
89                 {
90                     bytes[0] = (byte)(CONSTRUCTED | TAGGED | tagNo);
91                 }
92                 else
93                 {
94                     bytes[0] = (byte)(TAGGED | tagNo);
95                 }
96
97                 out.write(bytes);
98             }
99         }
100         else
101         {
102             out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, new byte[0]);
103         }
104     }
105 }
106
Popular Tags