package l7;

import B6.k;
import B6.l;
import P6.g;
import P6.p;
import X6.d;
import X6.e;
import X6.n;
import d3.E;
import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
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 k7.C;
import k7.D;
import k7.o;
import k7.q;
import r7.C1512b;
import v0.AbstractC1664c;
import x7.B;
import x7.C1731g;
import x7.C1734j;
import x7.H;
import x7.InterfaceC1733i;
import x7.w;

/* JADX INFO: loaded from: classes.dex */
public abstract class b {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final o f14413b = W3.b.q(new String[0]);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final D f14414c;

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

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

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

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

    static {
        byte[] bArr = new byte[0];
        f14412a = bArr;
        C1731g c1731g = new C1731g();
        c1731g.s0(bArr, 0, 0);
        long j8 = 0;
        f14414c = new D(null, j8, c1731g, 0);
        c(j8, j8, j8);
        C1734j c1734j = C1734j.f18766t;
        f14415d = AbstractC1664c.w(E.d("efbbbf"), E.d("feff"), E.d("fffe"), E.d("0000ffff"), E.d("ffff0000"));
        TimeZone timeZone = TimeZone.getTimeZone("GMT");
        g.b(timeZone);
        f14416e = timeZone;
        f14417f = new d("([0-9a-fA-F]*:[0-9a-fA-F:.]*)|([\\d.]+)");
        String strZ = e.Z(k7.w.class.getName(), "okhttp3.");
        if (n.H(strZ, "Client", false)) {
            strZ = strZ.substring(0, strZ.length() - 6);
            g.d(strZ, "substring(...)");
        }
        f14418g = strZ;
    }

    public static final boolean a(q qVar, q qVar2) {
        g.e(qVar, "<this>");
        g.e(qVar2, "other");
        return g.a(qVar.f14124d, qVar2.f14124d) && qVar.f14125e == qVar2.f14125e && g.a(qVar.f14121a, qVar2.f14121a);
    }

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

    public static final void c(long j8, long j9, long j10) {
        if ((j9 | j10) < 0 || j9 > j8 || j8 - j9 < j10) {
            throw new ArrayIndexOutOfBoundsException();
        }
    }

    public static final void d(Closeable closeable) {
        g.e(closeable, "<this>");
        try {
            closeable.close();
        } catch (RuntimeException e8) {
            throw e8;
        } catch (Exception unused) {
        }
    }

    public static final void e(Socket socket) {
        g.e(socket, "<this>");
        try {
            socket.close();
        } catch (AssertionError e8) {
            throw e8;
        } catch (RuntimeException e9) {
            if (!g.a(e9.getMessage(), "bio == null")) {
                throw e9;
            }
        } catch (Exception unused) {
        }
    }

    public static final int f(String str, char c3, int i, int i5) {
        g.e(str, "<this>");
        while (i < i5) {
            if (str.charAt(i) == c3) {
                return i;
            }
            i++;
        }
        return i5;
    }

    public static final int g(String str, int i, int i5, String str2) {
        g.e(str, "<this>");
        while (i < i5) {
            if (e.N(str2, str.charAt(i))) {
                return i;
            }
            i++;
        }
        return i5;
    }

    public static final boolean h(H h8) {
        g.e(TimeUnit.MILLISECONDS, "timeUnit");
        try {
            return u(h8, 100);
        } catch (IOException unused) {
            return false;
        }
    }

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

