package kotlinx.coroutines.internal;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import p006b1.C0471a;
import p009c1.C0503h;
import p009c1.C0507l;
import p009c1.C0510o;
import p030j1.C1268a;
import p039m1.C1470g;
import p057s1.C1630l;
import p057s1.C1631m;

/* renamed from: kotlinx.coroutines.internal.f */
/* loaded from: classes.dex */
public final class C1363f {

    /* renamed from: a */
    public static final C1363f f3034a = new C1363f();

    /* renamed from: a */
    public final <S> S m1541a(String str, ClassLoader classLoader, Class<S> cls) {
        Class<?> cls2 = Class.forName(str, false, classLoader);
        if (cls.isAssignableFrom(cls2)) {
            return cls.cast(cls2.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]));
        }
        throw new IllegalArgumentException(("Expected service of class " + cls + ", but found " + cls2).toString());
    }

    /* renamed from: b */
    public final <S> List<S> m1540b(Class<S> cls, ClassLoader classLoader) {
        try {
            return m1538d(cls, classLoader);
        } catch (Throwable unused) {
            return C0510o.m3602i(ServiceLoader.load(cls, classLoader));
        }
    }

    /* renamed from: c */
    public final List<InterfaceC1376o> m1539c() {
        InterfaceC1376o interfaceC1376o;
        if (C1364g.m1535a()) {
            try {
                ArrayList arrayList = new ArrayList(2);
                InterfaceC1376o interfaceC1376o2 = null;
                try {
                    interfaceC1376o = (InterfaceC1376o) InterfaceC1376o.class.cast(Class.forName("kotlinx.coroutines.android.AndroidDispatcherFactory", true, InterfaceC1376o.class.getClassLoader()).getDeclaredConstructor(new Class[0]).newInstance(new Object[0]));
                } catch (ClassNotFoundException unused) {
                    interfaceC1376o = null;
                }
                if (interfaceC1376o != null) {
                    arrayList.add(interfaceC1376o);
                }
                try {
                    interfaceC1376o2 = (InterfaceC1376o) InterfaceC1376o.class.cast(Class.forName("kotlinx.coroutines.test.internal.TestMainDispatcherFactory", true, InterfaceC1376o.class.getClassLoader()).getDeclaredConstructor(new Class[0]).newInstance(new Object[0]));
                } catch (ClassNotFoundException unused2) {
                }
                if (interfaceC1376o2 == null) {
                    return arrayList;
                }
                arrayList.add(interfaceC1376o2);
                return arrayList;
            } catch (Throwable unused3) {
                return m1540b(InterfaceC1376o.class, InterfaceC1376o.class.getClassLoader());
            }
        }
        return m1540b(InterfaceC1376o.class, InterfaceC1376o.class.getClassLoader());
    }

    /* renamed from: d */
    public final <S> List<S> m1538d(Class<S> cls, ClassLoader classLoader) {
        ArrayList<URL> list = Collections.list(classLoader.getResources(C1470g.m1287j("META-INF/services/", cls.getName())));
        C1470g.m1293d(list, "list(this)");
        ArrayList arrayList = new ArrayList();
        for (URL url : list) {
            C0507l.m3604g(arrayList, f3034a.m1537e(url));
        }
        Set<String> m3599l = C0510o.m3599l(arrayList);
        if (!m3599l.isEmpty()) {
            ArrayList arrayList2 = new ArrayList(C0503h.m3605f(m3599l, 10));
            for (String str : m3599l) {
                arrayList2.add(f3034a.m1541a(str, classLoader, cls));
            }
            return arrayList2;
        }
        throw new IllegalArgumentException("No providers were loaded with FastServiceLoader".toString());
    }

    /* renamed from: e */
    public final List<String> m1537e(URL url) {
        String url2 = url.toString();
        if (!C1630l.m780e(url2, "jar", false, 2, null)) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
            try {
                List<String> m1536f = f3034a.m1536f(bufferedReader);
                C1268a.m1749a(bufferedReader, null);
                return m1536f;
            } catch (Throwable th) {
                try {
                    throw th;
                } catch (Throwable th2) {
                    C1268a.m1749a(bufferedReader, th);
                    throw th2;
                }
            }
        }
        String m761x = C1631m.m761x(C1631m.m766s(url2, "jar:file:", null, 2, null), '!', null, 2, null);
        String m766s = C1631m.m766s(url2, "!/", null, 2, null);
        JarFile jarFile = new JarFile(m761x, false);
        try {
            BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(jarFile.getInputStream(new ZipEntry(m766s)), "UTF-8"));
            List<String> m1536f2 = f3034a.m1536f(bufferedReader2);
            C1268a.m1749a(bufferedReader2, null);
            jarFile.close();
            return m1536f2;
        } catch (Throwable th3) {
            try {
                throw th3;
            } catch (Throwable th4) {
                try {
                    jarFile.close();
                    throw th4;
                } catch (Throwable th5) {
                    C0471a.m3632a(th3, th5);
                    throw th3;
                }
            }
        }
    }

    /* renamed from: f */
    public final List<String> m1536f(BufferedReader bufferedReader) {
        boolean z;
        boolean z2;
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine == null) {
                return C0510o.m3602i(linkedHashSet);
            }
            String obj = C1631m.m759z(C1631m.m760y(readLine, "#", null, 2, null)).toString();
            int i = 0;
            while (true) {
                if (i >= obj.length()) {
                    z = true;
                    break;
                }
                char charAt = obj.charAt(i);
                i++;
                if (charAt == '.' || Character.isJavaIdentifierPart(charAt)) {
                    z2 = true;
                    continue;
                } else {
                    z2 = false;
                    continue;
                }
                if (!z2) {
                    z = false;
                    break;
                }
            }
            if (!z) {
                throw new IllegalArgumentException(C1470g.m1287j("Illegal service provider class name: ", obj).toString());
            }
            if (obj.length() > 0) {
                linkedHashSet.add(obj);
            }
        }
    }
}