001package org.apache.commons.ssl.org.bouncycastle.asn1;
002
003import java.io.IOException;
004import java.util.Enumeration;
005
006/**
007 * Note: this class is for processing DER/DL encoded sequences only.
008 */
009class LazyEncodedSequence
010    extends ASN1Sequence
011{
012    private byte[] encoded;
013
014    LazyEncodedSequence(
015        byte[] encoded)
016        throws IOException
017    {
018        this.encoded = encoded;
019    }
020
021    private void parse()
022    {
023        Enumeration en = new LazyConstructionEnumeration(encoded);
024
025        while (en.hasMoreElements())
026        {
027            seq.addElement(en.nextElement());
028        }
029
030        encoded = null;
031    }
032
033    public synchronized ASN1Encodable getObjectAt(int index)
034    {
035        if (encoded != null)
036        {
037            parse();
038        }
039
040        return super.getObjectAt(index);
041    }
042
043    public synchronized Enumeration getObjects()
044    {
045        if (encoded == null)
046        {
047            return super.getObjects();
048        }
049
050        return new LazyConstructionEnumeration(encoded);
051    }
052
053    public synchronized int size()
054    {
055        if (encoded != null)
056        {
057            parse();
058        }
059
060        return super.size();
061    }
062
063    ASN1Primitive toDERObject()
064    {
065        if (encoded != null)
066        {
067            parse();
068        }
069
070        return super.toDERObject();
071    }
072
073    ASN1Primitive toDLObject()
074    {
075        if (encoded != null)
076        {
077            parse();
078        }
079
080        return super.toDLObject();
081    }
082
083    int encodedLength()
084        throws IOException
085    {
086        if (encoded != null)
087        {
088            return 1 + StreamUtil.calculateBodyLength(encoded.length) + encoded.length;
089        }
090        else
091        {
092            return super.toDLObject().encodedLength();
093        }
094    }
095
096    void encode(
097        ASN1OutputStream out)
098        throws IOException
099    {
100        if (encoded != null)
101        {
102            out.writeEncoded(BERTags.SEQUENCE | BERTags.CONSTRUCTED, encoded);
103        }
104        else
105        {
106            super.toDLObject().encode(out);
107        }
108    }
109}