package okhttp3.n0.connection;

import io.netty.handler.codec.http.HttpHeaders;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.Objects;
import kotlin.Metadata;
import kotlin.jvm.internal.o;
import kotlin.reflect.w.internal.y0.n.c2.c;
import okhttp3.EventListener;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.n0.http.ExchangeCodec;
import okhttp3.n0.http.RealResponseBody;
import okio.Buffer;
import okio.ForwardingSink;
import okio.ForwardingSource;
import okio.Sink;
import okio.Source;

/* JADX INFO: renamed from: y.n0.g.c, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000z\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0007\n\u0002\u0010\u000b\n\u0002\b\t\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0005\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0005\u0018\u00002\u00020\u0001:\u0002CDB%\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005\u0012\u0006\u0010\u0006\u001a\u00020\u0007\u0012\u0006\u0010\b\u001a\u00020\t¢\u0006\u0002\u0010\nJ7\u0010\u001e\u001a\u0002H\u001f\"\n\b\u0000\u0010\u001f*\u0004\u0018\u00010 2\u0006\u0010!\u001a\u00020\"2\u0006\u0010#\u001a\u00020\u00162\u0006\u0010$\u001a\u00020\u00162\u0006\u0010%\u001a\u0002H\u001f¢\u0006\u0002\u0010&J\u0006\u0010'\u001a\u00020(J\u0016\u0010)\u001a\u00020*2\u0006\u0010+\u001a\u00020,2\u0006\u0010-\u001a\u00020\u0016J\u0006\u0010.\u001a\u00020(J\u0006\u0010/\u001a\u00020(J\u0006\u00100\u001a\u00020(J\u0006\u00101\u001a\u000202J\u0006\u00103\u001a\u00020(J\u0006\u00104\u001a\u00020(J\u000e\u00105\u001a\u0002062\u0006\u00107\u001a\u000208J\u0010\u00109\u001a\u0004\u0018\u00010:2\u0006\u0010;\u001a\u00020\u0016J\u000e\u0010<\u001a\u00020(2\u0006\u00107\u001a\u000208J\u0006\u0010=\u001a\u00020(J\u0010\u0010>\u001a\u00020(2\u0006\u0010%\u001a\u00020 H\u0002J\u0006\u0010?\u001a\u00020@J\u0006\u0010A\u001a\u00020(J\u000e\u0010B\u001a\u00020(2\u0006\u0010+\u001a\u00020,R\u0014\u0010\u0002\u001a\u00020\u0003X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u000b\u0010\fR\u000e\u0010\b\u001a\u00020\tX\u0082\u0004¢\u0006\u0002\n\u0000R\u0014\u0010\r\u001a\u00020\u000eX\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u000f\u0010\u0010R\u0014\u0010\u0004\u001a\u00020\u0005X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0011\u0010\u0012R\u0014\u0010\u0006\u001a\u00020\u0007X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0013\u0010\u0014R\u001e\u0010\u0017\u001a\u00020\u00162\u0006\u0010\u0015\u001a\u00020\u0016@BX\u0080\u000e¢\u0006\b\n\u0000\u001a\u0004\b\u0018\u0010\u0019R\u0014\u0010\u001a\u001a\u00020\u00168@X\u0080\u0004¢\u0006\u0006\u001a\u0004\b\u001b\u0010\u0019R\u001e\u0010\u001c\u001a\u00020\u00162\u0006\u0010\u0015\u001a\u00020\u0016@BX\u0080\u000e¢\u0006\b\n\u0000\u001a\u0004\b\u001d\u0010\u0019¨\u0006E"}, d2 = {"Lokhttp3/internal/connection/Exchange;", "", "call", "Lokhttp3/internal/connection/RealCall;", "eventListener", "Lokhttp3/EventListener;", "finder", "Lokhttp3/internal/connection/ExchangeFinder;", "codec", "Lokhttp3/internal/http/ExchangeCodec;", "(Lokhttp3/internal/connection/RealCall;Lokhttp3/EventListener;Lokhttp3/internal/connection/ExchangeFinder;Lokhttp3/internal/http/ExchangeCodec;)V", "getCall$okhttp", "()Lokhttp3/internal/connection/RealCall;", "connection", "Lokhttp3/internal/connection/RealConnection;", "getConnection$okhttp", "()Lokhttp3/internal/connection/RealConnection;", "getEventListener$okhttp", "()Lokhttp3/EventListener;", "getFinder$okhttp", "()Lokhttp3/internal/connection/ExchangeFinder;", "<set-?>", "", "hasFailure", "getHasFailure$okhttp", "()Z", "isCoalescedConnection", "isCoalescedConnection$okhttp", "isDuplex", "isDuplex$okhttp", "bodyComplete", "E", "Ljava/io/IOException;", "bytesRead", "", "responseDone", "requestDone", "e", "(JZZLjava/io/IOException;)Ljava/io/IOException;", "cancel", "", "createRequestBody", "Lokio/Sink;", "request", "Lokhttp3/Request;", "duplex", "detachWithViolence", "finishRequest", "flushRequest", "newWebSocketStreams", "Lokhttp3/internal/ws/RealWebSocket$Streams;", "noNewExchangesOnConnection", "noRequestBody", "openResponseBody", "Lokhttp3/ResponseBody;", "response", "Lokhttp3/Response;", "readResponseHeaders", "Lokhttp3/Response$Builder;", "expectContinue", "responseHeadersEnd", "responseHeadersStart", "trackFailure", HttpHeaders.Values.TRAILERS, "Lokhttp3/Headers;", "webSocketUpgradeFailed", "writeRequestHeaders", "RequestBodySink", "ResponseBodySource", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class Exchange {
    public final RealCall a;

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

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

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

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

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

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

    /* JADX INFO: renamed from: y.n0.g.c$a */
    @Metadata(d1 = {"\u00008\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0082\u0004\u0018\u00002\u00020\u0001B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\b\u0010\u000b\u001a\u00020\fH\u0016J!\u0010\r\u001a\u0002H\u000e\"\n\b\u0000\u0010\u000e*\u0004\u0018\u00010\u000f2\u0006\u0010\u0010\u001a\u0002H\u000eH\u0002¢\u0006\u0002\u0010\u0011J\b\u0010\u0012\u001a\u00020\fH\u0016J\u0018\u0010\u0013\u001a\u00020\f2\u0006\u0010\u0014\u001a\u00020\u00152\u0006\u0010\u0016\u001a\u00020\u0005H\u0016R\u000e\u0010\u0007\u001a\u00020\u0005X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u0017"}, d2 = {"Lokhttp3/internal/connection/Exchange$RequestBodySink;", "Lokio/ForwardingSink;", "delegate", "Lokio/Sink;", "contentLength", "", "(Lokhttp3/internal/connection/Exchange;Lokio/Sink;J)V", "bytesReceived", "closed", "", "completed", "close", "", "complete", "E", "Ljava/io/IOException;", "e", "(Ljava/io/IOException;)Ljava/io/IOException;", "flush", "write", "source", "Lokio/Buffer;", "byteCount", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public final class a extends ForwardingSink {

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

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

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

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

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

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public a(Exchange exchange, Sink sink, long j2) {
            super(sink);
            o.f(exchange, "this$0");
            o.f(sink, "delegate");
            this.f9753q = exchange;
            this.f9749m = j2;
        }

        public final <E extends IOException> E a(E e2) {
            if (this.f9750n) {
                return e2;
            }
            this.f9750n = true;
            return (E) this.f9753q.a(this.f9751o, false, true, e2);
        }

        @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
        public void close() throws IOException {
            if (this.f9752p) {
                return;
            }
            this.f9752p = true;
            long j2 = this.f9749m;
            if (j2 != -1 && this.f9751o != j2) {
                throw new ProtocolException("unexpected end of stream");
            }
            try {
                this.f10125f.close();
                a(null);
            } catch (IOException e2) {
                throw a(e2);
            }
        }

        @Override // okio.Sink, java.io.Flushable
        public void flush() throws IOException {
            try {
                this.f10125f.flush();
            } catch (IOException e2) {
                throw a(e2);
            }
        }

        @Override // okio.Sink
        public void write(Buffer buffer, long j2) throws IOException {
            o.f(buffer, "source");
            if (!(!this.f9752p)) {
                throw new IllegalStateException("closed".toString());
            }
            long j3 = this.f9749m;
            if (j3 != -1 && this.f9751o + j2 > j3) {
                StringBuilder sbF = g.a.a.a.a.F("expected ");
                sbF.append(this.f9749m);
                sbF.append(" bytes but received ");
                sbF.append(this.f9751o + j2);
                throw new ProtocolException(sbF.toString());
            }
            try {
                o.f(buffer, "source");
                this.f10125f.write(buffer, j2);
                this.f9751o += j2;
            } catch (IOException e2) {
                throw a(e2);
            }
        }
    }

    /* JADX INFO: renamed from: y.n0.g.c$b */
    @Metadata(d1 = {"\u00008\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0003\n\u0002\u0010\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0080\u0004\u0018\u00002\u00020\u0001B\u0015\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\b\u0010\f\u001a\u00020\rH\u0016J\u001f\u0010\u000e\u001a\u0002H\u000f\"\n\b\u0000\u0010\u000f*\u0004\u0018\u00010\u00102\u0006\u0010\u0011\u001a\u0002H\u000f¢\u0006\u0002\u0010\u0012J\u0018\u0010\u0013\u001a\u00020\u00052\u0006\u0010\u0014\u001a\u00020\u00152\u0006\u0010\u0016\u001a\u00020\u0005H\u0016R\u000e\u0010\u0007\u001a\u00020\u0005X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0004\u001a\u00020\u0005X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u000b\u001a\u00020\tX\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006\u0017"}, d2 = {"Lokhttp3/internal/connection/Exchange$ResponseBodySource;", "Lokio/ForwardingSource;", "delegate", "Lokio/Source;", "contentLength", "", "(Lokhttp3/internal/connection/Exchange;Lokio/Source;J)V", "bytesReceived", "closed", "", "completed", "invokeStartEvent", "close", "", "complete", "E", "Ljava/io/IOException;", "e", "(Ljava/io/IOException;)Ljava/io/IOException;", "read", "sink", "Lokio/Buffer;", "byteCount", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
    public final class b extends ForwardingSource {

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

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

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

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

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

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

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public b(Exchange exchange, Source source, long j2) {
            super(source);
            o.f(exchange, "this$0");
            o.f(source, "delegate");
            this.f9759r = exchange;
            this.f9754m = j2;
            this.f9756o = true;
            if (j2 == 0) {
                a(null);
            }
        }

        public final <E extends IOException> E a(E e2) {
            if (this.f9757p) {
                return e2;
            }
            this.f9757p = true;
            if (e2 == null && this.f9756o) {
                this.f9756o = false;
                Exchange exchange = this.f9759r;
                EventListener eventListener = exchange.f9743b;
                RealCall realCall = exchange.a;
                Objects.requireNonNull(eventListener);
                o.f(realCall, "call");
            }
            return (E) this.f9759r.a(this.f9755n, true, false, e2);
        }

        @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (this.f9758q) {
                return;
            }
            this.f9758q = true;
            try {
                this.f10126f.close();
                a(null);
            } catch (IOException e2) {
                throw a(e2);
            }
        }

        @Override // okio.Source
        public long r(Buffer buffer, long j2) throws IOException {
            o.f(buffer, "sink");
            if (!(!this.f9758q)) {
                throw new IllegalStateException("closed".toString());
            }
            try {
                long jR = this.f10126f.r(buffer, j2);
                if (this.f9756o) {
                    this.f9756o = false;
                    Exchange exchange = this.f9759r;
                    EventListener eventListener = exchange.f9743b;
                    RealCall realCall = exchange.a;
                    Objects.requireNonNull(eventListener);
                    o.f(realCall, "call");
                }
                if (jR == -1) {
                    a(null);
                    return -1L;
                }
                long j3 = this.f9755n + jR;
                long j4 = this.f9754m;
                if (j4 != -1 && j3 > j4) {
                    throw new ProtocolException("expected " + this.f9754m + " bytes but received " + j3);
                }
                this.f9755n = j3;
                if (j3 == j4) {
                    a(null);
                }
                return jR;
            } catch (IOException e2) {
                throw a(e2);
            }
        }
    }

    public Exchange(RealCall realCall, EventListener eventListener, ExchangeFinder exchangeFinder, ExchangeCodec exchangeCodec) {
        o.f(realCall, "call");
        o.f(eventListener, "eventListener");
        o.f(exchangeFinder, "finder");
        o.f(exchangeCodec, "codec");
        this.a = realCall;
        this.f9743b = eventListener;
        this.f9744c = exchangeFinder;
        this.f9745d = exchangeCodec;
        this.f9748g = exchangeCodec.getF9967c();
    }

    public final <E extends IOException> E a(long j2, boolean z2, boolean z3, E e2) {
        if (e2 != null) {
            f(e2);
        }
        if (z3) {
            EventListener eventListener = this.f9743b;
            RealCall realCall = this.a;
            if (e2 != null) {
                eventListener.b(realCall, e2);
            } else {
                Objects.requireNonNull(eventListener);
                o.f(realCall, "call");
            }
        }
        if (z2) {
            if (e2 != null) {
                this.f9743b.c(this.a, e2);
            } else {
                EventListener eventListener2 = this.f9743b;
                RealCall realCall2 = this.a;
                Objects.requireNonNull(eventListener2);
                o.f(realCall2, "call");
            }
        }
        return (E) this.a.m(this, z3, z2, e2);
    }

    public final Sink b(Request request, boolean z2) {
        o.f(request, "request");
        this.f9746e = z2;
        RequestBody requestBody = request.f9623d;
        o.c(requestBody);
        long jContentLength = requestBody.contentLength();
        EventListener eventListener = this.f9743b;
        RealCall realCall = this.a;
        Objects.requireNonNull(eventListener);
        o.f(realCall, "call");
        return new a(this, this.f9745d.h(request, jContentLength), jContentLength);
    }

    public final ResponseBody c(Response response) throws IOException {
        o.f(response, "response");
        try {
            String strA = Response.a(response, "Content-Type", null, 2);
            long jG = this.f9745d.g(response);
            return new RealResponseBody(strA, jG, c.m(new b(this, this.f9745d.c(response), jG)));
        } catch (IOException e2) {
            this.f9743b.c(this.a, e2);
            f(e2);
            throw e2;
        }
    }

    public final Response.a d(boolean z2) throws IOException {
        try {
            Response.a aVarD = this.f9745d.d(z2);
            if (aVarD != null) {
                o.f(this, "deferredTrailers");
                aVarD.f9665m = this;
            }
            return aVarD;
        } catch (IOException e2) {
            this.f9743b.c(this.a, e2);
            f(e2);
            throw e2;
        }
    }

    public final void e() {
        EventListener eventListener = this.f9743b;
        RealCall realCall = this.a;
        Objects.requireNonNull(eventListener);
        o.f(realCall, "call");
    }

    /* JADX WARN: Code restructure failed: missing block: B:9:0x0028, code lost:
    
        if (r6 > 1) goto L10;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void f(java.io.IOException r6) {
        /*
            r5 = this;
            r0 = 1
            r5.f9747f = r0
            y.n0.g.d r1 = r5.f9744c
            r1.c(r6)
            y.n0.h.d r1 = r5.f9745d
            y.n0.g.f r1 = r1.getF9967c()
            y.n0.g.e r2 = r5.a
            monitor-enter(r1)
            java.lang.String r3 = "call"
            kotlin.jvm.internal.o.f(r2, r3)     // Catch: java.lang.Throwable -> L58
            boolean r3 = r6 instanceof okhttp3.n0.http2.StreamResetException     // Catch: java.lang.Throwable -> L58
            if (r3 == 0) goto L3a
            r3 = r6
            y.n0.j.u r3 = (okhttp3.n0.http2.StreamResetException) r3     // Catch: java.lang.Throwable -> L58
            y.n0.j.b r3 = r3.f10022f     // Catch: java.lang.Throwable -> L58
            y.n0.j.b r4 = okhttp3.n0.http2.ErrorCode.REFUSED_STREAM     // Catch: java.lang.Throwable -> L58
            if (r3 != r4) goto L2d
            int r6 = r1.f9800n     // Catch: java.lang.Throwable -> L58
            int r6 = r6 + r0
            r1.f9800n = r6     // Catch: java.lang.Throwable -> L58
            if (r6 <= r0) goto L56
        L2a:
            r1.f9796j = r0     // Catch: java.lang.Throwable -> L58
            goto L51
        L2d:
            y.n0.j.u r6 = (okhttp3.n0.http2.StreamResetException) r6     // Catch: java.lang.Throwable -> L58
            y.n0.j.b r6 = r6.f10022f     // Catch: java.lang.Throwable -> L58
            y.n0.j.b r3 = okhttp3.n0.http2.ErrorCode.CANCEL     // Catch: java.lang.Throwable -> L58
            if (r6 != r3) goto L2a
            boolean r6 = r2.A     // Catch: java.lang.Throwable -> L58
            if (r6 == 0) goto L2a
            goto L56
        L3a:
            boolean r3 = r1.j()     // Catch: java.lang.Throwable -> L58
            if (r3 == 0) goto L44
            boolean r3 = r6 instanceof okhttp3.n0.http2.ConnectionShutdownException     // Catch: java.lang.Throwable -> L58
            if (r3 == 0) goto L56
        L44:
            r1.f9796j = r0     // Catch: java.lang.Throwable -> L58
            int r3 = r1.f9799m     // Catch: java.lang.Throwable -> L58
            if (r3 != 0) goto L56
            y.c0 r2 = r2.f9769f     // Catch: java.lang.Throwable -> L58
            y.l0 r3 = r1.f9788b     // Catch: java.lang.Throwable -> L58
            r1.d(r2, r3, r6)     // Catch: java.lang.Throwable -> L58
        L51:
            int r6 = r1.f9798l     // Catch: java.lang.Throwable -> L58
            int r6 = r6 + r0
            r1.f9798l = r6     // Catch: java.lang.Throwable -> L58
        L56:
            monitor-exit(r1)
            return
        L58:
            r6 = move-exception
            monitor-exit(r1)
            throw r6
        */
        throw new UnsupportedOperationException("Method not decompiled: okhttp3.n0.connection.Exchange.f(java.io.IOException):void");
    }

    public final void g(Request request) throws IOException {
        o.f(request, "request");
        try {
            EventListener eventListener = this.f9743b;
            RealCall realCall = this.a;
            Objects.requireNonNull(eventListener);
            o.f(realCall, "call");
            this.f9745d.b(request);
            EventListener eventListener2 = this.f9743b;
            RealCall realCall2 = this.a;
            Objects.requireNonNull(eventListener2);
            o.f(realCall2, "call");
            o.f(request, "request");
        } catch (IOException e2) {
            this.f9743b.b(this.a, e2);
            f(e2);
            throw e2;
        }
    }
}
