package com.android.org.conscrypt;

import android.compat.annotation.UnsupportedAppUsage;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: loaded from: classes.dex */
class OpenSSLBIOInputStream extends FilterInputStream {
    private long ctx;

    @UnsupportedAppUsage
    OpenSSLBIOInputStream(InputStream inputStream, boolean z2) {
        super(inputStream);
        this.ctx = NativeCrypto.create_BIO_InputStream(this, z2);
    }

    @UnsupportedAppUsage
    long getBioContext() {
        return this.ctx;
    }

    int gets(byte[] bArr) throws IOException {
        int i3;
        int i4 = 0;
        if (bArr != null && bArr.length != 0) {
            while (i4 < bArr.length && (i3 = read()) != -1) {
                if (i3 != 10) {
                    bArr[i4] = (byte) i3;
                    i4++;
                } else if (i4 != 0) {
                    break;
                }
            }
        }
        return i4;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int read(byte[] bArr) throws IOException {
        return read(bArr, 0, bArr.length);
    }

    @UnsupportedAppUsage
    void release() {
        NativeCrypto.BIO_free_all(this.ctx);
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int read(byte[] bArr, int i3, int i4) throws IOException {
        if (i3 < 0 || i4 < 0 || i4 > bArr.length - i3) {
            throw new IndexOutOfBoundsException("Invalid bounds");
        }
        int i5 = 0;
        if (i4 == 0) {
            return 0;
        }
        do {
            int i6 = super.read(bArr, i3 + i5, (i4 - i5) - i3);
            if (i6 == -1) {
                break;
            }
            i5 += i6;
        } while (i3 + i5 < i4);
        if (i5 == 0) {
            return -1;
        }
        return i5;
    }
}
