01: package org.bouncycastle.asn1;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05:
06: /**
07: * DER TaggedObject - in ASN.1 nottation this is any object proceeded by
08: * a [n] where n is some number - these are assume to follow the construction
09: * rules (as with sequences).
10: */
11: public class DERTaggedObject extends ASN1TaggedObject {
12: /**
13: * @param tagNo the tag number for this object.
14: * @param obj the tagged object.
15: */
16: public DERTaggedObject(int tagNo, DEREncodable obj) {
17: super (tagNo, obj);
18: }
19:
20: /**
21: * @param explicit true if an explicitly tagged object.
22: * @param tagNo the tag number for this object.
23: * @param obj the tagged object.
24: */
25: public DERTaggedObject(boolean explicit, int tagNo, DEREncodable obj) {
26: super (explicit, tagNo, obj);
27: }
28:
29: /**
30: * create an implicitly tagged object that contains a zero
31: * length sequence.
32: */
33: public DERTaggedObject(int tagNo) {
34: super (false, tagNo, new DERSequence());
35: }
36:
37: void encode(DEROutputStream out) throws IOException {
38: if (!empty) {
39: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
40: DEROutputStream dOut = new DEROutputStream(bOut);
41:
42: dOut.writeObject(obj);
43: dOut.close();
44:
45: byte[] bytes = bOut.toByteArray();
46:
47: if (explicit) {
48: out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, bytes);
49: } else {
50: //
51: // need to mark constructed types...
52: //
53: if ((bytes[0] & CONSTRUCTED) != 0) {
54: bytes[0] = (byte) (CONSTRUCTED | TAGGED | tagNo);
55: } else {
56: bytes[0] = (byte) (TAGGED | tagNo);
57: }
58:
59: out.write(bytes);
60: }
61: } else {
62: out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, new byte[0]);
63: }
64: }
65: }
|