package com.vungle.ads.internal.network; import com.vungle.ads.internal.network.converters.Converter; import com.vungle.ads.internal.util.Logger; import java.io.IOException; import java.util.Objects; import kotlin.Unit; import kotlin.io.CloseableKt; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import okhttp3.MediaType; import okhttp3.ResponseBody; import okio.Buffer; import okio.BufferedSource; import okio.ForwardingSource; import okio.Okio; /* loaded from: classes4.dex */ public final class OkHttpCall implements Call { public static final Companion Companion = new Companion(null); private static final String TAG = "OkHttpCall"; private volatile boolean canceled; private final okhttp3.Call rawCall; private final Converter responseConverter; public OkHttpCall(okhttp3.Call rawCall, Converter responseConverter) { Intrinsics.checkNotNullParameter(rawCall, "rawCall"); Intrinsics.checkNotNullParameter(responseConverter, "responseConverter"); this.rawCall = rawCall; this.responseConverter = responseConverter; } @Override // com.vungle.ads.internal.network.Call public void enqueue(final Callback callback) { okhttp3.Call call; Intrinsics.checkNotNullParameter(callback, "callback"); Objects.requireNonNull(callback, "callback == null"); synchronized (this) { call = this.rawCall; Unit unit = Unit.INSTANCE; } if (this.canceled) { call.cancel(); } call.enqueue(new okhttp3.Callback(this) { // from class: com.vungle.ads.internal.network.OkHttpCall$enqueue$2 final /* synthetic */ OkHttpCall this$0; { this.this$0 = this; } @Override // okhttp3.Callback public void onResponse(okhttp3.Call call2, okhttp3.Response response) { Intrinsics.checkNotNullParameter(call2, "call"); Intrinsics.checkNotNullParameter(response, "response"); try { try { callback.onResponse(this.this$0, this.this$0.parseResponse(response)); } catch (Throwable th) { OkHttpCall.Companion.throwIfFatal(th); Logger.Companion.e("OkHttpCall", "Cannot pass response to callback", th); } } catch (Throwable th2) { OkHttpCall.Companion.throwIfFatal(th2); callFailure(th2); } } @Override // okhttp3.Callback public void onFailure(okhttp3.Call call2, IOException e) { Intrinsics.checkNotNullParameter(call2, "call"); Intrinsics.checkNotNullParameter(e, "e"); callFailure(e); } private final void callFailure(Throwable th) { try { callback.onFailure(this.this$0, th); } catch (Throwable th2) { OkHttpCall.Companion.throwIfFatal(th2); Logger.Companion.e("OkHttpCall", "Cannot pass failure to callback", th2); } } }); } @Override // com.vungle.ads.internal.network.Call public Response execute() throws IOException { okhttp3.Call call; synchronized (this) { call = this.rawCall; Unit unit = Unit.INSTANCE; } if (this.canceled) { call.cancel(); } return parseResponse(call.execute()); } public final Response parseResponse(okhttp3.Response rawResp) throws IOException { Intrinsics.checkNotNullParameter(rawResp, "rawResp"); ResponseBody body = rawResp.body(); if (body == null) { return null; } okhttp3.Response build = rawResp.newBuilder().body(new NoContentResponseBody(body.contentType(), body.contentLength())).build(); int code = build.code(); if (code >= 200 && code < 300) { if (code == 204 || code == 205) { body.close(); return Response.Companion.success(null, build); } ExceptionCatchingResponseBody exceptionCatchingResponseBody = new ExceptionCatchingResponseBody(body); try { return Response.Companion.success(this.responseConverter.convert(exceptionCatchingResponseBody), build); } catch (RuntimeException e) { exceptionCatchingResponseBody.throwIfCaught(); throw e; } } try { Response error = Response.Companion.error(buffer(body), build); CloseableKt.closeFinally(body, null); return error; } finally { } } private final ResponseBody buffer(ResponseBody responseBody) throws IOException { Buffer buffer = new Buffer(); responseBody.source().readAll(buffer); return ResponseBody.Companion.create(buffer, responseBody.contentType(), responseBody.contentLength()); } @Override // com.vungle.ads.internal.network.Call public void cancel() { okhttp3.Call call; this.canceled = true; synchronized (this) { call = this.rawCall; Unit unit = Unit.INSTANCE; } call.cancel(); } @Override // com.vungle.ads.internal.network.Call public boolean isCanceled() { boolean isCanceled; if (this.canceled) { return true; } synchronized (this) { isCanceled = this.rawCall.isCanceled(); } return isCanceled; } public static final class NoContentResponseBody extends ResponseBody { private final long contentLength; private final MediaType contentType; @Override // okhttp3.ResponseBody public long contentLength() { return this.contentLength; } @Override // okhttp3.ResponseBody public MediaType contentType() { return this.contentType; } public NoContentResponseBody(MediaType mediaType, long j) { this.contentType = mediaType; this.contentLength = j; } @Override // okhttp3.ResponseBody public BufferedSource source() { throw new IllegalStateException("Cannot read raw response body of a converted body."); } } public static final class ExceptionCatchingResponseBody extends ResponseBody { private final ResponseBody delegate; private final BufferedSource delegateSource; private IOException thrownException; public final IOException getThrownException() { return this.thrownException; } public final void setThrownException(IOException iOException) { this.thrownException = iOException; } @Override // okhttp3.ResponseBody public BufferedSource source() { return this.delegateSource; } public ExceptionCatchingResponseBody(ResponseBody delegate) { Intrinsics.checkNotNullParameter(delegate, "delegate"); this.delegate = delegate; this.delegateSource = Okio.buffer(new ForwardingSource(delegate.source()) { // from class: com.vungle.ads.internal.network.OkHttpCall.ExceptionCatchingResponseBody.1 @Override // okio.ForwardingSource, okio.Source public long read(Buffer sink, long j) throws IOException { Intrinsics.checkNotNullParameter(sink, "sink"); try { return super.read(sink, j); } catch (IOException e) { ExceptionCatchingResponseBody.this.setThrownException(e); throw e; } } }); } @Override // okhttp3.ResponseBody public MediaType contentType() { return this.delegate.contentType(); } @Override // okhttp3.ResponseBody public long contentLength() { return this.delegate.contentLength(); } @Override // okhttp3.ResponseBody, java.io.Closeable, java.lang.AutoCloseable public void close() { this.delegate.close(); } public final void throwIfCaught() throws IOException { IOException iOException = this.thrownException; if (iOException != null) { throw iOException; } } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } /* JADX INFO: Access modifiers changed from: private */ public final void throwIfFatal(Throwable th) { if (th instanceof VirtualMachineError) { throw th; } if (th instanceof ThreadDeath) { throw th; } if (th instanceof LinkageError) { throw th; } } } }