package okhttp3.internal;

import F1.I;
import S4.a;
import S4.m;
import S4.p;
import androidx.navigation.C1134i;
import com.revenuecat.purchases.common.Constants;
import j$.util.DesugarTimeZone;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.reflect.Field;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.o;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.ResponseBody$Companion$asResponseBody$1;
import okhttp3.internal.http2.Header;
import p5.AbstractC2732b;
import p5.C2740j;
import p5.C2743m;
import p5.F;
import p5.InterfaceC2742l;
import p5.J;
import p5.P;
import x4.AbstractC3251q;
import x4.AbstractC3252r;

/* JADX INFO: loaded from: classes3.dex */
public final class Util {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final byte[] f22179a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final ResponseBody$Companion$asResponseBody$1 f22181c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final F f22182d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final TimeZone f22183e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final m f22184f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final String f22185g;

    /* JADX WARN: Type inference failed for: r0v1, types: [okhttp3.ResponseBody$Companion$asResponseBody$1] */
    static {
        byte[] bArr = new byte[0];
        f22179a = bArr;
        Headers.f22020b.getClass();
        f22180b = Headers.Companion.c(new String[0]);
        ResponseBody.f22165a.getClass();
        final C2740j c2740j = new C2740j();
        c2740j.O(bArr);
        final long j4 = 0;
        f22181c = new ResponseBody() { // from class: okhttp3.ResponseBody$Companion$asResponseBody$1
            @Override // okhttp3.ResponseBody
            public final long a() {
                return j4;
            }

            @Override // okhttp3.ResponseBody
            public final MediaType b() {
                return null;
            }

            @Override // okhttp3.ResponseBody
            public final InterfaceC2742l c() {
                return c2740j;
            }
        };
        RequestBody.Companion.b(RequestBody.f22136a, bArr, null, 7);
        C2743m c2743m = C2743m.f23101d;
        f22182d = AbstractC2732b.g(C1134i.m("efbbbf"), C1134i.m("feff"), C1134i.m("fffe"), C1134i.m("0000ffff"), C1134i.m("ffff0000"));
        TimeZone timeZone = DesugarTimeZone.getTimeZone("GMT");
        o.e(timeZone);
        f22183e = timeZone;
        f22184f = new m("([0-9a-fA-F]*:[0-9a-fA-F:.]*)|([\\d.]+)");
        f22185g = p.Y0(p.X0(OkHttpClient.class.getName(), "okhttp3."), "Client");
    }

    public static final String A(int i6, int i7, String str) {
        int iM = m(i6, i7, str);
        String strSubstring = str.substring(iM, n(iM, i7, str));
        o.g(strSubstring, "this as java.lang.String…ing(startIndex, endIndex)");
        return strSubstring;
    }

    public static final boolean a(HttpUrl httpUrl, HttpUrl other) {
        o.h(httpUrl, "<this>");
        o.h(other, "other");
        return o.c(httpUrl.f22027d, other.f22027d) && httpUrl.f22028e == other.f22028e && o.c(httpUrl.f22024a, other.f22024a);
    }

    public static final int b(long j4) {
        TimeUnit timeUnit = TimeUnit.SECONDS;
        if (j4 < 0) {
            throw new IllegalStateException("timeout".concat(" < 0").toString());
        }
        if (timeUnit == null) {
            throw new IllegalStateException("unit == null");
        }
        long millis = timeUnit.toMillis(j4);
        if (millis > 2147483647L) {
            throw new IllegalArgumentException("timeout".concat(" too large.").toString());
        }
        if (millis != 0 || j4 <= 0) {
            return (int) millis;
        }
        throw new IllegalArgumentException("timeout".concat(" too small.").toString());
    }

    public static final void c(Closeable closeable) {
        o.h(closeable, "<this>");
        try {
            closeable.close();
        } catch (RuntimeException e6) {
            throw e6;
        } catch (Exception unused) {
        }
    }

    public static final void d(Socket socket) {
        o.h(socket, "<this>");
        try {
            socket.close();
        } catch (AssertionError e6) {
            throw e6;
        } catch (RuntimeException e7) {
            if (!o.c(e7.getMessage(), "bio == null")) {
                throw e7;
            }
        } catch (Exception unused) {
        }
    }

    public static final int e(int i6, int i7, String str, String str2) {
        o.h(str, "<this>");
        while (i6 < i7) {
            if (p.E0(str2, str.charAt(i6))) {
                return i6;
            }
            i6++;
        }
        return i7;
    }

    public static final int f(String str, char c6, int i6, int i7) {
        o.h(str, "<this>");
        while (i6 < i7) {
            if (str.charAt(i6) == c6) {
                return i6;
            }
            i6++;
        }
        return i7;
    }

