package g2;

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

/* JADX INFO: compiled from: DefiniteLengthInputStream.java */
/* JADX INFO: loaded from: classes.dex */
class r1 extends w1 {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private static final byte[] f4737i = new byte[0];

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final int f4738g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private int f4739h;

    r1(InputStream inputStream, int i3, int i4) {
        super(inputStream, i4);
        if (i3 < 0) {
            throw new IllegalArgumentException("negative lengths not allowed");
        }
        this.f4738g = i3;
        this.f4739h = i3;
        if (i3 == 0) {
            b(true);
        }
    }

    int c() {
        return this.f4739h;
    }

    void d(byte[] bArr) throws IOException {
        int i3 = this.f4739h;
        if (i3 != bArr.length) {
            throw new IllegalArgumentException("buffer length not right for data");
        }
        if (i3 == 0) {
            return;
        }
        int iA = a();
        int i4 = this.f4739h;
        if (i4 >= iA) {
            throw new IOException("corrupted stream - out of bounds length found: " + this.f4739h + " >= " + iA);
        }
        int iC = i4 - k2.a.c(this.f4762e, bArr);
        this.f4739h = iC;
        if (iC == 0) {
            b(true);
            return;
        }
        throw new EOFException("DEF length " + this.f4738g + " object truncated by " + this.f4739h);
    }

    byte[] e() throws IOException {
        if (this.f4739h == 0) {
            return f4737i;
        }
        int iA = a();
        int i3 = this.f4739h;
        if (i3 >= iA) {
            throw new IOException("corrupted stream - out of bounds length found: " + this.f4739h + " >= " + iA);
        }
        byte[] bArr = new byte[i3];
        int iC = i3 - k2.a.c(this.f4762e, bArr);
        this.f4739h = iC;
        if (iC == 0) {
            b(true);
            return bArr;
        }
        throw new EOFException("DEF length " + this.f4738g + " object truncated by " + this.f4739h);
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        if (this.f4739h == 0) {
            return -1;
        }
        int i3 = this.f4762e.read();
        if (i3 >= 0) {
            int i4 = this.f4739h - 1;
            this.f4739h = i4;
            if (i4 == 0) {
                b(true);
            }
            return i3;
        }
        throw new EOFException("DEF length " + this.f4738g + " object truncated by " + this.f4739h);
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i3, int i4) throws IOException {
        int i5 = this.f4739h;
        if (i5 == 0) {
            return -1;
        }
        int i6 = this.f4762e.read(bArr, i3, Math.min(i4, i5));
        if (i6 >= 0) {
            int i7 = this.f4739h - i6;
            this.f4739h = i7;
            if (i7 == 0) {
                b(true);
            }
            return i6;
        }
        throw new EOFException("DEF length " + this.f4738g + " object truncated by " + this.f4739h);
    }
}
