package org.bouncycastle.asn1;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class DERSet extends org.bouncycastle.asn1.ASN1Set {
    private int d;

    public DERSet() {
        this.d = -1;
    }

    private static boolean G(boolean z) {
        if (z) {
            return z;
        }
        throw new java.lang.IllegalStateException("DERSet elements should always be in sorted order");
    }

    private int H() {
        if (this.d < 0) {
            int length = this.a.length;
            int iR = 0;
            for (int i = 0; i < length; i++) {
                iR += this.a[i].j().v().r(true);
            }
            this.d = iR;
        }
        return this.d;
    }

    @Override // org.bouncycastle.asn1.ASN1Primitive
    void o(org.bouncycastle.asn1.ASN1OutputStream aSN1OutputStream, boolean z) throws java.io.IOException {
        aSN1OutputStream.s(z, 49);
        org.bouncycastle.asn1.DEROutputStream dEROutputStreamD = aSN1OutputStream.d();
        int length = this.a.length;
        int i = 0;
        if (this.d >= 0 || length > 16) {
            aSN1OutputStream.k(H());
            while (i < length) {
                this.a[i].j().v().o(dEROutputStreamD, true);
                i++;
            }
            return;
        }
        org.bouncycastle.asn1.ASN1Primitive[] aSN1PrimitiveArr = new org.bouncycastle.asn1.ASN1Primitive[length];
        int iR = 0;
        for (int i2 = 0; i2 < length; i2++) {
            org.bouncycastle.asn1.ASN1Primitive aSN1PrimitiveV = this.a[i2].j().v();
            aSN1PrimitiveArr[i2] = aSN1PrimitiveV;
            iR += aSN1PrimitiveV.r(true);
        }
        this.d = iR;
        aSN1OutputStream.k(iR);
        while (i < length) {
            aSN1PrimitiveArr[i].o(dEROutputStreamD, true);
            i++;
        }
    }

    @Override // org.bouncycastle.asn1.ASN1Primitive
    int r(boolean z) {
        return org.bouncycastle.asn1.ASN1OutputStream.g(z, H());
    }

    @Override // org.bouncycastle.asn1.ASN1Set, org.bouncycastle.asn1.ASN1Primitive
    org.bouncycastle.asn1.ASN1Primitive v() {
        return this.b != null ? this : super.v();
    }

    public DERSet(org.bouncycastle.asn1.ASN1Encodable aSN1Encodable) {
        super(aSN1Encodable);
        this.d = -1;
    }

    public DERSet(org.bouncycastle.asn1.ASN1EncodableVector aSN1EncodableVector) {
        super(aSN1EncodableVector, true);
        this.d = -1;
    }

    DERSet(boolean z, org.bouncycastle.asn1.ASN1Encodable[] aSN1EncodableArr) {
        super(G(z), aSN1EncodableArr);
        this.d = -1;
    }

    public DERSet(org.bouncycastle.asn1.ASN1Encodable[] aSN1EncodableArr) {
        super(aSN1EncodableArr, true);
        this.d = -1;
    }

    @Override // org.bouncycastle.asn1.ASN1Set, org.bouncycastle.asn1.ASN1Primitive
    org.bouncycastle.asn1.ASN1Primitive w() {
        return this;
    }
}