    public static final boolean g(P p6) {
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        o.h(timeUnit, "timeUnit");
        try {
            return v(p6, 100);
        } catch (IOException unused) {
            return false;
        }
    }

    public static final String h(String format, Object... objArr) {
        o.h(format, "format");
        Locale locale = Locale.US;
        Object[] objArrCopyOf = Arrays.copyOf(objArr, objArr.length);
        return String.format(locale, format, Arrays.copyOf(objArrCopyOf, objArrCopyOf.length));
    }

    public static final boolean i(String[] strArr, String[] strArr2, Comparator comparator) {
        o.h(strArr, "<this>");
        o.h(comparator, "comparator");
        if (strArr.length != 0 && strArr2 != null && strArr2.length != 0) {
            for (String str : strArr) {
                I iK = o.k(strArr2);
                while (iK.hasNext()) {
                    if (comparator.compare(str, (String) iK.next()) == 0) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static final long j(Response response) {
        String strG = response.f22145f.g("Content-Length");
        if (strG == null) {
            return -1L;
        }
        try {
            return Long.parseLong(strG);
        } catch (NumberFormatException unused) {
            return -1L;
        }
    }

    public static final List k(Object... elements) {
        o.h(elements, "elements");
        Object[] objArr = (Object[]) elements.clone();
        List listUnmodifiableList = Collections.unmodifiableList(AbstractC3252r.B(Arrays.copyOf(objArr, objArr.length)));
        o.g(listUnmodifiableList, "unmodifiableList(listOf(*elements.clone()))");
        return listUnmodifiableList;
    }

    public static final int l(String str) {
        int length = str.length();
        for (int i6 = 0; i6 < length; i6++) {
            char cCharAt = str.charAt(i6);
            if (o.i(cCharAt, 31) <= 0 || o.i(cCharAt, 127) >= 0) {
                return i6;
            }
        }
        return -1;
    }

    public static final int m(int i6, int i7, String str) {
        o.h(str, "<this>");
        while (i6 < i7) {
            char cCharAt = str.charAt(i6);
            if (cCharAt != '\t' && cCharAt != '\n' && cCharAt != '\f' && cCharAt != '\r' && cCharAt != ' ') {
                return i6;
            }
            i6++;
        }
        return i7;
    }

    public static final int n(int i6, int i7, String str) {
        o.h(str, "<this>");
        int i8 = i7 - 1;
        if (i6 <= i8) {
            while (true) {
                char cCharAt = str.charAt(i8);
                if (cCharAt != '\t' && cCharAt != '\n' && cCharAt != '\f' && cCharAt != '\r' && cCharAt != ' ') {
                    return i8 + 1;
                }
                if (i8 == i6) {
                    break;
                }
                i8--;
            }
        }
        return i6;
    }

    public static final String[] o(String[] strArr, String[] other, Comparator comparator) {
        o.h(other, "other");
        o.h(comparator, "comparator");
        ArrayList arrayList = new ArrayList();
        for (String str : strArr) {
            int length = other.length;
            int i6 = 0;
            while (true) {
                if (i6 >= length) {
                    break;
                }
                if (comparator.compare(str, other[i6]) == 0) {
                    arrayList.add(str);
                    break;
                }
                i6++;
            }
        }
        return (String[]) arrayList.toArray(new String[0]);
    }

    public static final boolean p(String name) {
        o.h(name, "name");
        return name.equalsIgnoreCase("Authorization") || name.equalsIgnoreCase("Cookie") || name.equalsIgnoreCase("Proxy-Authorization") || name.equalsIgnoreCase("Set-Cookie");
    }

    public static final int q(char c6) {
        if ('0' <= c6 && c6 < ':') {
            return c6 - '0';
        }
        if ('a' <= c6 && c6 < 'g') {
            return c6 - 'W';
        }
        if ('A' > c6 || c6 >= 'G') {
            return -1;
        }
        return c6 - '7';
    }

    public static final Charset r(InterfaceC2742l interfaceC2742l, Charset charset) {
        o.h(interfaceC2742l, "<this>");
        o.h(charset, "default");
        int iM = interfaceC2742l.m(f22182d);
        if (iM == -1) {
            return charset;
        }
        if (iM == 0) {
            Charset UTF_8 = StandardCharsets.UTF_8;
            o.g(UTF_8, "UTF_8");
            return UTF_8;
        }
        if (iM == 1) {
            Charset UTF_16BE = StandardCharsets.UTF_16BE;
            o.g(UTF_16BE, "UTF_16BE");
            return UTF_16BE;
        }
        if (iM == 2) {
            Charset UTF_16LE = StandardCharsets.UTF_16LE;
            o.g(UTF_16LE, "UTF_16LE");
            return UTF_16LE;
        }
        if (iM == 3) {
            Charset charset2 = a.f6257a;
            Charset charset3 = a.f6259c;
            if (charset3 != null) {
                return charset3;
            }
            Charset charsetForName = Charset.forName("UTF-32BE");
            o.g(charsetForName, "forName(...)");
            a.f6259c = charsetForName;
            return charsetForName;
        }
        if (iM != 4) {
            throw new AssertionError();
        }
        Charset charset4 = a.f6257a;
        Charset charset5 = a.f6258b;
        if (charset5 != null) {
            return charset5;
        }
        Charset charsetForName2 = Charset.forName("UTF-32LE");
        o.g(charsetForName2, "forName(...)");
        a.f6258b = charsetForName2;
        return charsetForName2;
    }

    public static final Object s(Class fieldType, Object instance, String str) throws IllegalAccessException {
        Object objCast;
        Object objS;
        o.h(instance, "instance");
        o.h(fieldType, "fieldType");
        Class<?> superclass = instance.getClass();
        while (true) {
            objCast = null;
            if (superclass.equals(Object.class)) {
                if (str.equals("delegate") || (objS = s(Object.class, instance, "delegate")) == null) {
                    return null;
                }
                return s(fieldType, objS, str);
            }
            try {
                Field declaredField = superclass.getDeclaredField(str);
                declaredField.setAccessible(true);
                Object obj = declaredField.get(instance);
                if (!fieldType.isInstance(obj)) {
                    break;
                }
                objCast = fieldType.cast(obj);
                break;
            } catch (NoSuchFieldException unused) {
                superclass = superclass.getSuperclass();
                o.g(superclass, "c.superclass");
            }
        }
        return objCast;
    }

    public static final int t(J j4) {
        o.h(j4, "<this>");
        return (j4.b() & 255) | ((j4.b() & 255) << 16) | ((j4.b() & 255) << 8);
    }

    public static final int u(C2740j c2740j) throws EOFException {
        int i6 = 0;
        while (!c2740j.d() && c2740j.f(0L) == 61) {
            i6++;
            c2740j.y();
        }
        return i6;
    }

    public static final boolean v(P p6, int i6) {
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        o.h(timeUnit, "timeUnit");
        long jNanoTime = System.nanoTime();
        long jC = p6.timeout().e() ? p6.timeout().c() - jNanoTime : Long.MAX_VALUE;
        p6.timeout().d(Math.min(jC, timeUnit.toNanos(i6)) + jNanoTime);
        try {
            C2740j c2740j = new C2740j();
            while (p6.read(c2740j, 8192L) != -1) {
                c2740j.a();
            }
            if (jC == Long.MAX_VALUE) {
                p6.timeout().a();
                return true;
            }
            p6.timeout().d(jNanoTime + jC);
            return true;
        } catch (InterruptedIOException unused) {
            if (jC == Long.MAX_VALUE) {
                p6.timeout().a();
                return false;
            }
            p6.timeout().d(jNanoTime + jC);
            return false;
        } catch (Throwable th) {
            if (jC == Long.MAX_VALUE) {
                p6.timeout().a();
            } else {
                p6.timeout().d(jNanoTime + jC);
            }
            throw th;
        }
    }

    public static final Headers w(List list) {
        Headers.Builder builder = new Headers.Builder();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Header header = (Header) it.next();
            builder.c(header.f22442a.t(), header.f22443b.t());
        }
        return builder.e();
    }

    public static final String x(HttpUrl httpUrl, boolean z6) {
        o.h(httpUrl, "<this>");
        String strK = httpUrl.f22027d;
        if (p.D0(strK, Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, false)) {
            strK = A0.a.k(']', "[", strK);
        }
        int i6 = httpUrl.f22028e;
        if (!z6) {
            HttpUrl.f22023k.getClass();
            if (i6 == HttpUrl.Companion.b(httpUrl.f22024a)) {
                return strK;
            }
        }
        return strK + ':' + i6;
    }

    public static final List y(List list) {
        o.h(list, "<this>");
        List listUnmodifiableList = Collections.unmodifiableList(AbstractC3251q.E0(list));
        o.g(listUnmodifiableList, "unmodifiableList(toMutableList())");
        return listUnmodifiableList;
    }

    public static final int z(String str, int i6) {
        if (str != null) {
            try {
                long j4 = Long.parseLong(str);
                if (j4 > 2147483647L) {
                    return Integer.MAX_VALUE;
                }
                if (j4 < 0) {
                    return 0;
                }
                return (int) j4;
            } catch (NumberFormatException unused) {
            }
        }
        return i6;
    }
}
