package o7;

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

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

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

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

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public boolean f15944u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public boolean f15945v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final /* synthetic */ d f15946w;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public c(d dVar, H h8, long j8) {
        super(h8);
        P6.g.e(h8, "delegate");
        this.f15946w = dVar;
        this.f15941r = j8;
        this.f15943t = true;
        if (j8 == 0) {
            a(null);
        }
    }

    @Override // x7.o, x7.H
    public final long M(C1731g c1731g, long j8) throws IOException {
        P6.g.e(c1731g, "sink");
        if (this.f15945v) {
            throw new IllegalStateException("closed");
        }
        try {
            long jM = this.f18778q.M(c1731g, j8);
            if (this.f15943t) {
                this.f15943t = false;
                d dVar = this.f15946w;
                dVar.getClass();
                P6.g.e(dVar.f15947a, "call");
            }
            if (jM == -1) {
                a(null);
                return -1L;
            }
            long j9 = this.f15942s + jM;
            long j10 = this.f15941r;
            if (j10 == -1 || j9 <= j10) {
                this.f15942s = j9;
                if (j9 == j10) {
                    a(null);
                }
                return jM;
            }
            throw new ProtocolException("expected " + j10 + " bytes but received " + j9);
        } catch (IOException e8) {
            throw a(e8);
        }
    }

    public final IOException a(IOException iOException) {
        if (this.f15944u) {
            return iOException;
        }
        this.f15944u = true;
        d dVar = this.f15946w;
        if (iOException == null && this.f15943t) {
            this.f15943t = false;
            dVar.getClass();
            P6.g.e(dVar.f15947a, "call");
        }
        return dVar.a(true, false, iOException);
    }

    @Override // x7.o, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        if (this.f15945v) {
            return;
        }
        this.f15945v = true;
        try {
            super.close();
            a(null);
        } catch (IOException e8) {
            throw a(e8);
        }
    }
}
