package o7;

import java.io.IOException;
import java.net.ProtocolException;
import x7.C1731g;
import x7.F;
import x7.n;

/* JADX INFO: loaded from: classes.dex */
public final class b extends n {

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

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

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public b(d dVar, F f3, long j8) {
        super(f3);
        P6.g.e(f3, "delegate");
        this.f15940v = dVar;
        this.f15936r = j8;
    }

    @Override // x7.n, x7.F
    public final void X(C1731g c1731g, long j8) throws IOException {
        P6.g.e(c1731g, "source");
        if (this.f15939u) {
            throw new IllegalStateException("closed");
        }
        long j9 = this.f15936r;
        if (j9 == -1 || this.f15938t + j8 <= j9) {
            try {
                super.X(c1731g, j8);
                this.f15938t += j8;
                return;
            } catch (IOException e8) {
                throw a(e8);
            }
        }
        throw new ProtocolException("expected " + j9 + " bytes but received " + (this.f15938t + j8));
    }

    public final IOException a(IOException iOException) {
        if (this.f15937s) {
            return iOException;
        }
        this.f15937s = true;
        return this.f15940v.a(false, true, iOException);
    }

    @Override // x7.n, x7.F, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        if (this.f15939u) {
            return;
        }
        this.f15939u = true;
        long j8 = this.f15936r;
        if (j8 != -1 && this.f15938t != j8) {
            throw new ProtocolException("unexpected end of stream");
        }
        try {
            super.close();
            a(null);
        } catch (IOException e8) {
            throw a(e8);
        }
    }

    @Override // x7.n, x7.F, java.io.Flushable
    public final void flush() throws IOException {
        try {
            super.flush();
        } catch (IOException e8) {
            throw a(e8);
        }
    }
}
