package kotlinx.coroutines.internal; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; 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 kotlin.ExceptionsKt__ExceptionsKt; import kotlin.collections.CollectionsKt__IterablesKt; import kotlin.collections.CollectionsKt__MutableCollectionsKt; import kotlin.collections.CollectionsKt___CollectionsKt; import kotlin.io.CloseableKt; import kotlin.jvm.internal.Intrinsics; import kotlin.text.StringsKt__StringsJVMKt; import kotlin.text.StringsKt__StringsKt; /* loaded from: classes5.dex */ public final class FastServiceLoader { public static final FastServiceLoader INSTANCE = new FastServiceLoader(); public final List loadMainDispatcherFactory$kotlinx_coroutines_core() { MainDispatcherFactory mainDispatcherFactory; if (!FastServiceLoaderKt.getANDROID_DETECTED()) { return load(MainDispatcherFactory.class, MainDispatcherFactory.class.getClassLoader()); } try { ArrayList arrayList = new ArrayList(2); MainDispatcherFactory mainDispatcherFactory2 = null; try { mainDispatcherFactory = (MainDispatcherFactory) MainDispatcherFactory.class.cast(Class.forName("kotlinx.coroutines.android.AndroidDispatcherFactory", true, MainDispatcherFactory.class.getClassLoader()).getDeclaredConstructor(new Class[0]).newInstance(new Object[0])); } catch (ClassNotFoundException unused) { mainDispatcherFactory = null; } if (mainDispatcherFactory != null) { arrayList.add(mainDispatcherFactory); } try { mainDispatcherFactory2 = (MainDispatcherFactory) MainDispatcherFactory.class.cast(Class.forName("kotlinx.coroutines.test.internal.TestMainDispatcherFactory", true, MainDispatcherFactory.class.getClassLoader()).getDeclaredConstructor(new Class[0]).newInstance(new Object[0])); } catch (ClassNotFoundException unused2) { } if (mainDispatcherFactory2 == null) { return arrayList; } arrayList.add(mainDispatcherFactory2); return arrayList; } catch (Throwable unused3) { return load(MainDispatcherFactory.class, MainDispatcherFactory.class.getClassLoader()); } } public final List load(Class cls, ClassLoader classLoader) { try { return loadProviders$kotlinx_coroutines_core(cls, classLoader); } catch (Throwable unused) { return CollectionsKt___CollectionsKt.toList(ServiceLoader.load(cls, classLoader)); } } public final List loadProviders$kotlinx_coroutines_core(Class cls, ClassLoader classLoader) { Set set; ArrayList list = Collections.list(classLoader.getResources("META-INF/services/" + cls.getName())); Intrinsics.checkNotNullExpressionValue(list, "list(this)"); ArrayList arrayList = new ArrayList(); Iterator it = list.iterator(); while (it.hasNext()) { CollectionsKt__MutableCollectionsKt.addAll(arrayList, INSTANCE.parse((URL) it.next())); } set = CollectionsKt___CollectionsKt.toSet(arrayList); if (!(!set.isEmpty())) { throw new IllegalArgumentException("No providers were loaded with FastServiceLoader".toString()); } ArrayList arrayList2 = new ArrayList(CollectionsKt__IterablesKt.collectionSizeOrDefault(set, 10)); Iterator it2 = set.iterator(); while (it2.hasNext()) { arrayList2.add(INSTANCE.getProviderInstance((String) it2.next(), classLoader, cls)); } return arrayList2; } public final Object getProviderInstance(String str, ClassLoader classLoader, Class cls) { Class cls2 = Class.forName(str, false, classLoader); if (!cls.isAssignableFrom(cls2)) { throw new IllegalArgumentException(("Expected service of class " + cls + ", but found " + cls2).toString()); } return cls.cast(cls2.getDeclaredConstructor(new Class[0]).newInstance(new Object[0])); } public final List parse(URL url) { BufferedReader bufferedReader; String substringAfter$default; String substringBefore$default; String substringAfter$default2; String url2 = url.toString(); if (StringsKt__StringsJVMKt.startsWith$default(url2, "jar", false, 2, null)) { substringAfter$default = StringsKt__StringsKt.substringAfter$default(url2, "jar:file:", (String) null, 2, (Object) null); substringBefore$default = StringsKt__StringsKt.substringBefore$default(substringAfter$default, '!', (String) null, 2, (Object) null); substringAfter$default2 = StringsKt__StringsKt.substringAfter$default(url2, "!/", (String) null, 2, (Object) null); JarFile jarFile = new JarFile(substringBefore$default, false); try { bufferedReader = new BufferedReader(new InputStreamReader(jarFile.getInputStream(new ZipEntry(substringAfter$default2)), "UTF-8")); try { List parseFile = INSTANCE.parseFile(bufferedReader); CloseableKt.closeFinally(bufferedReader, null); jarFile.close(); return parseFile; } finally { } } catch (Throwable th) { try { throw th; } catch (Throwable th2) { try { jarFile.close(); throw th2; } catch (Throwable th3) { ExceptionsKt__ExceptionsKt.addSuppressed(th, th3); throw th; } } } } else { bufferedReader = new BufferedReader(new InputStreamReader(FirebasePerfUrlConnection.openStream(url))); try { List parseFile2 = INSTANCE.parseFile(bufferedReader); CloseableKt.closeFinally(bufferedReader, null); return parseFile2; } catch (Throwable th4) { try { throw th4; } finally { } } } } public final List parseFile(BufferedReader bufferedReader) { String substringBefore$default; CharSequence trim; LinkedHashSet linkedHashSet = new LinkedHashSet(); while (true) { String readLine = bufferedReader.readLine(); if (readLine != null) { substringBefore$default = StringsKt__StringsKt.substringBefore$default(readLine, "#", (String) null, 2, (Object) null); trim = StringsKt__StringsKt.trim(substringBefore$default); String obj = trim.toString(); for (int i = 0; i < obj.length(); i++) { char charAt = obj.charAt(i); if (charAt != '.' && !Character.isJavaIdentifierPart(charAt)) { throw new IllegalArgumentException(("Illegal service provider class name: " + obj).toString()); } } if (obj.length() > 0) { linkedHashSet.add(obj); } } else { return CollectionsKt___CollectionsKt.toList(linkedHashSet); } } } }