package q5;

import java.io.IOException;
import kotlin.jvm.internal.o;
import p5.C2740j;
import p5.P;
import p5.w;

/* JADX INFO: loaded from: classes3.dex */
public final class h extends w {

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

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

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

    public h(P p6, long j4, boolean z6) {
        super(p6);
        this.f23761a = j4;
        this.f23762b = z6;
    }

    @Override // p5.w, p5.P
    public final long read(C2740j sink, long j4) throws IOException {
        o.h(sink, "sink");
        long j6 = this.f23763c;
        long j7 = this.f23761a;
        if (j6 > j7) {
            j4 = 0;
        } else if (this.f23762b) {
            long j8 = j7 - j6;
            if (j8 == 0) {
                return -1L;
            }
            j4 = Math.min(j4, j8);
        }
        long j9 = super.read(sink, j4);
        if (j9 != -1) {
            this.f23763c += j9;
        }
        long j10 = this.f23763c;
        if ((j10 >= j7 || j9 != -1) && j10 <= j7) {
            return j9;
        }
        if (j9 > 0 && j10 > j7) {
            long j11 = sink.f23100b - (j10 - j7);
            C2740j c2740j = new C2740j();
            c2740j.q(sink);
            sink.write(c2740j, j11);
            c2740j.a();
        }
        throw new IOException("expected " + j7 + " bytes but got " + this.f23763c);
    }
}
