package org.bouncycastle.asn1;

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

public class BERConstructedOctetString
    extends DEROctetString
{
    private static final int MAX_LENGTH = 1000;

    /**
     * convert a vector of octet strings into a single byte string
     */
    static private byte[] toBytes(
        Vector  octs)
    {
        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();

        for (int i = 0; i != octs.size(); i++)
        {
            try
            {
                DEROctetString  o = (DEROctetString)octs.elementAt(i);

                bOut.write(o.getOctets());
            }
            catch (ClassCastException e)
            {
                throw new IllegalArgumentException(octs.elementAt(i).getClass().getName() + " found in input should only contain DEROctetString");
            }
            catch (IOException e)
            {
                throw new IllegalArgumentException("exception converting octets " + e.toString());
            }
        }

        return bOut.toByteArray();
    }

    private Vector  octs;

    /**
     * @param string the octets making up the octet string.
     */
    public BERConstructedOctetString(
        byte[]  string)
    {
        super(string);
    }

    public BERConstructedOctetString(
        Vector  octs)
    {
        super(toBytes(octs));

        this.octs = octs;
    }

    public BERConstructedOctetString(
        DERObject  obj)
    {
        super(obj);
    }

    public BERConstructedOctetString(
        DEREncodable  obj)
    {
        super(obj.getDERObject());
    }

    public byte[] getOctets()
    {
        return string;
    }

    /**
     * return the DER octets that make up this string.
     */
    public Enumeration getObjects()
    {
        if (octs == null)
        {
            return generateOcts().elements();
        }

        return octs.elements();
    }

    private Vector generateOcts()
    {
        int     start = 0;
        int     end = 0;
        Vector  vec = new Vector();

        while ((end + 1) < string.length)
        {
            if (string[end] == 0 && string[end + 1] == 0)
            {
                byte[]  nStr = new byte[end - start + 1];

                System.arraycopy(string, start, nStr, 0, nStr.length);

                vec.addElement(new DEROctetString(nStr));
                start = end + 1;
            }
            end++;
        }

        byte[]  nStr = new byte[string.length - start];

        System.arraycopy(string, start, nStr, 0, nStr.length);

        vec.addElement(new DEROctetString(nStr));

        return vec;
    }

    public void encode(
        DEROutputStream out)
        throws IOException
    {
        if (out instanceof ASN1OutputStream || out instanceof BEROutputStream)
        {
            out.write(CONSTRUCTED | OCTET_STRING);

            out.write(0x80);

            //
            // write out the octet array
            //
            if (octs != null)
            {
                for (int i = 0; i != octs.size(); i++)
                {
                    out.writeObject(octs.elementAt(i));
                }
            }
            else
            {
                for (int i = 0; i < string.length; i += MAX_LENGTH)
                {
                    int end;

                    if (i + MAX_LENGTH > string.length)
                    {
                        end = string.length;
                    }
                    else
                    {
                        end = i + MAX_LENGTH;
                    }

                    byte[]  nStr = new byte[end - i];

                    System.arraycopy(string, i, nStr, 0, nStr.length);

                    out.writeObject(new DEROctetString(nStr));
                }
            }

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

@@ -2012,66 +2012,16 @@
 ts()
-%0A    %7B%0A        int     start = 0;%0A        int     end = 0;
+ %0A    %7B 
 %0A   
@@ -2032,17 +2032,16 @@
  Vector 
- 
 vec = ne
@@ -2051,17 +2051,17 @@
 ector();
-%0A
+ 
 %0A       
@@ -2065,41 +2065,60 @@
     
-while ((end + 1) %3C string.length)
+for (int i = 0; i %3C string.length; i += MAX_LENGTH) 
 %0A   
@@ -2115,32 +2115,33 @@
 NGTH) %0A        %7B
+ 
 %0A            if 
@@ -2142,197 +2142,119 @@
    i
-f (string%5Bend%5D == 0 && string%5Bend + 1%5D == 0)%0A      
+nt end; %0A%0A
       
-%7B%0A
       
-          byte%5B%5D  nStr = new byte%5Bend - start + 1%5D;%0A%0A                System.arraycopy(string, start, nStr, 0, nStr
+if (i + MAX_LENGTH %3E string.length) %0A            %7B %0A                end = string
 .length
-);%0A
+; 
 %0A   
@@ -2266,86 +2266,43 @@
     
+%7D %0A
     
-vec.addElement(new DEROctetString(nStr));%0A                start = end + 1;
+        else %0A            %7B 
 %0A   
@@ -2314,29 +2314,39 @@
     
-%7D%0A
     
-        end++;%0A
+end = i + MAX_LENGTH; %0A    
     
@@ -2342,32 +2342,33 @@
 ; %0A            %7D
+ 
 %0A%0A        byte%5B%5D
@@ -2353,39 +2353,42 @@
     %7D %0A%0A        
+    
 byte%5B%5D
- 
  nStr = new byte
@@ -2392,33 +2392,24 @@
 yte%5B
-string.length - start
+end - i
 %5D;
+ 
 %0A%0A
+    
     
@@ -2441,13 +2441,9 @@
 ng, 
-start
+i
 , nS
@@ -2454,34 +2454,39 @@
 0, nStr.length);
+ 
 %0A%0A
+    
         vec.addE
@@ -2518,18 +2518,40 @@
 (nStr));
-%0A%0A
+ %0A         %7D %0A        %0A 
         
@@ -2561,16 +2561,17 @@
 urn vec;
+ 
 %0A    %7D%0A%0A
@@ -2914,520 +2914,78 @@
     
-if (octs != null)%0A            %7B%0A                for (int i = 0; i != octs.size(); i++)%0A                %7B%0A                    out.writeObject(octs.elementAt(i));%0A                %7D%0A            %7D%0A            else%0A            %7B%0A                for (int i = 0; i %3C string.length; i += MAX_LENGTH)%0A                %7B%0A                    int end;%0A%0A                    if (i + MAX_LENGTH %3E string.length)%0A                    %7B%0A                        end = string.length;%0A                    %7D%0A                    else%0A  
+Enumeration e = getObjects();%0A            while (e.hasMoreElements())%0A
     
@@ -2988,30 +2988,24 @@
             
-      
 %7B%0A          
@@ -3014,268 +3014,41 @@
     
-        end = i + MAX_LENGTH;%0A                    %7D%0A%0A                    byte%5B%5D  nStr = new byte%5Bend - i%5D;%0A%0A                    System.arraycopy(string, i, nStr, 0, nStr.length);%0A%0A                    out.writeObject(new DEROctetString(nStr));%0A                %7D
+out.writeObject(e.nextElement());
 %0A   

Bring generateOcts into line with encode, and refactor