package okhttp3.internal.connection;

import c.q.d.g;
import c.q.d.j;
import d.a0;
import d.f;
import d.k;
import d.p;
import d.y;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.SocketException;
import okhttp3.Call;
import okhttp3.EventListener;
import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.http.ExchangeCodec;
import okhttp3.internal.http.RealResponseBody;
import okhttp3.internal.ws.RealWebSocket;

/* JADX INFO: compiled from: Exchange.kt */
/* JADX INFO: loaded from: classes.dex */
public final class Exchange {
    public static final Companion Companion = new Companion(null);
    private final Call call;
    private final ExchangeCodec codec;
    private final EventListener eventListener;
    private final ExchangeFinder finder;
    private boolean isDuplex;
    private final Transmitter transmitter;

    /* JADX INFO: compiled from: Exchange.kt */
    public static final class Companion {
        private Companion() {
        }

        public final Exchange get(Response response) {
            j.c(response, "response");
            return response.exchange();
        }

        public /* synthetic */ Companion(g gVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: Exchange.kt */
    private final class RequestBodySink extends d.j {
        private long bytesReceived;
        private boolean closed;
        private boolean completed;
        private final long contentLength;
        final /* synthetic */ Exchange this$0;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public RequestBodySink(Exchange exchange, y yVar, long j) {
            super(yVar);
            j.c(yVar, "delegate");
            this.this$0 = exchange;
            this.contentLength = j;
        }

        private final <E extends IOException> E complete(E e2) {
            if (this.completed) {
                return e2;
            }
            this.completed = true;
            return (E) this.this$0.bodyComplete(this.bytesReceived, false, true, e2);
        }

        @Override // d.j, d.y, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (this.closed) {
                return;
            }
            this.closed = true;
            long j = this.contentLength;
            if (j != -1 && this.bytesReceived != j) {
                throw new ProtocolException("unexpected end of stream");
            }
            try {
                super.close();
                complete(null);
            } catch (IOException e2) {
                throw complete(e2);
            }
        }

        @Override // d.j, d.y, java.io.Flushable
        public void flush() throws IOException {
            try {
                super.flush();
            } catch (IOException e2) {
                throw complete(e2);
            }
        }

        @Override // d.j, d.y
        public void write(f fVar, long j) throws IOException {
            j.c(fVar, "source");
            if (!(!this.closed)) {
                throw new IllegalStateException("closed".toString());
            }
            long j2 = this.contentLength;
            if (j2 == -1 || this.bytesReceived + j <= j2) {
                try {
                    super.write(fVar, j);
                    this.bytesReceived += j;
                    return;
                } catch (IOException e2) {
                    throw complete(e2);
                }
            }
            throw new ProtocolException("expected " + this.contentLength + " bytes but received " + (this.bytesReceived + j));
        }
    }

    /* JADX INFO: compiled from: Exchange.kt */
    public final class ResponseBodySource extends k {
        private long bytesReceived;
        private boolean closed;
        private boolean completed;
        private final long contentLength;
        final /* synthetic */ Exchange this$0;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public ResponseBodySource(Exchange exchange, a0 a0Var, long j) {
            super(a0Var);
            j.c(a0Var, "delegate");
            this.this$0 = exchange;
            this.contentLength = j;
            if (j == 0) {
                complete(null);
            }
        }

