2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / BERTaggedObject.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.util.*;
5
6 /**
7  * BER TaggedObject - in ASN.1 nottation this is any object proceeded by
8  * a [n] where n is some number - these are assume to follow the construction
9  * rules (as with sequences).
10  */
11 public class BERTaggedObject
12     extends DERTaggedObject
13 {
14     /**
15      * This creates an empty tagged object of tagNo (ie. zero length).
16      *
17      * @param tagNo the tag number for this object.
18      */
19     public BERTaggedObject(
20         int     tagNo)
21     {
22                 super(tagNo);
23     }
24
25     /**
26      * @param tagNo the tag number for this object.
27      * @param obj the tagged object.
28      */
29     public BERTaggedObject(
30         int         tagNo,
31         DERObject   obj)
32     {
33                 super(tagNo, obj);
34     }
35
36     /**
37      * @param explicit true if an explicitly tagged object.
38      * @param tagNo the tag number for this object.
39      * @param obj the tagged object.
40      */
41     public BERTaggedObject(
42         boolean     explicit,
43         int         tagNo,
44         DERObject   obj)
45     {
46                 super(explicit, tagNo, obj);
47     }
48
49     void encode(
50         DEROutputStream  out)
51         throws IOException
52     {
53                 if (out instanceof BEROutputStream)
54                 {
55             out.write(CONSTRUCTED | TAGGED | tagNo);
56             out.write(0x80);
57
58                         if (!empty)
59                         {
60                                 ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
61                                 BEROutputStream         dOut = new BEROutputStream(bOut);
62
63                 if (!explicit)
64                 {
65                     if (obj instanceof BERConstructedOctetString)
66                     {
67                         Vector  octs = ((BERConstructedOctetString)obj).getDEROctets();
68
69                         for (int i = 0; i != octs.size(); i++)
70                         {
71                             dOut.writeObject(octs.elementAt(i));
72                         }
73                     }
74                     else
75                     {
76                         dOut.writeObject(obj); // hmmm...
77                     }
78                 }
79                 else
80                 {
81                                     dOut.writeObject(obj);
82                 }
83
84                                 dOut.close();
85
86                 out.write(bOut.toByteArray());
87                         }
88
89             out.write(0x00);
90             out.write(0x00);
91                 }
92                 else
93                 {
94                         super.encode(out);
95                 }
96     }
97 }