package retrofit2; import java.io.IOException; import okhttp3.MediaType; import okhttp3.Request; import okhttp3.ResponseBody; import okio.Buffer; import okio.BufferedSource; import okio.ForwardingSource; import okio.Okio; /* loaded from: classes2.dex */ final class OkHttpCall implements Call { private final Object[] args; private volatile boolean canceled; private Throwable creationFailure; private boolean executed; private okhttp3.Call rawCall; private final ServiceMethod serviceMethod; static final class ExceptionCatchingRequestBody extends ResponseBody { private final ResponseBody delegate; IOException thrownException; ExceptionCatchingRequestBody(ResponseBody responseBody) { this.delegate = responseBody; } @Override // okhttp3.ResponseBody, java.io.Closeable, java.lang.AutoCloseable public void close() { this.delegate.close(); } @Override // okhttp3.ResponseBody public long contentLength() { return this.delegate.contentLength(); } @Override // okhttp3.ResponseBody public MediaType contentType() { return this.delegate.contentType(); } @Override // okhttp3.ResponseBody public BufferedSource source() { return Okio.buffer(new ForwardingSource(this.delegate.source()) { // from class: retrofit2.OkHttpCall.ExceptionCatchingRequestBody.1 @Override // okio.ForwardingSource, okio.Source public long read(Buffer buffer, long j) throws IOException { try { return super.read(buffer, j); } catch (IOException e) { ExceptionCatchingRequestBody.this.thrownException = e; throw e; } } }); } void throwIfCaught() throws IOException { IOException iOException = this.thrownException; if (iOException != null) { throw iOException; } } } static final class NoContentResponseBody extends ResponseBody { private final long contentLength; private final MediaType contentType; NoContentResponseBody(MediaType mediaType, long j) { this.contentType = mediaType; this.contentLength = j; } @Override // okhttp3.ResponseBody public long contentLength() { return this.contentLength; } @Override // okhttp3.ResponseBody public MediaType contentType() { return this.contentType; } @Override // okhttp3.ResponseBody public BufferedSource source() { throw new IllegalStateException("Cannot read raw response body of a converted body."); } } OkHttpCall(ServiceMethod serviceMethod, Object[] objArr) { this.serviceMethod = serviceMethod; this.args = objArr; } private okhttp3.Call createRawCall() throws IOException { okhttp3.Call call = this.serviceMethod.toCall(this.args); if (call != null) { return call; } throw new NullPointerException("Call.Factory returned null."); } @Override // retrofit2.Call public void cancel() { okhttp3.Call call; this.canceled = true; synchronized (this) { call = this.rawCall; } if (call != null) { call.cancel(); } } @Override // retrofit2.Call public void enqueue(final Callback callback) { okhttp3.Call call; Throwable th; Utils.checkNotNull(callback, "callback == null"); synchronized (this) { if (this.executed) { throw new IllegalStateException("Already executed."); } this.executed = true; call = this.rawCall; th = this.creationFailure; if (call == null && th == null) { try { okhttp3.Call createRawCall = createRawCall(); this.rawCall = createRawCall; call = createRawCall; } catch (Throwable th2) { th = th2; Utils.throwIfFatal(th); this.creationFailure = th; } } } if (th != null) { callback.onFailure(this, th); return; } if (this.canceled) { call.cancel(); } call.enqueue(new okhttp3.Callback() { // from class: retrofit2.OkHttpCall.1 private void callFailure(Throwable th3) { try { callback.onFailure(OkHttpCall.this, th3); } catch (Throwable th4) { th4.printStackTrace(); } } @Override // okhttp3.Callback public void onFailure(okhttp3.Call call2, IOException iOException) { callFailure(iOException); } @Override // okhttp3.Callback public void onResponse(okhttp3.Call call2, okhttp3.Response response) { try { try { callback.onResponse(OkHttpCall.this, OkHttpCall.this.parseResponse(response)); } catch (Throwable th3) { th3.printStackTrace(); } } catch (Throwable th4) { callFailure(th4); } } }); } @Override // retrofit2.Call public Response execute() throws IOException { okhttp3.Call call; synchronized (this) { if (this.executed) { throw new IllegalStateException("Already executed."); } this.executed = true; if (this.creationFailure != null) { if (this.creationFailure instanceof IOException) { throw ((IOException) this.creationFailure); } if (this.creationFailure instanceof RuntimeException) { throw ((RuntimeException) this.creationFailure); } throw ((Error) this.creationFailure); } call = this.rawCall; if (call == null) { try { call = createRawCall(); this.rawCall = call; } catch (IOException | Error | RuntimeException e) { Utils.throwIfFatal(e); this.creationFailure = e; throw e; } } } if (this.canceled) { call.cancel(); } return parseResponse(call.execute()); } @Override // retrofit2.Call public boolean isCanceled() { boolean z = true; if (this.canceled) { return true; } synchronized (this) { if (this.rawCall == null || !this.rawCall.isCanceled()) { z = false; } } return z; } @Override // retrofit2.Call public synchronized boolean isExecuted() { return this.executed; } Response parseResponse(okhttp3.Response response) throws IOException { ResponseBody body = response.body(); okhttp3.Response build = response.newBuilder().body(new NoContentResponseBody(body.contentType(), body.contentLength())).build(); int code = build.code(); if (code < 200 || code >= 300) { try { return Response.error(Utils.buffer(body), build); } finally { body.close(); } } if (code == 204 || code == 205) { body.close(); return Response.success((Object) null, build); } ExceptionCatchingRequestBody exceptionCatchingRequestBody = new ExceptionCatchingRequestBody(body); try { return Response.success(this.serviceMethod.toResponse(exceptionCatchingRequestBody), build); } catch (RuntimeException e) { exceptionCatchingRequestBody.throwIfCaught(); throw e; } } @Override // retrofit2.Call public synchronized Request request() { okhttp3.Call call = this.rawCall; if (call != null) { return call.request(); } if (this.creationFailure != null) { if (this.creationFailure instanceof IOException) { throw new RuntimeException("Unable to create request.", this.creationFailure); } if (this.creationFailure instanceof RuntimeException) { throw ((RuntimeException) this.creationFailure); } throw ((Error) this.creationFailure); } try { okhttp3.Call createRawCall = createRawCall(); this.rawCall = createRawCall; return createRawCall.request(); } catch (IOException e) { this.creationFailure = e; throw new RuntimeException("Unable to create request.", e); } catch (Error e2) { e = e2; Utils.throwIfFatal(e); this.creationFailure = e; throw e; } catch (RuntimeException e3) { e = e3; Utils.throwIfFatal(e); this.creationFailure = e; throw e; } } @Override // retrofit2.Call public OkHttpCall clone() { return new OkHttpCall<>(this.serviceMethod, this.args); } }