package org.bouncycastle.asn1;

import java.io.IOException;
import java.util.Enumeration;

/**
 * BER TaggedObject - in ASN.1 notation this is any object preceded by
 * a [n] where n is some number - these are assumed to follow the construction
 * rules (as with sequences).
 */
public class BERTaggedObject
    extends DERTaggedObject
{
    /**
     * @param tagNo the tag number for this object.
     * @param obj the tagged object.
     */
    public BERTaggedObject(
        int             tagNo,
        DEREncodable    obj)
    {
        super(tagNo, obj);
    }

    /**
     * @param explicit true if an explicitly tagged object.
     * @param tagNo the tag number for this object.
     * @param obj the tagged object.
     */
    public BERTaggedObject(
        boolean         explicit,
        int             tagNo,
        DEREncodable    obj)
    {
        super(explicit, tagNo, obj);
    }

    /**
     * create an implicitly tagged object that contains a zero
     * length sequence.
     */
    public BERTaggedObject(
        int             tagNo)
    {
        super(false, tagNo, new BERSequence());
    }

    void encode(
        DEROutputStream  out)
        throws IOException
    {
        if (out instanceof ASN1OutputStream || out instanceof BEROutputStream)
        {
            out.writeTag(CONSTRUCTED | TAGGED, tagNo);
            out.write(0x80);

            if (!empty)
            {
                if (!explicit)
                {
                    if (obj instanceof ASN1OctetString)
                    {
                        Enumeration  e;

                        if (obj instanceof BERConstructedOctetString)
                        {
                            e = ((BERConstructedOctetString)obj).getObjects();
                        }
                        else
                        {
                            ASN1OctetString             octs = (ASN1OctetString)obj;
                            BERConstructedOctetString   berO = new BERConstructedOctetString(octs.getOctets());

                            e = berO.getObjects();
                        }

                        while (e.hasMoreElements())
                        {
                            out.writeObject(e.nextElement());
                        }
                    }
                    else if (obj instanceof ASN1Sequence)
                    {
                        Enumeration  e = ((ASN1Sequence)obj).getObjects();

                        while (e.hasMoreElements())
                        {
                            out.writeObject(e.nextElement());
                        }
                    }
                    else if (obj instanceof ASN1Set)
                    {
                        Enumeration  e = ((ASN1Set)obj).getObjects();

                        while (e.hasMoreElements())
                        {
                            out.writeObject(e.nextElement());
                        }
                    }
                    else
                    {
                        throw new RuntimeException("not implemented: " + obj.getClass().getName());
                    }
                }
                else
                {
                    out.writeObject(obj);
                }
            }

            out.write(0x00);
            out.write(0x00);
        }
        else
        {
            super.encode(out);
        }
    }
}

@@ -1457,32 +1457,67 @@
               %7B%0A
+                    Enumeration e;%0A
                 
@@ -1582,49 +1582,8 @@
   %7B%0A
-                        Enumeration  e;%0A%0A
     
@@ -2031,17 +2031,16 @@
 ets());%0A
-%0A
         
@@ -2112,153 +2112,8 @@
   %7D%0A
-%0A                        while (e.hasMoreElements())%0A                        %7B%0A                            out.writeObject(e.nextElement());%0A    
     
@@ -2130,38 +2130,16 @@
       %7D%0A
-                    %7D%0A
         
@@ -2230,37 +2230,24 @@
             
-Enumeration  
 e = ((ASN1Se
@@ -2275,175 +2275,8 @@
 s();
-%0A%0A                        while (e.hasMoreElements())%0A                        %7B%0A                            out.writeObject(e.nextElement());%0A                        %7D
 %0A   
@@ -2397,21 +2397,8 @@
     
-Enumeration  
 e = 
@@ -2426,17 +2426,16 @@
 ects();%0A
-%0A
         
@@ -2450,44 +2450,35 @@
     
+%7D%0A
     
-while (e.hasMoreElements())%0A    
+                else%0A
     
@@ -2523,236 +2523,230 @@
     
-    out.writeObject(e.nextElement());%0A                        %7D%0A                    %7D%0A                    else%0A                    %7B%0A                        throw new RuntimeException(%22not implemented: %22 + obj.getClass().getName
+throw new RuntimeException(%22not implemented: %22 + obj.getClass().getName());%0A                    %7D%0A%0A                    while (e.hasMoreElements())%0A                    %7B%0A                        out.writeObject(e.nextElement
 ());

Factor out common enumeration code