package okhttp3;

import S4.p;
import a.AbstractC0872a;
import android.support.v4.media.session.b;
import androidx.navigation.C1134i;
import java.io.Closeable;
import java.io.File;
import java.io.Flushable;
import java.io.IOException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import kotlin.jvm.internal.o;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.TlsVersion;
import okhttp3.internal.Util;
import okhttp3.internal.cache.CacheRequest;
import okhttp3.internal.cache.DiskLruCache;
import okhttp3.internal.concurrent.TaskRunner;
import okhttp3.internal.http.HttpMethod;
import okhttp3.internal.http.StatusLine;
import okhttp3.internal.io.FileSystem;
import okhttp3.internal.platform.Platform;
import p5.AbstractC2732b;
import p5.C2739i;
import p5.C2740j;
import p5.C2743m;
import p5.I;
import p5.InterfaceC2742l;
import p5.J;
import p5.N;
import p5.P;
import p5.v;
import p5.w;
import x4.C3224A;
import x4.y;

/* JADX INFO: loaded from: classes3.dex */
public final class Cache implements Closeable, Flushable {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final Companion f21901b = new Companion(0);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final DiskLruCache f21902a;

    public static final class CacheResponseBody extends ResponseBody {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final DiskLruCache.Snapshot f21903b;

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

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

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final J f21906e;

        public CacheResponseBody(DiskLruCache.Snapshot snapshot, String str, String str2) {
            this.f21903b = snapshot;
            this.f21904c = str;
            this.f21905d = str2;
            this.f21906e = AbstractC2732b.c(new w((P) snapshot.f22255c.get(1)) { // from class: okhttp3.Cache.CacheResponseBody.1
                @Override // p5.w, java.io.Closeable, java.lang.AutoCloseable
                public final void close() {
                    this.f21903b.close();
                    super.close();
                }
            });
        }

        @Override // okhttp3.ResponseBody
        public final long a() {
            String str = this.f21905d;
            if (str == null) {
                return -1L;
            }
            byte[] bArr = Util.f22179a;
            try {
                return Long.parseLong(str);
            } catch (NumberFormatException unused) {
                return -1L;
            }
        }

        @Override // okhttp3.ResponseBody
        public final MediaType b() {
            String str = this.f21904c;
            if (str == null) {
                return null;
            }
            MediaType.f22043d.getClass();
            return MediaType.Companion.b(str);
        }

        @Override // okhttp3.ResponseBody
        public final InterfaceC2742l c() {
            return this.f21906e;
        }
    }

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        public static String a(HttpUrl url) {
            o.h(url, "url");
            C2743m c2743m = C2743m.f23101d;
            return C1134i.n(url.f22032i).c("MD5").e();
        }

        public static int b(J j4) throws IOException {
            C2740j c2740j;
            byte bF;
            try {
                j4.A(1L);
                long j6 = 0;
                while (true) {
                    long j7 = j6 + 1;
                    boolean zP = j4.p(j7);
                    c2740j = j4.f23059b;
                    if (!zP) {
                        break;
                    }
                    bF = c2740j.f(j6);
                    if ((bF < 48 || bF > 57) && (j6 != 0 || bF != 45)) {
                        break;
                    }
                    j6 = j7;
                }
                if (j6 == 0) {
                    AbstractC0872a.w(16);
                    String string = Integer.toString(bF, 16);
                    o.g(string, "toString(...)");
                    throw new NumberFormatException("Expected a digit or '-' but was 0x".concat(string));
                }
                long jB = c2740j.B();
                String strZ = j4.z(Long.MAX_VALUE);
                if (jB >= 0 && jB <= 2147483647L && strZ.length() <= 0) {
                    return (int) jB;
                }
                throw new IOException("expected an int but was \"" + jB + strZ + '\"');
            } catch (NumberFormatException e6) {
                throw new IOException(e6.getMessage());
            }
        }

