package g.i0;

import com.seewo.libmcuservice.constant.IMcuConstants;
import g.e0;
import g.f0;
import g.r;
import g.s;
import h.f;
import h.i;
import h.x;
import h.y;
import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.reflect.Method;
import java.net.IDN;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes.dex */
public final class c {

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final String[] f6155b = new String[0];

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

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final i f6161h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final Charset f6162i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final Charset f6163j;
    public static final Charset k;
    public static final Charset l;
    public static final Charset m;
    public static final TimeZone n;
    public static final Comparator<String> o;
    public static final Method p;
    public static final Pattern q;

    public class a implements Comparator<String> {
        @Override // java.util.Comparator
        public int compare(String str, String str2) {
            return str.compareTo(str2);
        }
    }

    static {
        byte[] bArr = new byte[0];
        f6154a = bArr;
        f fVar = new f();
        fVar.X(bArr);
        long j2 = 0;
        Method declaredMethod = null;
        f6156c = new e0(null, j2, fVar);
        c(j2, j2, j2);
        f6157d = i.b("efbbbf");
        f6158e = i.b("feff");
        f6159f = i.b("fffe");
        f6160g = i.b("0000ffff");
        f6161h = i.b("ffff0000");
        f6162i = Charset.forName("UTF-8");
        Charset.forName("ISO-8859-1");
        f6163j = Charset.forName("UTF-16BE");
        k = Charset.forName("UTF-16LE");
        l = Charset.forName("UTF-32BE");
        m = Charset.forName("UTF-32LE");
        n = TimeZone.getTimeZone("GMT");
        o = new a();
        try {
            declaredMethod = Throwable.class.getDeclaredMethod("addSuppressed", Throwable.class);
        } catch (Exception unused) {
        }
        p = declaredMethod;
        q = Pattern.compile("([0-9a-fA-F]*:[0-9a-fA-F:.]*)|([\\d.]+)");
    }

    public static AssertionError a(String str, Exception exc) {
        AssertionError assertionError = new AssertionError(str);
        try {
            assertionError.initCause(exc);
        } catch (IllegalStateException unused) {
        }
        return assertionError;
    }

    public static String b(String str) {
        int i2 = -1;
        int i3 = 0;
        if (!str.contains(":")) {
            try {
                String lowerCase = IDN.toASCII(str).toLowerCase(Locale.US);
                if (lowerCase.isEmpty()) {
                    return null;
                }
                for (int i4 = 0; i4 < lowerCase.length(); i4++) {
                    char cCharAt = lowerCase.charAt(i4);
                    if (cCharAt > 31 && cCharAt < 127 && " #%/:?@[\\]".indexOf(cCharAt) == -1) {
                    }
                    i3 = 1;
                }
                if (i3 != 0) {
                    return null;
                }
                return lowerCase;
            } catch (IllegalArgumentException unused) {
                return null;
            }
        }
        InetAddress inetAddressG = (str.startsWith("[") && str.endsWith("]")) ? g(str, 1, str.length() - 1) : g(str, 0, str.length());
        if (inetAddressG == null) {
            return null;
        }
        byte[] address = inetAddressG.getAddress();
        if (address.length != 16) {
            throw new AssertionError(a.b.c.a.a.q("Invalid IPv6 address: '", str, "'"));
        }
        int i5 = 0;
        int i6 = 0;
        while (i5 < address.length) {
            int i7 = i5;
            while (i7 < 16 && address[i7] == 0 && address[i7 + 1] == 0) {
                i7 += 2;
            }
            int i8 = i7 - i5;
            if (i8 > i6 && i8 >= 4) {
                i2 = i5;
                i6 = i8;
            }
            i5 = i7 + 2;
        }
        f fVar = new f();
        while (i3 < address.length) {
            if (i3 == i2) {
                fVar.Z(58);
                i3 += i6;
                if (i3 == 16) {
                    fVar.Z(58);
                }
            } else {
                if (i3 > 0) {
                    fVar.Z(58);
                }
                fVar.j(((address[i3] & IMcuConstants.DEFAULT_VALUE) << 8) | (address[i3 + 1] & IMcuConstants.DEFAULT_VALUE));
                i3 += 2;
            }
        }
        return fVar.T();
    }

    public static void c(long j2, long j3, long j4) {
        if ((j3 | j4) < 0 || j3 > j2 || j2 - j3 < j4) {
            throw new ArrayIndexOutOfBoundsException();
        }
    }

