2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / BERConstructedSequence.java
diff --git a/src/org/bouncycastle/asn1/BERConstructedSequence.java b/src/org/bouncycastle/asn1/BERConstructedSequence.java
new file mode 100644 (file)
index 0000000..b8a3c2d
--- /dev/null
@@ -0,0 +1,49 @@
+package org.bouncycastle.asn1;
+
+import java.io.*;
+import java.util.*;
+
+public class BERConstructedSequence
+    extends DERConstructedSequence
+{
+    /*
+     * A note on the implementation:
+     * <p>
+     * As DER requires the constructed, definite-length model to
+     * be used for structured types, this varies slightly from the
+     * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
+     * we also have to specify CONSTRUCTED, and the objects length.
+     */
+    void encode(
+        DEROutputStream out)
+        throws IOException
+    {
+        if (out instanceof BEROutputStream)
+        {
+            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
+            BEROutputStream         dOut = new BEROutputStream(bOut);
+            Enumeration             e = getObjects();
+
+            while (e.hasMoreElements())
+            {
+                Object    obj = e.nextElement();
+
+                dOut.writeObject(obj);
+            }
+
+            dOut.close();
+
+            byte[]  bytes = bOut.toByteArray();
+
+            out.write(SEQUENCE | CONSTRUCTED);
+            out.write(0x80);
+            out.write(bytes);
+            out.write(0x00);
+            out.write(0x00);
+        }
+        else
+        {
+            super.encode(out);
+        }
+    }
+}