package org.bouncycastle.cms;

import java.io.IOException;
import java.io.InputStream;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public final class InputStreamWithMAC extends InputStream {
    private final InputStream base;
    private boolean baseFinished = false;
    private int index = 0;
    private byte[] mac;
    private MACProvider macProvider;

    InputStreamWithMAC(InputStream inputStream, MACProvider mACProvider) {
        this.base = inputStream;
        this.macProvider = mACProvider;
    }

    public InputStreamWithMAC(InputStream inputStream, byte[] bArr) {
        this.base = inputStream;
        this.mac = bArr;
    }

    public byte[] getMAC() {
        if (this.baseFinished) {
            return Arrays.clone(this.mac);
        }
        throw new IllegalStateException("input stream not fully processed");
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        byte b;
        if (!this.baseFinished) {
            int i = this.base.read();
            if (i >= 0) {
                return i;
            }
            this.baseFinished = true;
            if (this.macProvider != null) {
                this.macProvider.init();
                this.mac = this.macProvider.getMAC();
            }
            byte[] bArr = this.mac;
            int i2 = this.index;
            this.index = i2 + 1;
            b = bArr[i2];
        } else {
            if (this.index >= this.mac.length) {
                return -1;
            }
            byte[] bArr2 = this.mac;
            int i3 = this.index;
            this.index = i3 + 1;
            b = bArr2[i3];
        }
        return b & 255;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws IOException {
        if (bArr == null) {
            throw new NullPointerException("input array is null");
        }
        if (i < 0 || bArr.length < i + i2) {
            throw new IndexOutOfBoundsException("invalid off(" + i + ") and len(" + i2 + ")");
        }
        if (!this.baseFinished) {
            int i3 = this.base.read(bArr, i, i2);
            if (i3 >= 0) {
                return i3;
            }
            this.baseFinished = true;
            if (this.macProvider != null) {
                this.macProvider.init();
                this.mac = this.macProvider.getMAC();
            }
            int length = this.mac.length;
            byte[] bArr2 = this.mac;
            if (i2 >= length) {
                System.arraycopy(bArr2, 0, bArr, i, this.mac.length);
                this.index = this.mac.length;
                return this.mac.length;
            }
            System.arraycopy(bArr2, 0, bArr, i, i2);
        } else {
            if (this.index >= this.mac.length) {
                return -1;
            }
            int length2 = this.mac.length - this.index;
            byte[] bArr3 = this.mac;
            if (i2 >= length2) {
                System.arraycopy(bArr3, this.index, bArr, i, this.mac.length - this.index);
                int length3 = this.mac.length - this.index;
                this.index = this.mac.length;
                return length3;
            }
            System.arraycopy(bArr3, this.index, bArr, i, i2);
        }
        this.index += i2;
        return i2;
    }
}
