001package org.apache.commons.ssl.org.bouncycastle.asn1;
002
003import java.io.ByteArrayOutputStream;
004import java.io.IOException;
005import java.util.Enumeration;
006import java.util.Vector;
007
008/**
009 * @deprecated use BEROctetString
010 */
011public class BERConstructedOctetString
012    extends BEROctetString
013{
014    private static final int MAX_LENGTH = 1000;
015
016    /**
017     * convert a vector of octet strings into a single byte string
018     */
019    static private byte[] toBytes(
020        Vector  octs)
021    {
022        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
023
024        for (int i = 0; i != octs.size(); i++)
025        {
026            try
027            {
028                DEROctetString  o = (DEROctetString)octs.elementAt(i);
029
030                bOut.write(o.getOctets());
031            }
032            catch (ClassCastException e)
033            {
034                throw new IllegalArgumentException(octs.elementAt(i).getClass().getName() + " found in input should only contain DEROctetString");
035            }
036            catch (IOException e)
037            {
038                throw new IllegalArgumentException("exception converting octets " + e.toString());
039            }
040        }
041
042        return bOut.toByteArray();
043    }
044
045    private Vector  octs;
046
047    /**
048     * @param string the octets making up the octet string.
049     */
050    public BERConstructedOctetString(
051        byte[]  string)
052    {
053        super(string);
054    }
055
056    public BERConstructedOctetString(
057        Vector  octs)
058    {
059        super(toBytes(octs));
060
061        this.octs = octs;
062    }
063
064    public BERConstructedOctetString(
065        ASN1Primitive  obj)
066    {
067        super(toByteArray(obj));
068    }
069
070    private static byte[] toByteArray(ASN1Primitive obj)
071    {
072        try
073        {
074            return obj.getEncoded();
075        }
076        catch (IOException e)
077        {
078            throw new IllegalArgumentException("Unable to encode object");
079        }
080    }
081
082    public BERConstructedOctetString(
083        ASN1Encodable  obj)
084    {
085        this(obj.toASN1Primitive());
086    }
087
088    public byte[] getOctets()
089    {
090        return string;
091    }
092
093    /**
094     * return the DER octets that make up this string.
095     */
096    public Enumeration getObjects()
097    {
098        if (octs == null)
099        {
100            return generateOcts().elements();
101        }
102
103        return octs.elements();
104    }
105
106    private Vector generateOcts() 
107    { 
108        Vector vec = new Vector(); 
109        for (int i = 0; i < string.length; i += MAX_LENGTH) 
110        { 
111            int end; 
112
113            if (i + MAX_LENGTH > string.length) 
114            { 
115                end = string.length; 
116            } 
117            else 
118            { 
119                end = i + MAX_LENGTH; 
120            } 
121
122            byte[] nStr = new byte[end - i]; 
123
124            System.arraycopy(string, i, nStr, 0, nStr.length); 
125
126            vec.addElement(new DEROctetString(nStr)); 
127         } 
128        
129         return vec; 
130    }
131
132    public static BEROctetString fromSequence(ASN1Sequence seq)
133    {
134        Vector      v = new Vector();
135        Enumeration e = seq.getObjects();
136
137        while (e.hasMoreElements())
138        {
139            v.addElement(e.nextElement());
140        }
141
142        return new BERConstructedOctetString(v);
143    }
144}