1    package org.bouncycastle.asn1;
2    
3    import java.io.*;
4    
5    
10   public class DERTaggedObject
11       extends ASN1TaggedObject
12   {
13       
17       public DERTaggedObject(
18           int             tagNo,
19           DEREncodable    obj)
20       {
21                   super(tagNo, obj);
22       }
23   
24       
29       public DERTaggedObject(
30           boolean         explicit,
31           int             tagNo,
32           DEREncodable    obj)
33       {
34                   super(explicit, tagNo, obj);
35       }
36   
37       
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                   
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