FileDocCategorySizeDatePackage
BERConstructedOctetString.javaAPI DocAndroid 1.5 API4114Wed May 06 22:41:06 BST 2009org.bouncycastle.asn1

BERConstructedOctetString

public class BERConstructedOctetString extends DEROctetString

Fields Summary
private Vector
octs
Constructors Summary
public BERConstructedOctetString(byte[] string)

param
string the octets making up the octet 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());
    
Methods Summary
public voidencode(DEROutputStream out)

        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
            {
                int     start = 0;
                int     end = 0;

                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);

                        out.writeObject(new DEROctetString(nStr));
                        start = end + 1;
                    }
                    end++;
                }

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

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

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

            out.write(0x00);
            out.write(0x00);
        }
        else
        {
            super.encode(out);
        }
    
private java.util.VectorgenerateOcts()

        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 java.util.EnumerationgetObjects()
return the DER octets that make up this string.

        if (octs == null)
        {
            return generateOcts().elements();
        }

        return octs.elements();
    
public byte[]getOctets()

        return string;
    
private static byte[]toBytes(java.util.Vector octs)
convert a vector of octet strings into a single byte string

        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();