package org.bouncycastle.asn1;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
class ConstructedBitStream extends java.io.InputStream {
    private final org.bouncycastle.asn1.ASN1StreamParser a;
    private final boolean b;
    private boolean c = true;
    private int d = 0;
    private org.bouncycastle.asn1.ASN1BitStringParser e;
    private java.io.InputStream f;

    ConstructedBitStream(org.bouncycastle.asn1.ASN1StreamParser aSN1StreamParser, boolean z) {
        this.a = aSN1StreamParser;
        this.b = z;
    }

    private org.bouncycastle.asn1.ASN1BitStringParser a() throws java.io.IOException {
        org.bouncycastle.asn1.ASN1Encodable aSN1EncodableI = this.a.i();
        if (aSN1EncodableI == null) {
            if (!this.b || this.d == 0) {
                return null;
            }
            throw new java.io.IOException("expected octet-aligned bitstring, but found padBits: " + this.d);
        }
        if (aSN1EncodableI instanceof org.bouncycastle.asn1.ASN1BitStringParser) {
            if (this.d == 0) {
                return (org.bouncycastle.asn1.ASN1BitStringParser) aSN1EncodableI;
            }
            throw new java.io.IOException("only the last nested bitstring can have padding");
        }
        throw new java.io.IOException("unknown object encountered: " + aSN1EncodableI.getClass());
    }

    int d() {
        return this.d;
    }

    @Override // java.io.InputStream
    public int read() throws java.io.IOException {
        if (this.f == null) {
            if (!this.c) {
                return -1;
            }
            org.bouncycastle.asn1.ASN1BitStringParser aSN1BitStringParserA = a();
            this.e = aSN1BitStringParserA;
            if (aSN1BitStringParserA == null) {
                return -1;
            }
            this.c = false;
            this.f = aSN1BitStringParserA.e();
        }
        while (true) {
            int i = this.f.read();
            if (i >= 0) {
                return i;
            }
            this.d = this.e.i();
            org.bouncycastle.asn1.ASN1BitStringParser aSN1BitStringParserA2 = a();
            this.e = aSN1BitStringParserA2;
            if (aSN1BitStringParserA2 == null) {
                this.f = null;
                return -1;
            }
            this.f = aSN1BitStringParserA2.e();
        }
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws java.io.IOException {
        int i3 = 0;
        if (this.f == null) {
            if (!this.c) {
                return -1;
            }
            org.bouncycastle.asn1.ASN1BitStringParser aSN1BitStringParserA = a();
            this.e = aSN1BitStringParserA;
            if (aSN1BitStringParserA == null) {
                return -1;
            }
            this.c = false;
            this.f = aSN1BitStringParserA.e();
        }
        while (true) {
            int i4 = this.f.read(bArr, i + i3, i2 - i3);
            if (i4 >= 0) {
                i3 += i4;
                if (i3 == i2) {
                    return i3;
                }
            } else {
                this.d = this.e.i();
                org.bouncycastle.asn1.ASN1BitStringParser aSN1BitStringParserA2 = a();
                this.e = aSN1BitStringParserA2;
                if (aSN1BitStringParserA2 == null) {
                    this.f = null;
                    if (i3 < 1) {
                        return -1;
                    }
                    return i3;
                }
                this.f = aSN1BitStringParserA2.e();
            }
        }
    }
}