        @Override // d.k, d.a0, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            if (this.closed) {
                return;
            }
            this.closed = true;
            try {
                super.close();
                complete(null);
            } catch (IOException e2) {
                throw complete(e2);
            }
        }

        public final <E extends IOException> E complete(E e2) {
            if (this.completed) {
                return e2;
            }
            this.completed = true;
            return (E) this.this$0.bodyComplete(this.bytesReceived, true, false, e2);
        }

        @Override // d.k, d.a0
        public long read(f fVar, long j) throws IOException {
            j.c(fVar, "sink");
            if (!(!this.closed)) {
                throw new IllegalStateException("closed".toString());
            }
            try {
                long j2 = delegate().read(fVar, j);
                if (j2 == -1) {
                    complete(null);
                    return -1L;
                }
                long j3 = this.bytesReceived + j2;
                if (this.contentLength != -1 && j3 > this.contentLength) {
                    throw new ProtocolException("expected " + this.contentLength + " bytes but received " + j3);
                }
                this.bytesReceived = j3;
                if (j3 == this.contentLength) {
                    complete(null);
                }
                return j2;
            } catch (IOException e2) {
                throw complete(e2);
            }
        }
    }

    public Exchange(Transmitter transmitter, Call call, EventListener eventListener, ExchangeFinder exchangeFinder, ExchangeCodec exchangeCodec) {
        j.c(transmitter, "transmitter");
        j.c(call, "call");
        j.c(eventListener, "eventListener");
        j.c(exchangeFinder, "finder");
        j.c(exchangeCodec, "codec");
        this.transmitter = transmitter;
        this.call = call;
        this.eventListener = eventListener;
        this.finder = exchangeFinder;
        this.codec = exchangeCodec;
    }

    private final void trackFailure(IOException iOException) {
        this.finder.trackFailure();
        RealConnection realConnectionConnection = this.codec.connection();
        if (realConnectionConnection != null) {
            realConnectionConnection.trackFailure$okhttp(iOException);
        } else {
            j.g();
            throw null;
        }
    }

    public final <E extends IOException> E bodyComplete(long j, boolean z, boolean z2, E e2) {
        if (e2 != null) {
            trackFailure(e2);
        }
        if (z2) {
            if (e2 != null) {
                this.eventListener.requestFailed(this.call, e2);
            } else {
                this.eventListener.requestBodyEnd(this.call, j);
            }
        }
        if (z) {
            if (e2 != null) {
                this.eventListener.responseFailed(this.call, e2);
            } else {
                this.eventListener.responseBodyEnd(this.call, j);
            }
        }
        return (E) this.transmitter.exchangeMessageDone$okhttp(this, z2, z, e2);
    }

    public final void cancel() {
        this.codec.cancel();
    }

    public final RealConnection connection() {
        return this.codec.connection();
    }

    public final y createRequestBody(Request request, boolean z) throws IOException {
        j.c(request, "request");
        this.isDuplex = z;
        RequestBody requestBodyBody = request.body();
        if (requestBodyBody == null) {
            j.g();
            throw null;
        }
        long jContentLength = requestBodyBody.contentLength();
        this.eventListener.requestBodyStart(this.call);
        return new RequestBodySink(this, this.codec.createRequestBody(request, jContentLength), jContentLength);
    }

    public final void detachWithViolence() {
        this.codec.cancel();
        this.transmitter.exchangeMessageDone$okhttp(this, true, true, null);
    }

    public final void finishRequest() throws IOException {
        try {
            this.codec.finishRequest();
        } catch (IOException e2) {
            this.eventListener.requestFailed(this.call, e2);
            trackFailure(e2);
            throw e2;
        }
    }

    public final void flushRequest() throws IOException {
        try {
            this.codec.flushRequest();
        } catch (IOException e2) {
            this.eventListener.requestFailed(this.call, e2);
            trackFailure(e2);
            throw e2;
        }
    }

    public final Call getCall$okhttp() {
        return this.call;
    }

    public final EventListener getEventListener$okhttp() {
        return this.eventListener;
    }

    public final Transmitter getTransmitter$okhttp() {
        return this.transmitter;
    }

    public final boolean isDuplex() {
        return this.isDuplex;
    }

    public final RealWebSocket.Streams newWebSocketStreams() throws SocketException {
        this.transmitter.timeoutEarlyExit();
        RealConnection realConnectionConnection = this.codec.connection();
        if (realConnectionConnection != null) {
            return realConnectionConnection.newWebSocketStreams$okhttp(this);
        }
        j.g();
        throw null;
    }

    public final void noNewExchangesOnConnection() {
        RealConnection realConnectionConnection = this.codec.connection();
        if (realConnectionConnection != null) {
            realConnectionConnection.noNewExchanges();
        } else {
            j.g();
            throw null;
        }
    }

    public final void noRequestBody() {
        this.transmitter.exchangeMessageDone$okhttp(this, true, false, null);
    }

    public final ResponseBody openResponseBody(Response response) throws IOException {
        j.c(response, "response");
        try {
            this.eventListener.responseBodyStart(this.call);
            String strHeader$default = Response.header$default(response, "Content-Type", null, 2, null);
            long jReportedContentLength = this.codec.reportedContentLength(response);
            return new RealResponseBody(strHeader$default, jReportedContentLength, p.d(new ResponseBodySource(this, this.codec.openResponseBodySource(response), jReportedContentLength)));
        } catch (IOException e2) {
            this.eventListener.responseFailed(this.call, e2);
            trackFailure(e2);
            throw e2;
        }
    }

    public final Response.Builder readResponseHeaders(boolean z) throws IOException {
        try {
            Response.Builder responseHeaders = this.codec.readResponseHeaders(z);
            if (responseHeaders != null) {
                responseHeaders.initExchange$okhttp(this);
            }
            return responseHeaders;
        } catch (IOException e2) {
            this.eventListener.responseFailed(this.call, e2);
            trackFailure(e2);
            throw e2;
        }
    }

    public final void responseHeadersEnd(Response response) {
        j.c(response, "response");
        this.eventListener.responseHeadersEnd(this.call, response);
    }

    public final void responseHeadersStart() {
        this.eventListener.responseHeadersStart(this.call);
    }

    public final void timeoutEarlyExit() {
        this.transmitter.timeoutEarlyExit();
    }

    public final Headers trailers() throws IOException {
        return this.codec.trailers();
    }

    public final void webSocketUpgradeFailed() {
        bodyComplete(-1L, true, true, null);
    }

    public final void writeRequestHeaders(Request request) throws IOException {
        j.c(request, "request");
        try {
            this.eventListener.requestHeadersStart(this.call);
            this.codec.writeRequestHeaders(request);
            this.eventListener.requestHeadersEnd(this.call, request);
        } catch (IOException e2) {
            this.eventListener.requestFailed(this.call, e2);
            trackFailure(e2);
            throw e2;
        }
    }
}
