2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERTaggedObject.java
diff --git a/src/org/bouncycastle/asn1/DERTaggedObject.java b/src/org/bouncycastle/asn1/DERTaggedObject.java
new file mode 100644 (file)
index 0000000..4ec0310
--- /dev/null
@@ -0,0 +1,119 @@
+package org.bouncycastle.asn1;
+
+import java.io.*;
+
+/**
+ * DER TaggedObject - in ASN.1 nottation this is any object proceeded by
+ * a [n] where n is some number - these are assume to follow the construction
+ * rules (as with sequences).
+ */
+public class DERTaggedObject
+    extends DERObject
+{
+    int             tagNo;
+    boolean         empty = false;
+    boolean         explicit = true;
+    DEREncodable    obj = null;
+
+    /**
+     * This creates an empty tagged object of tagNo (ie. zero length).
+     *
+     * @param tagNo the tag number for this object.
+     */
+    public DERTaggedObject(
+        int     tagNo)
+    {
+        this.explicit = true;
+        this.tagNo = tagNo;
+        this.empty = true; 
+    }
+
+    /**
+     * @param tagNo the tag number for this object.
+     * @param obj the tagged object.
+     */
+    public DERTaggedObject(
+        int             tagNo,
+        DEREncodable    obj)
+    {
+        this.explicit = true;
+        this.tagNo = tagNo;
+        this.obj = obj;
+    }
+
+    /**
+     * @param explicit true if the object is explicitly tagged.
+     * @param tagNo the tag number for this object.
+     * @param obj the tagged object.
+     */
+    public DERTaggedObject(
+        boolean         explicit,
+        int             tagNo,
+        DEREncodable    obj)
+    {
+        this.explicit = explicit;
+        this.tagNo = tagNo;
+        this.obj = obj;
+    }
+
+    public int getTagNo()
+    {
+        return tagNo;
+    }
+
+    public boolean isExplicit()
+    {
+        return explicit;
+    }
+
+    public boolean isEmpty()
+    {
+        return empty;
+    }
+
+    public DERObject getObject()
+    {
+        return obj.getDERObject();
+    }
+
+    void encode(
+        DEROutputStream  out)
+        throws IOException
+    {
+        if (!empty)
+        {
+            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
+            DEROutputStream         dOut = new DEROutputStream(bOut);
+
+            dOut.writeObject(obj);
+            dOut.close();
+
+            byte[]  bytes = bOut.toByteArray();
+
+            if (explicit)
+            {
+                out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, bOut.toByteArray());
+            }
+            else
+            {
+                //
+                // need to mark constructed types...
+                //
+                if ((bytes[0] & CONSTRUCTED) != 0)
+                {
+                    bytes[0] = (byte)(CONSTRUCTED | TAGGED | tagNo);
+                }
+                else
+                {
+                    bytes[0] = (byte)(TAGGED | tagNo);
+                }
+
+                out.write(bytes);
+            }
+        }
+        else
+        {
+            out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, new byte[0]);
+        }
+    }
+}