package org.bouncycastle.cms;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public final class InputStreamWithMAC extends java.io.InputStream {
    private final java.io.InputStream a;
    private org.bouncycastle.cms.MACProvider b;
    private byte[] c;
    private boolean d;
    private int e;

    @Override // java.io.InputStream
    public int read() throws java.io.IOException {
        byte b;
        if (this.d) {
            int i = this.e;
            byte[] bArr = this.c;
            if (i >= bArr.length) {
                return -1;
            }
            this.e = i + 1;
            b = bArr[i];
        } else {
            int i2 = this.a.read();
            if (i2 >= 0) {
                return i2;
            }
            this.d = true;
            org.bouncycastle.cms.MACProvider mACProvider = this.b;
            if (mACProvider != null) {
                mACProvider.a();
                this.c = this.b.c();
            }
            byte[] bArr2 = this.c;
            int i3 = this.e;
            this.e = i3 + 1;
            b = bArr2[i3];
        }
        return b & 255;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws java.io.IOException {
        if (bArr == null) {
            throw new java.lang.NullPointerException("input array is null");
        }
        if (i < 0 || bArr.length < i + i2) {
            throw new java.lang.IndexOutOfBoundsException("invalid off(" + i + ") and len(" + i2 + ")");
        }
        if (this.d) {
            int i3 = this.e;
            byte[] bArr2 = this.c;
            if (i3 >= bArr2.length) {
                return -1;
            }
            if (i2 >= bArr2.length - i3) {
                java.lang.System.arraycopy(bArr2, i3, bArr, i, bArr2.length - i3);
                byte[] bArr3 = this.c;
                int length = bArr3.length - this.e;
                this.e = bArr3.length;
                return length;
            }
            java.lang.System.arraycopy(bArr2, i3, bArr, i, i2);
        } else {
            int i4 = this.a.read(bArr, i, i2);
            if (i4 >= 0) {
                return i4;
            }
            this.d = true;
            org.bouncycastle.cms.MACProvider mACProvider = this.b;
            if (mACProvider != null) {
                mACProvider.a();
                this.c = this.b.c();
            }
            byte[] bArr4 = this.c;
            if (i2 >= bArr4.length) {
                java.lang.System.arraycopy(bArr4, 0, bArr, i, bArr4.length);
                byte[] bArr5 = this.c;
                this.e = bArr5.length;
                return bArr5.length;
            }
            java.lang.System.arraycopy(bArr4, 0, bArr, i, i2);
        }
        this.e += i2;
        return i2;
    }
}