    public static void d(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (RuntimeException e2) {
                throw e2;
            } catch (Exception unused) {
            }
        }
    }

    public static void e(Socket socket) {
        if (socket != null) {
            try {
                socket.close();
            } catch (AssertionError e2) {
                if (!r(e2)) {
                    throw e2;
                }
            } catch (RuntimeException e3) {
                throw e3;
            } catch (Exception unused) {
            }
        }
    }

    public static int f(char c2) {
        if (c2 >= '0' && c2 <= '9') {
            return c2 - '0';
        }
        char c3 = 'a';
        if (c2 < 'a' || c2 > 'f') {
            c3 = 'A';
            if (c2 < 'A' || c2 > 'F') {
                return -1;
            }
        }
        return (c2 - c3) + 10;
    }

    /* JADX WARN: Code restructure failed: missing block: B:52:0x009a, code lost:
    
        r14 = 0;
     */
    /* JADX WARN: Code restructure failed: missing block: B:70:0x00d9, code lost:
    
        return null;
     */
    /* JADX WARN: Removed duplicated region for block: B:61:0x00ab  */
    @javax.annotation.Nullable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public static java.net.InetAddress g(java.lang.String r16, int r17, int r18) {
        /*
            Method dump skipped, instruction units count: 250
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: g.i0.c.g(java.lang.String, int, int):java.net.InetAddress");
    }

    public static int h(String str, int i2, int i3, char c2) {
        while (i2 < i3) {
            if (str.charAt(i2) == c2) {
                return i2;
            }
            i2++;
        }
        return i3;
    }

    public static int i(String str, int i2, int i3, String str2) {
        while (i2 < i3) {
            if (str2.indexOf(str.charAt(i2)) != -1) {
                return i2;
            }
            i2++;
        }
        return i3;
    }

    public static boolean j(x xVar, int i2, TimeUnit timeUnit) {
        try {
            return t(xVar, i2, timeUnit);
        } catch (IOException unused) {
            return false;
        }
    }

    public static boolean k(Object obj, Object obj2) {
        return obj == obj2 || (obj != null && obj.equals(obj2));
    }

    public static String l(String str, Object... objArr) {
        return String.format(Locale.US, str, objArr);
    }

    public static String m(s sVar, boolean z) {
        String strU = sVar.f6457e.contains(":") ? a.b.c.a.a.u(a.b.c.a.a.C("["), sVar.f6457e, "]") : sVar.f6457e;
        if (!z && sVar.f6458f == s.d(sVar.f6454b)) {
            return strU;
        }
        StringBuilder sbE = a.b.c.a.a.E(strU, ":");
        sbE.append(sVar.f6458f);
        return sbE.toString();
    }

    public static <T> List<T> n(List<T> list) {
        return Collections.unmodifiableList(new ArrayList(list));
    }

    public static <T> List<T> o(T... tArr) {
        return Collections.unmodifiableList(Arrays.asList((Object[]) tArr.clone()));
    }

    public static int p(String str) {
        int length = str.length();
        for (int i2 = 0; i2 < length; i2++) {
            char cCharAt = str.charAt(i2);
            if (cCharAt <= 31 || cCharAt >= 127) {
                return i2;
            }
        }
        return -1;
    }

    public static String[] q(Comparator<? super String> comparator, String[] strArr, String[] strArr2) {
        ArrayList arrayList = new ArrayList();
        for (String str : strArr) {
            int length = strArr2.length;
            int i2 = 0;
            while (true) {
                if (i2 >= length) {
                    break;
                }
                if (comparator.compare(str, strArr2[i2]) == 0) {
                    arrayList.add(str);
                    break;
                }
                i2++;
            }
        }
        return (String[]) arrayList.toArray(new String[arrayList.size()]);
    }

    public static boolean r(AssertionError assertionError) {
        return (assertionError.getCause() == null || assertionError.getMessage() == null || !assertionError.getMessage().contains("getsockname failed")) ? false : true;
    }

    public static boolean s(Comparator<String> comparator, String[] strArr, String[] strArr2) {
        if (strArr != null && strArr2 != null && strArr.length != 0 && strArr2.length != 0) {
            for (String str : strArr) {
                for (String str2 : strArr2) {
                    if (comparator.compare(str, str2) == 0) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static boolean t(x xVar, int i2, TimeUnit timeUnit) {
        long jNanoTime = System.nanoTime();
        long jC = xVar.e().e() ? xVar.e().c() - jNanoTime : Long.MAX_VALUE;
        xVar.e().d(Math.min(jC, timeUnit.toNanos(i2)) + jNanoTime);
        try {
            f fVar = new f();
            while (xVar.v(fVar, 8192L) != -1) {
                fVar.K();
            }
            y yVarE = xVar.e();
            if (jC == Long.MAX_VALUE) {
                yVarE.a();
            } else {
                yVarE.d(jNanoTime + jC);
            }
            return true;
        } catch (InterruptedIOException unused) {
            y yVarE2 = xVar.e();
            if (jC == Long.MAX_VALUE) {
                yVarE2.a();
            } else {
                yVarE2.d(jNanoTime + jC);
            }
            return false;
        } catch (Throwable th) {
            y yVarE3 = xVar.e();
            if (jC == Long.MAX_VALUE) {
                yVarE3.a();
            } else {
                yVarE3.d(jNanoTime + jC);
            }
            throw th;
        }
    }

    public static int u(String str, int i2, int i3) {
        while (i2 < i3) {
            char cCharAt = str.charAt(i2);
            if (cCharAt != '\t' && cCharAt != '\n' && cCharAt != '\f' && cCharAt != '\r' && cCharAt != ' ') {
                return i2;
            }
            i2++;
        }
        return i3;
    }

    public static int v(String str, int i2, int i3) {
        for (int i4 = i3 - 1; i4 >= i2; i4--) {
            char cCharAt = str.charAt(i4);
            if (cCharAt != '\t' && cCharAt != '\n' && cCharAt != '\f' && cCharAt != '\r' && cCharAt != ' ') {
                return i4 + 1;
            }
        }
        return i2;
    }

    public static r w(List<g.i0.i.c> list) {
        r.a aVar = new r.a();
        for (g.i0.i.c cVar : list) {
            g.i0.a.f6152a.a(aVar, cVar.f6261g.o(), cVar.f6262h.o());
        }
        return new r(aVar);
    }

    public static String x(String str, int i2, int i3) {
        int iU = u(str, i2, i3);
        return str.substring(iU, v(str, iU, i3));
    }
}
