package okhttp3.internal.connection;

import java.io.IOException;
import java.net.ProtocolException;
import kotlin.jvm.internal.o;
import okhttp3.EventListener;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.internal.http.ExchangeCodec;
import okhttp3.internal.http.RealResponseBody;
import okhttp3.internal.http2.ConnectionShutdownException;
import okhttp3.internal.http2.ErrorCode;
import okhttp3.internal.http2.StreamResetException;
import p5.AbstractC2732b;
import p5.C2740j;
import p5.N;
import p5.P;
import p5.v;
import p5.w;

/* JADX INFO: loaded from: classes3.dex */
public final class Exchange {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ExchangeFinder f22292c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final ExchangeCodec f22293d;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final RealConnection f22295f;

    public final class RequestBodySink extends v {

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

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

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

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

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final /* synthetic */ Exchange f22300e;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public RequestBodySink(Exchange exchange, N delegate, long j4) {
            super(delegate);
            o.h(delegate, "delegate");
            this.f22300e = exchange;
            this.f22296a = j4;
        }

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

        @Override // p5.v, p5.N, java.io.Closeable, java.lang.AutoCloseable
        public final void close() throws IOException {
            if (this.f22299d) {
                return;
            }
            this.f22299d = true;
            long j4 = this.f22296a;
            if (j4 != -1 && this.f22298c != j4) {
                throw new ProtocolException("unexpected end of stream");
            }
            try {
                super.close();
                a(null);
            } catch (IOException e6) {
                throw a(e6);
            }
        }

        @Override // p5.v, p5.N, java.io.Flushable
        public final void flush() throws IOException {
            try {
                super.flush();
            } catch (IOException e6) {
                throw a(e6);
            }
        }

        @Override // p5.v, p5.N
        public final void write(C2740j source, long j4) throws IOException {
            o.h(source, "source");
            if (this.f22299d) {
                throw new IllegalStateException("closed");
            }
            long j6 = this.f22296a;
            if (j6 == -1 || this.f22298c + j4 <= j6) {
                try {
                    super.write(source, j4);
                    this.f22298c += j4;
                    return;
                } catch (IOException e6) {
                    throw a(e6);
                }
            }
            throw new ProtocolException("expected " + j6 + " bytes but received " + (this.f22298c + j4));
        }
    }

    public final class ResponseBodySource extends w {

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

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

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

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

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

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final /* synthetic */ Exchange f22306f;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public ResponseBodySource(Exchange exchange, P delegate, long j4) {
            super(delegate);
            o.h(delegate, "delegate");
            this.f22306f = exchange;
            this.f22301a = j4;
            this.f22303c = true;
            if (j4 == 0) {
                a(null);
            }
        }

        public final IOException a(IOException iOException) {
            if (this.f22304d) {
                return iOException;
            }
            this.f22304d = true;
            Exchange exchange = this.f22306f;
            if (iOException == null && this.f22303c) {
                this.f22303c = false;
                exchange.f22291b.getClass();
                RealCall call = exchange.f22290a;
                o.h(call, "call");
            }
            return exchange.a(true, false, iOException);
        }

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

        @Override // p5.w, p5.P
        public final long read(C2740j sink, long j4) throws IOException {
            o.h(sink, "sink");
            if (this.f22305e) {
                throw new IllegalStateException("closed");
            }
            try {
                long j6 = delegate().read(sink, j4);
                if (this.f22303c) {
                    this.f22303c = false;
                    Exchange exchange = this.f22306f;
                    EventListener eventListener = exchange.f22291b;
                    RealCall call = exchange.f22290a;
                    eventListener.getClass();
                    o.h(call, "call");
                }
                if (j6 == -1) {
                    a(null);
                    return -1L;
                }
                long j7 = this.f22302b + j6;
                long j8 = this.f22301a;
                if (j8 == -1 || j7 <= j8) {
                    this.f22302b = j7;
                    if (j7 == j8) {
                        a(null);
                    }
                    return j6;
                }
                throw new ProtocolException("expected " + j8 + " bytes but received " + j7);
            } catch (IOException e6) {
                throw a(e6);
            }
        }
    }

    public Exchange(RealCall call, EventListener eventListener, ExchangeFinder finder, ExchangeCodec exchangeCodec) {
        o.h(call, "call");
        o.h(eventListener, "eventListener");
        o.h(finder, "finder");
        this.f22290a = call;
        this.f22291b = eventListener;
        this.f22292c = finder;
        this.f22293d = exchangeCodec;
        this.f22295f = exchangeCodec.e();
    }

    public final IOException a(boolean z6, boolean z7, IOException iOException) {
        if (iOException != null) {
            e(iOException);
        }
        EventListener eventListener = this.f22291b;
        RealCall call = this.f22290a;
        if (z7) {
            if (iOException != null) {
                eventListener.getClass();
                o.h(call, "call");
            } else {
                eventListener.getClass();
                o.h(call, "call");
            }
        }
        if (z6) {
            if (iOException != null) {
                eventListener.getClass();
                o.h(call, "call");
            } else {
                eventListener.getClass();
                o.h(call, "call");
            }
        }
        return call.h(this, z7, z6, iOException);
    }

    public final N b(Request request) {
        o.h(request, "request");
        RequestBody requestBody = request.f22128d;
        o.e(requestBody);
        long jA = requestBody.a();
        this.f22291b.getClass();
        RealCall call = this.f22290a;
        o.h(call, "call");
        return new RequestBodySink(this, this.f22293d.h(request, jA), jA);
    }

    public final RealResponseBody c(Response response) throws IOException {
        ExchangeCodec exchangeCodec = this.f22293d;
        try {
            String strB = Response.b("Content-Type", response);
            long jG = exchangeCodec.g(response);
            return new RealResponseBody(strB, jG, AbstractC2732b.c(new ResponseBodySource(this, exchangeCodec.c(response), jG)));
        } catch (IOException e6) {
            this.f22291b.getClass();
            RealCall call = this.f22290a;
            o.h(call, "call");
            e(e6);
            throw e6;
        }
    }

    public final Response.Builder d(boolean z6) throws IOException {
        try {
            Response.Builder builderD = this.f22293d.d(z6);
            if (builderD != null) {
                builderD.f22164m = this;
            }
            return builderD;
        } catch (IOException e6) {
            this.f22291b.getClass();
            RealCall call = this.f22290a;
            o.h(call, "call");
            e(e6);
            throw e6;
        }
    }

    public final void e(IOException iOException) {
        this.f22294e = true;
        this.f22292c.c(iOException);
        RealConnection realConnectionE = this.f22293d.e();
        RealCall call = this.f22290a;
        synchronized (realConnectionE) {
            try {
                o.h(call, "call");
                if (!(iOException instanceof StreamResetException)) {
                    if (!(realConnectionE.f22342g != null) || (iOException instanceof ConnectionShutdownException)) {
                        realConnectionE.f22345j = true;
                        if (realConnectionE.f22347m == 0) {
                            RealConnection.d(call.f22317a, realConnectionE.f22337b, iOException);
                            realConnectionE.l++;
                        }
                    }
                } else if (((StreamResetException) iOException).f22594a == ErrorCode.REFUSED_STREAM) {
                    int i6 = realConnectionE.f22348n + 1;
                    realConnectionE.f22348n = i6;
                    if (i6 > 1) {
                        realConnectionE.f22345j = true;
                        realConnectionE.l++;
                    }
                } else if (((StreamResetException) iOException).f22594a != ErrorCode.CANCEL || !call.f22329q) {
                    realConnectionE.f22345j = true;
                    realConnectionE.l++;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
