package io.netty.util.internal;

import com.cvte.vman.instance.InvokeCmd;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
public final class NativeLibraryLoader {
    private static final String NATIVE_RESOURCE_HOME = "META-INF/native/";
    private static final File WORKDIR;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) NativeLibraryLoader.class);
    private static final String OSNAME = SystemPropertyUtil.get("os.name", "").toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", "");

    static {
        String str = SystemPropertyUtil.get("io.netty.native.workdir");
        if (str == null) {
            WORKDIR = tmpdir();
            logger.debug("-Dio.netty.native.workdir: " + WORKDIR + " (io.netty.tmpdir)");
            return;
        }
        File file = new File(str);
        file.mkdirs();
        try {
            file = file.getAbsoluteFile();
        } catch (Exception unused) {
        }
        WORKDIR = file;
        logger.debug("-Dio.netty.native.workdir: " + WORKDIR);
    }

    private NativeLibraryLoader() {
    }

    private static boolean isOSX() {
        return OSNAME.startsWith("macosx") || OSNAME.startsWith("osx");
    }

    private static boolean isWindows() {
        return OSNAME.startsWith("windows");
    }

    public static void load(String str, ClassLoader classLoader) throws Throwable {
        File fileCreateTempFile;
        FileOutputStream fileOutputStream;
        InputStream inputStreamOpenStream;
        Exception e2;
        byte[] bArr;
        String strMapLibraryName = System.mapLibraryName(str);
        String str2 = NATIVE_RESOURCE_HOME + strMapLibraryName;
        URL resource = classLoader.getResource(str2);
        if (resource == null && isOSX()) {
            if (str2.endsWith(".jnilib")) {
                resource = classLoader.getResource("META-INF/native/lib" + str + ".dynlib");
            } else {
                resource = classLoader.getResource("META-INF/native/lib" + str + ".jnilib");
            }
        }
        if (resource == null) {
            System.loadLibrary(str);
            return;
        }
        int iLastIndexOf = strMapLibraryName.lastIndexOf(46);
        String strSubstring = strMapLibraryName.substring(0, iLastIndexOf);
        String strSubstring2 = strMapLibraryName.substring(iLastIndexOf, strMapLibraryName.length());
        InputStream inputStream = null;
        try {
            fileCreateTempFile = File.createTempFile(strSubstring, strSubstring2, WORKDIR);
            try {
                inputStreamOpenStream = resource.openStream();
            } catch (Exception e3) {
                e = e3;
                inputStreamOpenStream = null;
                fileOutputStream = null;
            } catch (Throwable th) {
                th = th;
                fileOutputStream = null;
            }
            try {
                fileOutputStream = new FileOutputStream(fileCreateTempFile);
                try {
                    try {
                        bArr = new byte[InvokeCmd.CMD_TOUCH_CTRL_START];
                    } catch (Exception e4) {
                        e2 = e4;
                        throw ((UnsatisfiedLinkError) new UnsatisfiedLinkError("could not load a native library: " + str).initCause(e2));
                    }
                } catch (Throwable th2) {
                    th = th2;
                }
            } catch (Exception e5) {
                e = e5;
                fileOutputStream = null;
                e2 = e;
                throw ((UnsatisfiedLinkError) new UnsatisfiedLinkError("could not load a native library: " + str).initCause(e2));
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = null;
            }
        } catch (Exception e6) {
            inputStreamOpenStream = null;
            fileOutputStream = null;
            e2 = e6;
            fileCreateTempFile = null;
        } catch (Throwable th4) {
            th = th4;
            fileCreateTempFile = null;
            fileOutputStream = null;
        }
        while (true) {
            int i2 = inputStreamOpenStream.read(bArr);
            if (i2 <= 0) {
                break;
            } else {
                fileOutputStream.write(bArr, 0, i2);
            }
            th = th2;
            inputStream = inputStreamOpenStream;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException unused) {
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException unused2) {
                }
            }
            if (fileCreateTempFile == null || fileCreateTempFile.delete()) {
                throw th;
            }
            fileCreateTempFile.deleteOnExit();
            throw th;
        }
        fileOutputStream.flush();
        fileOutputStream.close();
        System.load(fileCreateTempFile.getPath());
        if (inputStreamOpenStream != null) {
            try {
                inputStreamOpenStream.close();
            } catch (IOException unused3) {
            }
        }
        if (fileCreateTempFile != null) {
            fileCreateTempFile.deleteOnExit();
        }
    }

    public static void loadFirstAvailable(ClassLoader classLoader, String... strArr) {
        for (String str : strArr) {
            try {
                load(str, classLoader);
                return;
            } catch (Throwable th) {
                logger.debug("Unable to load the library: " + str + '.', th);
            }
        }
        throw new IllegalArgumentException("Failed to load any of the given libraries: " + Arrays.toString(strArr));
    }

    private static File tmpdir() {
        File directory;
        try {
            directory = toDirectory(SystemPropertyUtil.get("io.netty.tmpdir"));
        } catch (Exception unused) {
        }
        if (directory != null) {
            logger.debug("-Dio.netty.tmpdir: " + directory);
            return directory;
        }
        File directory2 = toDirectory(SystemPropertyUtil.get("java.io.tmpdir"));
        if (directory2 != null) {
            logger.debug("-Dio.netty.tmpdir: " + directory2 + " (java.io.tmpdir)");
            return directory2;
        }
        if (isWindows()) {
            File directory3 = toDirectory(System.getenv("TEMP"));
            if (directory3 != null) {
                logger.debug("-Dio.netty.tmpdir: " + directory3 + " (%TEMP%)");
                return directory3;
            }
            String str = System.getenv("USERPROFILE");
            if (str != null) {
                File directory4 = toDirectory(str + "\\AppData\\Local\\Temp");
                if (directory4 != null) {
                    logger.debug("-Dio.netty.tmpdir: " + directory4 + " (%USERPROFILE%\\AppData\\Local\\Temp)");
                    return directory4;
                }
                File directory5 = toDirectory(str + "\\Local Settings\\Temp");
                if (directory5 != null) {
                    logger.debug("-Dio.netty.tmpdir: " + directory5 + " (%USERPROFILE%\\Local Settings\\Temp)");
                    return directory5;
                }
            }
        } else {
            File directory6 = toDirectory(System.getenv("TMPDIR"));
            if (directory6 != null) {
                logger.debug("-Dio.netty.tmpdir: " + directory6 + " ($TMPDIR)");
                return directory6;
            }
        }
        File file = isWindows() ? new File("C:\\Windows\\Temp") : new File("/tmp");
        logger.warn("Failed to get the temporary directory; falling back to: " + file);
        return file;
    }

    private static File toDirectory(String str) {
        if (str == null) {
            return null;
        }
        File file = new File(str);
        file.mkdirs();
        if (!file.isDirectory()) {
            return null;
        }
        try {
            return file.getAbsoluteFile();
        } catch (Exception unused) {
            return file;
        }
    }
}
