2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERTaggedObject.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4
5 /**
6  * DER TaggedObject - in ASN.1 nottation this is any object proceeded by
7  * a [n] where n is some number - these are assume to follow the construction
8  * rules (as with sequences).
9  */
10 public class DERTaggedObject
11     extends DERObject
12 {
13     int             tagNo;
14     boolean         empty = false;
15     boolean         explicit = true;
16     DEREncodable    obj = null;
17
18     /**
19      * This creates an empty tagged object of tagNo (ie. zero length).
20      *
21      * @param tagNo the tag number for this object.
22      */
23     public DERTaggedObject(
24         int     tagNo)
25     {
26         this.explicit = true;
27         this.tagNo = tagNo;
28         this.empty = true; 
29     }
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         this.explicit = true;
40         this.tagNo = tagNo;
41         this.obj = obj;
42     }
43
44     /**
45      * @param explicit true if the object is explicitly tagged.
46      * @param tagNo the tag number for this object.
47      * @param obj the tagged object.
48      */
49     public DERTaggedObject(
50         boolean         explicit,
51         int             tagNo,
52         DEREncodable    obj)
53     {
54         this.explicit = explicit;
55         this.tagNo = tagNo;
56         this.obj = obj;
57     }
58
59     public int getTagNo()
60     {
61         return tagNo;
62     }
63
64     public boolean isExplicit()
65     {
66         return explicit;
67     }
68
69     public boolean isEmpty()
70     {
71         return empty;
72     }
73
74     public DERObject getObject()
75     {
76         return obj.getDERObject();
77     }
78
79     void encode(
80         DEROutputStream  out)
81         throws IOException
82     {
83         if (!empty)
84         {
85             ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
86             DEROutputStream         dOut = new DEROutputStream(bOut);
87
88             dOut.writeObject(obj);
89             dOut.close();
90
91             byte[]  bytes = bOut.toByteArray();
92
93             if (explicit)
94             {
95                 out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, bOut.toByteArray());
96             }
97             else
98             {
99                 //
100                 // need to mark constructed types...
101                 //
102                 if ((bytes[0] & CONSTRUCTED) != 0)
103                 {
104                     bytes[0] = (byte)(CONSTRUCTED | TAGGED | tagNo);
105                 }
106                 else
107                 {
108                     bytes[0] = (byte)(TAGGED | tagNo);
109                 }
110
111                 out.write(bytes);
112             }
113         }
114         else
115         {
116             out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, new byte[0]);
117         }
118     }
119 }