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