package org.bouncycastle.openpgp.api;

import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: loaded from: classes2.dex */
public class DoubleBufferedInputStream<I extends InputStream> extends InputStream {
    private static final int BUFFER_SIZE = 33554432;
    private int b1Max;
    private int b1Pos;
    private int b2Max;
    private byte[] buf1;
    private byte[] buf2;
    private boolean closed;
    private final I in;

    public DoubleBufferedInputStream(I i) {
        this(i, BUFFER_SIZE);
    }

    public DoubleBufferedInputStream(I i, int i2) {
        this.closed = false;
        if (i2 <= 0) {
            throw new IllegalArgumentException("Buffer size cannot be zero nor negative.");
        }
        this.buf1 = new byte[i2];
        this.buf2 = new byte[i2];
        this.in = i;
        this.b1Pos = -1;
    }

    private void fill() throws IOException {
        int i;
        if (this.b1Pos == -1) {
            this.b1Max = this.in.read(this.buf1);
            this.b2Max = this.in.read(this.buf2);
            if (this.b2Max == -1) {
                close();
            }
            this.b1Pos = 0;
            return;
        }
        if (this.b1Max > 0 && this.b1Pos == this.b1Max) {
            byte[] bArr = this.buf1;
            this.buf1 = this.buf2;
            this.buf2 = bArr;
            this.b1Max = this.b2Max;
            this.b1Pos = 0;
            try {
                this.b2Max = this.in.read(this.buf2);
                if (this.b2Max != this.buf2.length && (i = this.in.read()) != -1) {
                    byte[] bArr2 = this.buf2;
                    int i2 = this.b2Max;
                    this.b2Max = i2 + 1;
                    bArr2[i2] = (byte) i;
                }
                if (this.b2Max == -1) {
                    close();
                }
            } catch (IOException e) {
                this.b1Max = -1;
                this.b2Max = -1;
                close();
                throw e;
            }
        }
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (this.closed) {
            return;
        }
        this.closed = true;
        this.in.close();
    }

    public I getInputStream() {
        return this.in;
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        fill();
        if (this.b1Max == -1) {
            close();
            return -1;
        }
        byte[] bArr = this.buf1;
        int i = this.b1Pos;
        this.b1Pos = i + 1;
        return bArr[i];
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws IOException {
        fill();
        if (this.b1Max == -1) {
            close();
            return -1;
        }
        int iMin = Math.min(this.b1Max - this.b1Pos, i2);
        System.arraycopy(this.buf1, this.b1Pos, bArr, i, iMin);
        this.b1Pos += iMin;
        return iMin;
    }
}
