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 ASN1TaggedObject
12   {
13       /**
14        * @param tagNo the tag number for this object.
15        * @param obj the tagged object.
16        */
17       public DERTaggedObject(
18           int             tagNo,
19           DEREncodable    obj)
20       {
21                   super(tagNo, obj);
22       }
23   
24       /**
25        * @param explicit true if an explicitly tagged object.
26        * @param tagNo the tag number for this object.
27        * @param obj the tagged object.
28        */
29       public DERTaggedObject(
30           boolean         explicit,
31           int             tagNo,
32           DEREncodable    obj)
33       {
34                   super(explicit, tagNo, obj);
35       }
36   
37       /**
38        * create an implicitly tagged object that contains a zero
39        * length sequence.
40        */
41       public DERTaggedObject(
42           int             tagNo)
43       {
44           super(false, tagNo, new DERSequence());
45       }
46   
47       void encode(
48           DEROutputStream  out)
49           throws IOException
50       {
51           if (!empty)
52           {
53               ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
54               DEROutputStream         dOut = new DEROutputStream(bOut);
55   
56               dOut.writeObject(obj);
57               dOut.close();
58   
59               byte[]  bytes = bOut.toByteArray();
60   
61               if (explicit)
62               {
63                   out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, bytes);
64               }
65               else
66               {
67                   //
68                   // need to mark constructed types...
69                   //
70                   if ((bytes[0] & CONSTRUCTED) != 0)
71                   {
72                       bytes[0] = (byte)(CONSTRUCTED | TAGGED | tagNo);
73                   }
74                   else
75                   {
76                       bytes[0] = (byte)(TAGGED | tagNo);
77                   }
78   
79                   out.write(bytes);
80               }
81           }
82           else
83           {
84               out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, new byte[0]);
85           }
86       }
87   }
88