    public static final boolean j(String[] strArr, String[] strArr2, Comparator comparator) {
        g.e(strArr, "<this>");
        if (strArr.length != 0 && strArr2 != null && strArr2.length != 0) {
            for (String str : strArr) {
                B6.b bVarD = p.d(strArr2);
                while (bVarD.hasNext()) {
                    if (comparator.compare(str, (String) bVarD.next()) == 0) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static final long k(C c3) {
        String strA = c3.f14015v.a("Content-Length");
        if (strA == null) {
            return -1L;
        }
        try {
            return Long.parseLong(strA);
        } catch (NumberFormatException unused) {
            return -1L;
        }
    }

    public static final List l(Object... objArr) {
        g.e(objArr, "elements");
        Object[] objArr2 = (Object[]) objArr.clone();
        List listUnmodifiableList = Collections.unmodifiableList(l.W(Arrays.copyOf(objArr2, objArr2.length)));
        g.d(listUnmodifiableList, "unmodifiableList(listOf(*elements.clone()))");
        return listUnmodifiableList;
    }

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

    public static final int n(int i, int i5, String str) {
        g.e(str, "<this>");
        while (i < i5) {
            char cCharAt = str.charAt(i);
            if (cCharAt != '\t' && cCharAt != '\n' && cCharAt != '\f' && cCharAt != '\r' && cCharAt != ' ') {
                return i;
            }
            i++;
        }
        return i5;
    }

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

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

    public static final boolean q(String str) {
        g.e(str, "name");
        return str.equalsIgnoreCase("Authorization") || str.equalsIgnoreCase("Cookie") || str.equalsIgnoreCase("Proxy-Authorization") || str.equalsIgnoreCase("Set-Cookie");
    }

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

    public static final Charset s(InterfaceC1733i interfaceC1733i, Charset charset) {
        g.e(interfaceC1733i, "<this>");
        g.e(charset, "default");
        int iT = interfaceC1733i.t(f14415d);
        if (iT == -1) {
            return charset;
        }
        if (iT == 0) {
            Charset charset2 = StandardCharsets.UTF_8;
            g.d(charset2, "UTF_8");
            return charset2;
        }
        if (iT == 1) {
            Charset charset3 = StandardCharsets.UTF_16BE;
            g.d(charset3, "UTF_16BE");
            return charset3;
        }
        if (iT == 2) {
            Charset charset4 = StandardCharsets.UTF_16LE;
            g.d(charset4, "UTF_16LE");
            return charset4;
        }
        if (iT == 3) {
            Charset charset5 = X6.a.f7235a;
            Charset charset6 = X6.a.f7237c;
            if (charset6 != null) {
                return charset6;
            }
            Charset charsetForName = Charset.forName("UTF-32BE");
            g.d(charsetForName, "forName(...)");
            X6.a.f7237c = charsetForName;
            return charsetForName;
        }
        if (iT != 4) {
            throw new AssertionError();
        }
        Charset charset7 = X6.a.f7235a;
        Charset charset8 = X6.a.f7236b;
        if (charset8 != null) {
            return charset8;
        }
        Charset charsetForName2 = Charset.forName("UTF-32LE");
        g.d(charsetForName2, "forName(...)");
        X6.a.f7236b = charsetForName2;
        return charsetForName2;
    }

    public static final int t(B b8) {
        g.e(b8, "<this>");
        return (b8.g() & 255) | ((b8.g() & 255) << 16) | ((b8.g() & 255) << 8);
    }

    public static final boolean u(H h8, int i) {
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        g.e(timeUnit, "timeUnit");
        long jNanoTime = System.nanoTime();
        long jC = h8.f().e() ? h8.f().c() - jNanoTime : Long.MAX_VALUE;
        h8.f().d(Math.min(jC, timeUnit.toNanos(i)) + jNanoTime);
        try {
            C1731g c1731g = new C1731g();
            while (h8.M(c1731g, 8192L) != -1) {
                c1731g.m();
            }
            if (jC == Long.MAX_VALUE) {
                h8.f().a();
                return true;
            }
            h8.f().d(jNanoTime + jC);
            return true;
        } catch (InterruptedIOException unused) {
            if (jC == Long.MAX_VALUE) {
                h8.f().a();
                return false;
            }
            h8.f().d(jNanoTime + jC);
            return false;
        } catch (Throwable th) {
            if (jC == Long.MAX_VALUE) {
                h8.f().a();
            } else {
                h8.f().d(jNanoTime + jC);
            }
            throw th;
        }
    }

    public static final o v(List list) {
        ArrayList arrayList = new ArrayList(20);
        Iterator it = list.iterator();
        while (it.hasNext()) {
            C1512b c1512b = (C1512b) it.next();
            String strP = c1512b.f16588a.p();
            String strP2 = c1512b.f16589b.p();
            arrayList.add(strP);
            arrayList.add(e.j0(strP2).toString());
        }
        return new o((String[]) arrayList.toArray(new String[0]));
    }

    public static final String w(q qVar, boolean z8) {
        g.e(qVar, "<this>");
        String str = qVar.f14124d;
        if (e.O(str, ":")) {
            str = "[" + str + ']';
        }
        int i = qVar.f14125e;
        if (!z8) {
            String str2 = qVar.f14121a;
            g.e(str2, "scheme");
            if (i == (str2.equals("http") ? 80 : str2.equals("https") ? 443 : -1)) {
                return str;
            }
        }
        return str + ':' + i;
    }

    public static final List x(List list) {
        g.e(list, "<this>");
        List listUnmodifiableList = Collections.unmodifiableList(k.y0(list));
        g.d(listUnmodifiableList, "unmodifiableList(toMutableList())");
        return listUnmodifiableList;
    }

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

    public static final String z(int i, int i5, String str) {
        int iN = n(i, i5, str);
        String strSubstring = str.substring(iN, o(iN, i5, str));
        g.d(strSubstring, "this as java.lang.String…ing(startIndex, endIndex)");
        return strSubstring;
    }
}
