package y7;

import java.io.IOException;
import x7.C1731g;
import x7.H;
import x7.o;

/* JADX INFO: loaded from: classes.dex */
public final class d extends o {

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final long f19046r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final boolean f19047s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public long f19048t;

    public d(H h8, long j8, boolean z8) {
        super(h8);
        this.f19046r = j8;
        this.f19047s = z8;
    }

    @Override // x7.o, x7.H
    public final long M(C1731g c1731g, long j8) throws IOException {
        P6.g.e(c1731g, "sink");
        long j9 = this.f19048t;
        long j10 = this.f19046r;
        if (j9 > j10) {
            j8 = 0;
        } else if (this.f19047s) {
            long j11 = j10 - j9;
            if (j11 == 0) {
                return -1L;
            }
            j8 = Math.min(j8, j11);
        }
        long jM = super.M(c1731g, j8);
        if (jM != -1) {
            this.f19048t += jM;
        }
        long j12 = this.f19048t;
        if ((j12 >= j10 || jM != -1) && j12 <= j10) {
            return jM;
        }
        if (jM > 0 && j12 > j10) {
            long j13 = c1731g.f18765r - (j12 - j10);
            C1731g c1731g2 = new C1731g();
            c1731g2.t0(c1731g);
            c1731g.X(c1731g2, j13);
            c1731g2.m();
        }
        throw new IOException("expected " + j10 + " bytes but got " + this.f19048t);
    }
}
