// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.asn1;

import java.io.IOException;

public class DLSequence extends ASN1Sequence
{
    private int contentsLength;
    
    public DLSequence() {
        this.contentsLength = -1;
    }
    
    public DLSequence(final ASN1Encodable asn1Encodable) {
        super(asn1Encodable);
        this.contentsLength = -1;
    }
    
    public DLSequence(final ASN1Encodable asn1Encodable, final ASN1Encodable asn1Encodable2) {
        super(asn1Encodable, asn1Encodable2);
        this.contentsLength = -1;
    }
    
    public DLSequence(final ASN1EncodableVector asn1EncodableVector) {
        super(asn1EncodableVector);
        this.contentsLength = -1;
    }
    
    public DLSequence(final ASN1Encodable[] array) {
        super(array);
        this.contentsLength = -1;
    }
    
    DLSequence(final ASN1Encodable[] array, final boolean b) {
        super(array, b);
        this.contentsLength = -1;
    }
    
    private int getContentsLength() throws IOException {
        if (this.contentsLength < 0) {
            final int length = this.elements.length;
            int contentsLength = 0;
            for (int i = 0; i < length; ++i) {
                contentsLength += this.elements[i].toASN1Primitive().toDLObject().encodedLength(true);
            }
            this.contentsLength = contentsLength;
        }
        return this.contentsLength;
    }
    
    @Override
    int encodedLength(final boolean b) throws IOException {
        return ASN1OutputStream.getLengthOfEncodingDL(b, this.getContentsLength());
    }
    
    @Override
    void encode(final ASN1OutputStream asn1OutputStream, final boolean b) throws IOException {
        asn1OutputStream.writeIdentifier(b, 48);
        final DLOutputStream dlSubStream = asn1OutputStream.getDLSubStream();
        final int length = this.elements.length;
        if (this.contentsLength >= 0 || length > 16) {
            asn1OutputStream.writeDL(this.getContentsLength());
            for (int i = 0; i < length; ++i) {
                dlSubStream.writePrimitive(this.elements[i].toASN1Primitive(), true);
            }
        }
        else {
            int contentsLength = 0;
            final ASN1Primitive[] array = new ASN1Primitive[length];
            for (int j = 0; j < length; ++j) {
                final ASN1Primitive dlObject = this.elements[j].toASN1Primitive().toDLObject();
                array[j] = dlObject;
                contentsLength += dlObject.encodedLength(true);
            }
            asn1OutputStream.writeDL(this.contentsLength = contentsLength);
            for (int k = 0; k < length; ++k) {
                dlSubStream.writePrimitive(array[k], true);
            }
        }
    }
    
    @Override
    ASN1BitString toASN1BitString() {
        return new DLBitString(BERBitString.flattenBitStrings(this.getConstructedBitStrings()), false);
    }
    
    @Override
    ASN1External toASN1External() {
        return new DLExternal(this);
    }
    
    @Override
    ASN1OctetString toASN1OctetString() {
        return new DEROctetString(BEROctetString.flattenOctetStrings(this.getConstructedOctetStrings()));
    }
    
    @Override
    ASN1Set toASN1Set() {
        return new DLSet(false, this.toArrayInternal());
    }
    
    @Override
    ASN1Primitive toDLObject() {
        return this;
    }
}
