package okhttp3.internal.connection; import csdk.gluads.Consts; import java.io.IOException; import java.net.ProtocolException; import okhttp3.Call; import okhttp3.EventListener; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; import okhttp3.internal.Internal; import okhttp3.internal.http.ExchangeCodec; import okhttp3.internal.http.RealResponseBody; import okio.Buffer; import okio.ForwardingSink; import okio.ForwardingSource; import okio.Okio; import okio.Sink; import okio.Source; /* loaded from: classes5.dex */ public final class Exchange { public final Call call; public final ExchangeCodec codec; public boolean duplex; public final EventListener eventListener; public final ExchangeFinder finder; public final Transmitter transmitter; public boolean isDuplex() { return this.duplex; } public Exchange(Transmitter transmitter, Call call, EventListener eventListener, ExchangeFinder exchangeFinder, ExchangeCodec exchangeCodec) { this.transmitter = transmitter; this.call = call; this.eventListener = eventListener; this.finder = exchangeFinder; this.codec = exchangeCodec; } public RealConnection connection() { return this.codec.connection(); } public void writeRequestHeaders(Request request) { try { this.eventListener.requestHeadersStart(this.call); this.codec.writeRequestHeaders(request); this.eventListener.requestHeadersEnd(this.call, request); } catch (IOException e) { this.eventListener.requestFailed(this.call, e); trackFailure(e); throw e; } } public Sink createRequestBody(Request request, boolean z) { this.duplex = z; long contentLength = request.body().contentLength(); this.eventListener.requestBodyStart(this.call); return new RequestBodySink(this.codec.createRequestBody(request, contentLength), contentLength); } public void flushRequest() { try { this.codec.flushRequest(); } catch (IOException e) { this.eventListener.requestFailed(this.call, e); trackFailure(e); throw e; } } public void finishRequest() { try { this.codec.finishRequest(); } catch (IOException e) { this.eventListener.requestFailed(this.call, e); trackFailure(e); throw e; } } public void responseHeadersStart() { this.eventListener.responseHeadersStart(this.call); } public Response.Builder readResponseHeaders(boolean z) { try { Response.Builder readResponseHeaders = this.codec.readResponseHeaders(z); if (readResponseHeaders != null) { Internal.instance.initExchange(readResponseHeaders, this); } return readResponseHeaders; } catch (IOException e) { this.eventListener.responseFailed(this.call, e); trackFailure(e); throw e; } } public void responseHeadersEnd(Response response) { this.eventListener.responseHeadersEnd(this.call, response); } public ResponseBody openResponseBody(Response response) { try { this.eventListener.responseBodyStart(this.call); String header = response.header("Content-Type"); long reportedContentLength = this.codec.reportedContentLength(response); return new RealResponseBody(header, reportedContentLength, Okio.buffer(new ResponseBodySource(this.codec.openResponseBodySource(response), reportedContentLength))); } catch (IOException e) { this.eventListener.responseFailed(this.call, e); trackFailure(e); throw e; } } public void noNewExchangesOnConnection() { this.codec.connection().noNewExchanges(); } public void cancel() { this.codec.cancel(); } public void detachWithViolence() { this.codec.cancel(); this.transmitter.exchangeMessageDone(this, true, true, null); } public void trackFailure(IOException iOException) { this.finder.trackFailure(); this.codec.connection().trackFailure(iOException); } public IOException bodyComplete(long j, boolean z, boolean z2, IOException iOException) { if (iOException != null) { trackFailure(iOException); } if (z2) { if (iOException != null) { this.eventListener.requestFailed(this.call, iOException); } else { this.eventListener.requestBodyEnd(this.call, j); } } if (z) { if (iOException != null) { this.eventListener.responseFailed(this.call, iOException); } else { this.eventListener.responseBodyEnd(this.call, j); } } return this.transmitter.exchangeMessageDone(this, z2, z, iOException); } public void noRequestBody() { this.transmitter.exchangeMessageDone(this, true, false, null); } public final class RequestBodySink extends ForwardingSink { public long bytesReceived; public boolean closed; public boolean completed; public long contentLength; public RequestBodySink(Sink sink, long j) { super(sink); this.contentLength = j; } @Override // okio.ForwardingSink, okio.Sink public void write(Buffer buffer, long j) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } long j2 = this.contentLength; if (j2 != -1 && this.bytesReceived + j > j2) { throw new ProtocolException("expected " + this.contentLength + " bytes but received " + (this.bytesReceived + j)); } try { super.write(buffer, j); this.bytesReceived += j; } catch (IOException e) { throw complete(e); } } @Override // okio.ForwardingSink, okio.Sink, java.io.Flushable public void flush() { try { super.flush(); } catch (IOException e) { throw complete(e); } } @Override // okio.ForwardingSink, okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() { 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 e) { throw complete(e); } } private IOException complete(IOException iOException) { if (this.completed) { return iOException; } this.completed = true; return Exchange.this.bodyComplete(this.bytesReceived, false, true, iOException); } } public final class ResponseBodySource extends ForwardingSource { public long bytesReceived; public boolean closed; public boolean completed; public final long contentLength; public ResponseBodySource(Source source, long j) { super(source); this.contentLength = j; if (j == 0) { complete(null); } } @Override // okio.ForwardingSource, okio.Source public long read(Buffer buffer, long j) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } try { long read = delegate().read(buffer, j); if (read == -1) { complete(null); return -1L; } long j2 = this.bytesReceived + read; long j3 = this.contentLength; if (j3 != -1 && j2 > j3) { throw new ProtocolException("expected " + this.contentLength + " bytes but received " + j2); } this.bytesReceived = j2; if (j2 == j3) { complete(null); } return read; } catch (IOException e) { throw complete(e); } } @Override // okio.ForwardingSource, okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.closed) { return; } this.closed = true; try { super.close(); complete(null); } catch (IOException e) { throw complete(e); } } public IOException complete(IOException iOException) { if (this.completed) { return iOException; } this.completed = true; return Exchange.this.bodyComplete(this.bytesReceived, true, false, iOException); } } }