package org.bouncycastle.asn1;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
class IndefiniteLengthInputStream extends org.bouncycastle.asn1.LimitedInputStream {
    private int c;
    private int d;
    private boolean e;
    private boolean f;

    IndefiniteLengthInputStream(java.io.InputStream inputStream, int i) throws java.io.IOException {
        super(inputStream, i);
        this.e = false;
        this.f = true;
        this.c = inputStream.read();
        int i2 = inputStream.read();
        this.d = i2;
        if (i2 < 0) {
            throw new java.io.EOFException();
        }
        e();
    }

    private boolean e() {
        if (!this.e && this.f && this.c == 0 && this.d == 0) {
            this.e = true;
            d(true);
        }
        return this.e;
    }

    void i(boolean z) {
        this.f = z;
        e();
    }

    @Override // java.io.InputStream
    public int read() throws java.io.IOException {
        if (e()) {
            return -1;
        }
        int i = this.a.read();
        if (i < 0) {
            throw new java.io.EOFException();
        }
        int i2 = this.c;
        this.c = this.d;
        this.d = i;
        return i2;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws java.io.IOException {
        if (this.f || i2 < 3) {
            return super.read(bArr, i, i2);
        }
        if (this.e) {
            return -1;
        }
        int i3 = this.a.read(bArr, i + 2, i2 - 2);
        if (i3 < 0) {
            throw new java.io.EOFException();
        }
        bArr[i] = (byte) this.c;
        bArr[i + 1] = (byte) this.d;
        this.c = this.a.read();
        int i4 = this.a.read();
        this.d = i4;
        if (i4 >= 0) {
            return i3 + 2;
        }
        throw new java.io.EOFException();
    }
}