        public static Set c(Headers headers) {
            int size = headers.size();
            TreeSet treeSet = null;
            for (int i6 = 0; i6 < size; i6++) {
                if ("Vary".equalsIgnoreCase(headers.h(i6))) {
                    String strL = headers.l(i6);
                    if (treeSet == null) {
                        Comparator CASE_INSENSITIVE_ORDER = String.CASE_INSENSITIVE_ORDER;
                        o.g(CASE_INSENSITIVE_ORDER, "CASE_INSENSITIVE_ORDER");
                        treeSet = new TreeSet(CASE_INSENSITIVE_ORDER);
                    }
                    Iterator it = p.c1(strL, new char[]{','}, 6).iterator();
                    while (it.hasNext()) {
                        treeSet.add(p.o1((String) it.next()).toString());
                    }
                }
            }
            return treeSet == null ? C3224A.f26032a : treeSet;
        }

        private Companion() {
        }
    }

    public final class RealCacheRequest implements CacheRequest {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final DiskLruCache.Editor f21919a;

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

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

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

        /* JADX WARN: Type inference failed for: r0v1, types: [okhttp3.Cache$RealCacheRequest$1] */
        public RealCacheRequest(DiskLruCache.Editor editor) {
            this.f21919a = editor;
            N nD = editor.d(1);
            this.f21920b = nD;
            this.f21921c = new v(nD) { // from class: okhttp3.Cache.RealCacheRequest.1
                @Override // p5.v, p5.N, java.io.Closeable, java.lang.AutoCloseable
                public final void close() {
                    Cache cache = cache;
                    RealCacheRequest realCacheRequest = this;
                    synchronized (cache) {
                        if (realCacheRequest.f21922d) {
                            return;
                        }
                        realCacheRequest.f21922d = true;
                        super.close();
                        this.f21919a.b();
                    }
                }
            };
        }

        @Override // okhttp3.internal.cache.CacheRequest
        public final AnonymousClass1 a() {
            return this.f21921c;
        }

        @Override // okhttp3.internal.cache.CacheRequest
        public final void abort() {
            synchronized (Cache.this) {
                if (this.f21922d) {
                    return;
                }
                this.f21922d = true;
                Util.c(this.f21920b);
                try {
                    this.f21919a.a();
                } catch (IOException unused) {
                }
            }
        }
    }

    public Cache(File file) {
        FileSystem fileSystem = FileSystem.f22595a;
        o.h(fileSystem, "fileSystem");
        this.f21902a = new DiskLruCache(fileSystem, file, TaskRunner.f22274i);
    }

    public static void c(Response cached, Response response) throws Throwable {
        DiskLruCache.Editor editorC;
        o.h(cached, "cached");
        Entry entry = new Entry(response);
        ResponseBody responseBody = cached.f22146j;
        o.f(responseBody, "null cannot be cast to non-null type okhttp3.Cache.CacheResponseBody");
        DiskLruCache.Snapshot snapshot = ((CacheResponseBody) responseBody).f21903b;
        try {
            editorC = snapshot.f22256d.c(snapshot.f22253a, snapshot.f22254b);
            if (editorC == null) {
                return;
            }
            try {
                entry.c(editorC);
                editorC.b();
            } catch (IOException unused) {
                if (editorC != null) {
                    try {
                        editorC.a();
                    } catch (IOException unused2) {
                    }
                }
            }
        } catch (IOException unused3) {
            editorC = null;
        }
    }

    public final Response a(Request request) {
        o.h(request, "request");
        f21901b.getClass();
        HttpUrl httpUrl = request.f22125a;
        try {
            DiskLruCache.Snapshot snapshotD = this.f21902a.d(Companion.a(httpUrl));
            if (snapshotD != null) {
                try {
                    Entry entry = new Entry((P) snapshotD.f22255c.get(0));
                    Headers headers = entry.f21910b;
                    String str = entry.f21911c;
                    HttpUrl url = entry.f21909a;
                    Headers headers2 = entry.f21915g;
                    String strG = headers2.g("Content-Type");
                    String strG2 = headers2.g("Content-Length");
                    Request.Builder builder = new Request.Builder();
                    o.h(url, "url");
                    builder.f22131a = url;
                    builder.g(str, null);
                    builder.f(headers);
                    Request requestB = builder.b();
                    Response.Builder builder2 = new Response.Builder();
                    builder2.f22153a = requestB;
                    Protocol protocol = entry.f21912d;
                    o.h(protocol, "protocol");
                    builder2.f22154b = protocol;
                    builder2.f22155c = entry.f21913e;
                    String message = entry.f21914f;
                    o.h(message, "message");
                    builder2.f22156d = message;
                    builder2.c(headers2);
                    builder2.f22159g = new CacheResponseBody(snapshotD, strG, strG2);
                    builder2.f22157e = entry.f21916h;
                    builder2.f22163k = entry.f21917i;
                    builder2.l = entry.f21918j;
                    Response responseA = builder2.a();
                    if (url.equals(httpUrl) && str.equals(request.f22126b)) {
                        Set<String> setC = Companion.c(responseA.f22145f);
                        if (!(setC instanceof Collection) || !setC.isEmpty()) {
                            for (String str2 : setC) {
                                if (!headers.m(str2).equals(request.f22127c.m(str2))) {
                                }
                            }
                        }
                        return responseA;
                    }
                    ResponseBody responseBody = responseA.f22146j;
                    if (responseBody != null) {
                        Util.c(responseBody);
                        return null;
                    }
                } catch (IOException unused) {
                    Util.c(snapshotD);
                }
            }
        } catch (IOException unused2) {
        }
        return null;
    }

    public final CacheRequest b(Response response) throws Throwable {
        DiskLruCache.Editor editorC;
        Request request = response.f22140a;
        String str = request.f22126b;
        HttpMethod.f22383a.getClass();
        if (!HttpMethod.a(str)) {
            if (str.equals("GET")) {
                f21901b.getClass();
                if (!Companion.c(response.f22145f).contains("*")) {
                    Entry entry = new Entry(response);
                    try {
                        editorC = this.f21902a.c(Companion.a(request.f22125a), DiskLruCache.f22206A);
                        if (editorC != null) {
                            try {
                                entry.c(editorC);
                                return new RealCacheRequest(editorC);
                            } catch (IOException unused) {
                                if (editorC != null) {
                                    editorC.a();
                                }
                                return null;
                            }
                        }
                    } catch (IOException unused2) {
                        editorC = null;
                    }
                }
            }
            return null;
        }
        Companion companion = f21901b;
        HttpUrl httpUrl = request.f22125a;
        companion.getClass();
        String key = Companion.a(httpUrl);
        DiskLruCache diskLruCache = this.f21902a;
        synchronized (diskLruCache) {
            o.h(key, "key");
            diskLruCache.f();
            diskLruCache.a();
            DiskLruCache.C(key);
            DiskLruCache.Entry entry2 = (DiskLruCache.Entry) diskLruCache.f22224k.get(key);
            if (entry2 == null) {
                return null;
            }
            diskLruCache.A(entry2);
            if (diskLruCache.f22222f <= 104857600) {
                diskLruCache.f22229q = false;
            }
            return null;
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        this.f21902a.close();
    }

    @Override // java.io.Flushable
    public final void flush() {
        this.f21902a.flush();
    }

    public static final class Entry {

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        public static final String f21908k;
        public static final String l;

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final HttpUrl f21909a;

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

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

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

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final int f21913e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final String f21914f;

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

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        public final Handshake f21916h;

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

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

        public static final class Companion {
            public /* synthetic */ Companion(int i6) {
                this();
            }

            private Companion() {
            }
        }

        static {
            new Companion(0);
            StringBuilder sb = new StringBuilder();
            Platform.Companion companion = Platform.Companion;
            sb.append(companion.get().getPrefix());
            sb.append("-Sent-Millis");
            f21908k = sb.toString();
            l = companion.get().getPrefix() + "-Received-Millis";
        }

        public Entry(P rawSource) throws IOException {
            HttpUrl httpUrlA;
            TlsVersion tlsVersionA;
            o.h(rawSource, "rawSource");
            try {
                J jC = AbstractC2732b.c(rawSource);
                String strZ = jC.z(Long.MAX_VALUE);
                HttpUrl.f22023k.getClass();
                try {
                    HttpUrl.Builder builder = new HttpUrl.Builder();
                    builder.b(null, strZ);
                    httpUrlA = builder.a();
                } catch (IllegalArgumentException unused) {
                    httpUrlA = null;
                }
                if (httpUrlA == null) {
                    IOException iOException = new IOException("Cache corruption for ".concat(strZ));
                    Platform.Companion.get().log("cache corruption", 5, iOException);
                    throw iOException;
                }
                this.f21909a = httpUrlA;
                this.f21911c = jC.z(Long.MAX_VALUE);
                Headers.Builder builder2 = new Headers.Builder();
                Cache.f21901b.getClass();
                int iB = Companion.b(jC);
                for (int i6 = 0; i6 < iB; i6++) {
                    builder2.b(jC.z(Long.MAX_VALUE));
                }
                this.f21910b = builder2.e();
                StatusLine.Companion companion = StatusLine.f22398d;
                String strZ2 = jC.z(Long.MAX_VALUE);
                companion.getClass();
                StatusLine statusLineA = StatusLine.Companion.a(strZ2);
                this.f21912d = statusLineA.f22399a;
                this.f21913e = statusLineA.f22400b;
                this.f21914f = statusLineA.f22401c;
                Headers.Builder builder3 = new Headers.Builder();
                Cache.f21901b.getClass();
                int iB2 = Companion.b(jC);
                for (int i7 = 0; i7 < iB2; i7++) {
                    builder3.b(jC.z(Long.MAX_VALUE));
                }
                String str = f21908k;
                String strF = builder3.f(str);
                String str2 = l;
                String strF2 = builder3.f(str2);
                builder3.g(str);
                builder3.g(str2);
                this.f21917i = strF != null ? Long.parseLong(strF) : 0L;
                this.f21918j = strF2 != null ? Long.parseLong(strF2) : 0L;
                this.f21915g = builder3.e();
                if (o.c(this.f21909a.f22024a, "https")) {
                    String strZ3 = jC.z(Long.MAX_VALUE);
                    if (strZ3.length() > 0) {
                        throw new IOException("expected \"\" but was \"" + strZ3 + '\"');
                    }
                    CipherSuite cipherSuiteB = CipherSuite.f21956b.b(jC.z(Long.MAX_VALUE));
                    List listA = a(jC);
                    List listA2 = a(jC);
                    if (jC.a()) {
                        tlsVersionA = TlsVersion.SSL_3_0;
                    } else {
                        TlsVersion.Companion companion2 = TlsVersion.f22171b;
                        String strZ4 = jC.z(Long.MAX_VALUE);
                        companion2.getClass();
                        tlsVersionA = TlsVersion.Companion.a(strZ4);
                    }
                    Handshake.f22012e.getClass();
                    this.f21916h = new Handshake(tlsVersionA, cipherSuiteB, Util.y(listA2), new Handshake$Companion$get$1(Util.y(listA)));
                } else {
                    this.f21916h = null;
                }
                rawSource.close();
            } catch (Throwable th) {
                try {
                    throw th;
                } catch (Throwable th2) {
                    b.j(rawSource, th);
                    throw th2;
                }
            }
        }

        public static List a(J j4) throws IOException {
            int i6 = 0;
            Cache.f21901b.getClass();
            int iB = Companion.b(j4);
            if (iB == -1) {
                return y.f26065a;
            }
            try {
                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
                ArrayList arrayList = new ArrayList(iB);
                for (int i7 = 0; i7 < iB; i7++) {
                    String strZ = j4.z(Long.MAX_VALUE);
                    C2740j c2740j = new C2740j();
                    C2743m c2743m = C2743m.f23101d;
                    C2743m c2743mL = C1134i.l(strZ);
                    if (c2743mL == null) {
                        throw new IOException("Corrupt certificate in cache entry");
                    }
                    c2740j.N(c2743mL);
                    arrayList.add(certificateFactory.generateCertificate(new C2739i(c2740j, i6)));
                }
                return arrayList;
            } catch (CertificateException e6) {
                throw new IOException(e6.getMessage());
            }
        }

        public static void b(I i6, List list) throws IOException {
            try {
                i6.w(list.size());
                i6.h(10);
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    byte[] bytes = ((Certificate) it.next()).getEncoded();
                    C2743m c2743m = C2743m.f23101d;
                    o.g(bytes, "bytes");
                    i6.n(C1134i.s(bytes).a());
                    i6.h(10);
                }
            } catch (CertificateEncodingException e6) {
                throw new IOException(e6.getMessage());
            }
        }

        public final void c(DiskLruCache.Editor editor) throws Throwable {
            HttpUrl httpUrl = this.f21909a;
            Handshake handshake = this.f21916h;
            Headers headers = this.f21915g;
            Headers headers2 = this.f21910b;
            I iB = AbstractC2732b.b(editor.d(0));
            try {
                iB.n(httpUrl.f22032i);
                iB.h(10);
                iB.n(this.f21911c);
                iB.h(10);
                iB.w(headers2.size());
                iB.h(10);
                int size = headers2.size();
                for (int i6 = 0; i6 < size; i6++) {
                    iB.n(headers2.h(i6));
                    iB.n(": ");
                    iB.n(headers2.l(i6));
                    iB.h(10);
                }
                iB.n(new StatusLine(this.f21912d, this.f21913e, this.f21914f).toString());
                iB.h(10);
                iB.w(headers.size() + 2);
                iB.h(10);
                int size2 = headers.size();
                for (int i7 = 0; i7 < size2; i7++) {
                    iB.n(headers.h(i7));
                    iB.n(": ");
                    iB.n(headers.l(i7));
                    iB.h(10);
                }
                iB.n(f21908k);
                iB.n(": ");
                iB.w(this.f21917i);
                iB.h(10);
                iB.n(l);
                iB.n(": ");
                iB.w(this.f21918j);
                iB.h(10);
                if (o.c(httpUrl.f22024a, "https")) {
                    iB.h(10);
                    o.e(handshake);
                    iB.n(handshake.f22014b.f21974a);
                    iB.h(10);
                    b(iB, handshake.a());
                    b(iB, handshake.f22015c);
                    iB.n(handshake.f22013a.f22178a);
                    iB.h(10);
                }
                iB.close();
            } finally {
            }
        }

        public Entry(Response response) {
            Headers headersE;
            Request request = response.f22140a;
            this.f21909a = request.f22125a;
            Cache.f21901b.getClass();
            Response response2 = response.f22147k;
            o.e(response2);
            Headers headers = response2.f22140a.f22127c;
            Headers headers2 = response.f22145f;
            Set setC = Companion.c(headers2);
            if (setC.isEmpty()) {
                headersE = Util.f22180b;
            } else {
                Headers.Builder builder = new Headers.Builder();
                int size = headers.size();
                for (int i6 = 0; i6 < size; i6++) {
                    String strH = headers.h(i6);
                    if (setC.contains(strH)) {
                        builder.a(strH, headers.l(i6));
                    }
                }
                headersE = builder.e();
            }
            this.f21910b = headersE;
            this.f21911c = request.f22126b;
            this.f21912d = response.f22141b;
            this.f21913e = response.f22143d;
            this.f21914f = response.f22142c;
            this.f21915g = headers2;
            this.f21916h = response.f22144e;
            this.f21917i = response.f22149n;
            this.f21918j = response.f22150o;
        }
    }
}
