package com.ss.android.ugc.effectmanager.common.utils;

import X.0HH;
import X.4N2;
import X.7y5;
import X.8Av;
import X.8Kc;
import X.8n2;
import X.8r0;
import X.8r1;
import X.GhQ;
import X.M12;
import X.Q39;
import android.os.Environment;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.abmock.SettingsManager;
import com.ss.android.ugc.effectmanager.common.download.DownloadListener;
import com.ss.android.ugc.effectmanager.common.logger.EPLog;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import kotlin.h.b.n;
import kotlin.o.y;

public final class FileUtils {
    public static final int BUFFER_SIZE = BUFFER_SIZE;
    public static final FileUtils INSTANCE = new FileUtils();
    public static final Charset US_ASCII = Charset.forName("US-ASCII");
    public static final Charset UTF_8 = Charset.forName("UTF-8");

    public final int getBUFFER_SIZE() {
        return BUFFER_SIZE;
    }

    public final Charset getUS_ASCII() {
        return US_ASCII;
    }

    public final Charset getUTF_8() {
        return UTF_8;
    }

    public final boolean isSdcardWritable() {
        try {
            return n.LIZ("mounted", INVOKESTATIC_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_lancet_process_AwemeMetaDataLancet_getExternalStorageState());
        } catch (Exception unused) {
            return false;
        }
    }

    public static String INVOKESTATIC_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_lancet_process_AwemeMetaDataLancet_getExternalStorageState() {
        if (8n2.LJIIIZ && !TextUtils.isEmpty(8n2.LJIJI)) {
            return 8n2.LJIJI;
        }
        String externalStorageState = Environment.getExternalStorageState();
        8n2.LJIJI = externalStorageState;
        return externalStorageState;
    }

    public final boolean isSdcardAvailable() {
        String INVOKESTATIC_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_lancet_process_AwemeMetaDataLancet_getExternalStorageState = INVOKESTATIC_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_lancet_process_AwemeMetaDataLancet_getExternalStorageState();
        if (n.LIZ("mounted", INVOKESTATIC_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_lancet_process_AwemeMetaDataLancet_getExternalStorageState) || n.LIZ("mounted_ro", INVOKESTATIC_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_lancet_process_AwemeMetaDataLancet_getExternalStorageState)) {
            return true;
        }
        return false;
    }

    static {
        Covode.recordClassIndex(125320);
    }

    public final void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e2) {
                0HH.LIZ(e2);
            }
        }
    }

    public final boolean deleteDir(File file) {
        try {
            deleteContents(file);
            if (file != null) {
                return INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(file);
            }
        } catch (IOException unused) {
        }
        return false;
    }

    public final boolean checkFileExists(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return new File(str).exists();
    }

    public final boolean ensureDirExists(File file) {
        M12.LIZ(file);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file.exists();
    }

    public final synchronized void removeDir(String str) {
        MethodCollector.i(16610);
        if (str == null) {
            MethodCollector.o(16610);
            return;
        }
        removeDir(new File(str));
        MethodCollector.o(16610);
    }

    public final boolean removeFile(String str) {
        if (str == null) {
            return false;
        }
        File file = new File(str);
        if (!file.exists() || !file.canWrite() || !INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(file)) {
            return false;
        }
        return true;
    }

    public final OutputStream getFileOutputStream(String str) {
        MethodCollector.i(17414);
        FileOutputStream fileOutputStream = null;
        if (str == null) {
            MethodCollector.o(17414);
            return null;
        }
        File parentFile = new File(str).getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        try {
            fileOutputStream = new FileOutputStream(str);
        } catch (Exception e) {
            0HH.LIZ(e);
        }
        MethodCollector.o(17414);
        return fileOutputStream;
    }

    public final InputStream getFileStream(String str) {
        MethodCollector.i(16608);
        FileInputStream fileInputStream = null;
        if (str == null) {
            MethodCollector.o(16608);
            return null;
        }
        File file = new File(str);
        if (checkFileExists(file.getPath())) {
            try {
                fileInputStream = new FileInputStream(file);
            } catch (Exception e) {
                0HH.LIZ(e);
            }
            MethodCollector.o(16608);
            return fileInputStream;
        }
        MethodCollector.o(16608);
        return null;
    }

    public final long getFolderSize(File file) {
        File[] listFiles;
        long folderSize;
        long j = 0;
        if (file == null || (listFiles = file.listFiles()) == null) {
            return 0;
        }
        int length = listFiles.length;
        for (int i = 0; i < length; i++) {
            File file2 = listFiles[i];
            n.LIZ(file2, Q39.LIZJ);
            if (file2.isFile()) {
                folderSize = listFiles[i].length();
            } else {
                folderSize = getFolderSize(listFiles[i]);
            }
            j += folderSize;
        }
        return j;
    }

    public final String joinSeparator(String str) {
        M12.LIZ(str);
        String str2 = File.separator;
        n.LIZ(str2, Q39.LIZJ);
        if (y.LIZJ(str, str2, false)) {
            return str;
        }
        return str + File.separator;
    }

    public final void removeDir(File file) {
        File[] listFiles;
        if (file != null && file.exists() && file.isDirectory() && (listFiles = file.listFiles()) != null) {
            if (listFiles.length == 0) {
                INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(file);
                return;
            }
            for (File file2 : listFiles) {
                n.LIZ(file2, Q39.LIZJ);
                if (file2.isDirectory()) {
                    removeDir(file2);
                } else {
                    INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(file2);
                }
            }
            INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(file);
        }
    }

    public static boolean INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(File file) {
        8r1 r3;
        MethodCollector.i(16319);
        try {
            if (8Kc.LIZIZ()) {
                r3 = (8r1) SettingsManager.LIZ().LIZ("storage_intercepter_key", 8r1.class, 4N2.LIZ);
            } else {
                r3 = 4N2.LIZ;
            }
            if (8r0.LIZ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_delete_log", 8r0.LIZ(r3));
            }
            if (8r0.LIZJ(file.getAbsolutePath(), r3)) {
                8r0.LIZ(file, new RuntimeException(), "exception_handle", 8r0.LIZ(r3));
                MethodCollector.o(16319);
                return false;
            }
        } catch (Throwable unused) {
        }
        boolean delete = file.delete();
        MethodCollector.o(16319);
        return delete;
    }

    public final void deleteContents(File file) {
        if (file != null) {
            File[] listFiles = file.listFiles();
            if (listFiles != null) {
                int length = listFiles.length;
                int i = 0;
                while (i < length) {
                    File file2 = listFiles[i];
                    n.LIZ(file2, Q39.LIZJ);
                    if (file2.isDirectory()) {
                        deleteContents(file2);
                    }
                    if (INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(file2)) {
                        i++;
                    } else {
                        throw new IOException("failed to delete file: ".concat(String.valueOf(file2)));
                    }
                }
                return;
            }
            throw new IOException("not a readable directory: ".concat(String.valueOf(file)));
        }
    }

    public final String readFully(Reader reader) {
        MethodCollector.i(16744);
        M12.LIZ(reader);
        try {
            Reader reader2 = reader;
            StringWriter stringWriter = new StringWriter();
            char[] cArr = new char[1024];
            for (int read = reader2.read(cArr); read != -1; read = reader2.read(cArr)) {
                stringWriter.write(cArr, 0, read);
            }
            String stringWriter2 = stringWriter.toString();
            n.LIZ(stringWriter2, Q39.LIZJ);
            7y5.LIZ(reader, (Throwable) null);
            MethodCollector.o(16744);
            return stringWriter2;
        } catch (Throwable th) {
            7y5.LIZ(reader, th);
            MethodCollector.o(16744);
            throw th;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:27:0x005b A[SYNTHETIC, Splitter:B:27:0x005b] */
    /* JADX WARNING: Removed duplicated region for block: B:32:0x0065 A[SYNTHETIC, Splitter:B:32:0x0065] */
    /* JADX WARNING: Removed duplicated region for block: B:42:0x0077 A[SYNTHETIC, Splitter:B:42:0x0077] */
    /* JADX WARNING: Removed duplicated region for block: B:47:0x0081 A[SYNTHETIC, Splitter:B:47:0x0081] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final java.lang.String getFileContent(java.lang.String r8) {
        /*
            r7 = this;
            r6 = 16606(0x40de, float:2.327E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r6)
            X.M12.LIZ(r8)
            java.io.File r1 = new java.io.File
            r1.<init>(r8)
            java.lang.String r0 = r1.getPath()
            boolean r0 = r7.checkFileExists(r0)
            java.lang.String r5 = ""
            if (r0 == 0) goto L_0x008d
            r4 = 0
            java.io.FileReader r3 = new java.io.FileReader     // Catch:{ Exception -> 0x0053, all -> 0x004f }
            r3.<init>(r1)     // Catch:{ Exception -> 0x0053, all -> 0x004f }
            java.io.BufferedReader r2 = new java.io.BufferedReader     // Catch:{ Exception -> 0x004c, all -> 0x0073 }
            r2.<init>(r3)     // Catch:{ Exception -> 0x004c, all -> 0x0073 }
            java.lang.String r1 = r2.readLine()     // Catch:{ Exception -> 0x004a }
        L_0x0028:
            if (r1 == 0) goto L_0x003e
            java.lang.StringBuilder r0 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x004a }
            r0.<init>()     // Catch:{ Exception -> 0x004a }
            r0.append(r5)     // Catch:{ Exception -> 0x004a }
            r0.append(r1)     // Catch:{ Exception -> 0x004a }
            java.lang.String r5 = r0.toString()     // Catch:{ Exception -> 0x004a }
            java.lang.String r1 = r2.readLine()     // Catch:{ Exception -> 0x004a }
            goto L_0x0028
        L_0x003e:
            r3.close()     // Catch:{ IOException -> 0x0042 }
            goto L_0x0046
        L_0x0042:
            r0 = move-exception
            X.0HH.LIZ(r0)
        L_0x0046:
            r2.close()     // Catch:{ IOException -> 0x0069 }
            goto L_0x006d
        L_0x004a:
            r0 = move-exception
            goto L_0x0056
        L_0x004c:
            r0 = move-exception
            r2 = r4
            goto L_0x0056
        L_0x004f:
            r1 = move-exception
            r3 = r4
            r2 = r3
            goto L_0x0075
        L_0x0053:
            r0 = move-exception
            r3 = r4
            r2 = r3
        L_0x0056:
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x0071 }
            if (r3 == 0) goto L_0x0063
            r3.close()     // Catch:{ IOException -> 0x005f }
            goto L_0x0063
        L_0x005f:
            r0 = move-exception
            X.0HH.LIZ(r0)
        L_0x0063:
            if (r2 == 0) goto L_0x006d
            r2.close()     // Catch:{ IOException -> 0x0069 }
            goto L_0x006d
        L_0x0069:
            r0 = move-exception
            X.0HH.LIZ(r0)
        L_0x006d:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r6)
            return r5
        L_0x0071:
            r1 = move-exception
            goto L_0x0075
        L_0x0073:
            r1 = move-exception
            r2 = r4
        L_0x0075:
            if (r3 == 0) goto L_0x007f
            r3.close()     // Catch:{ IOException -> 0x007b }
            goto L_0x007f
        L_0x007b:
            r0 = move-exception
            X.0HH.LIZ(r0)
        L_0x007f:
            if (r2 == 0) goto L_0x0089
            r2.close()     // Catch:{ IOException -> 0x0085 }
            goto L_0x0089
        L_0x0085:
            r0 = move-exception
            X.0HH.LIZ(r0)
        L_0x0089:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r6)
            throw r1
        L_0x008d:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r6)
            return r5
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.effectmanager.common.utils.FileUtils.getFileContent(java.lang.String):java.lang.String");
    }

    public final long copyStream(InputStream inputStream, OutputStream outputStream) {
        InputStream inputStream2 = inputStream;
        OutputStream outputStream2 = outputStream;
        M12.LIZ(inputStream2, outputStream2);
        return copyStream(inputStream2, outputStream2, 0, (DownloadListener) null);
    }

    public final String addPathSuffix(String str, String str2) {
        M12.LIZ(str2);
        if (str == null) {
            return null;
        }
        String str3 = File.separator;
        n.LIZ(str3, Q39.LIZJ);
        if (y.LIZJ(str, str3, false)) {
            return GhQ.LIZIZ(str, 1) + str2 + File.separator;
        }
        return str + str2;
    }

    public final String combineFilePath(String str, String str2) {
        M12.LIZ(str, str2);
        StringBuilder sb = new StringBuilder();
        if (TextUtils.isEmpty(str)) {
            str = Q39.LIZJ;
        }
        sb.append(str);
        if (TextUtils.isEmpty(str2)) {
            str2 = Q39.LIZJ;
        }
        sb.append(str2);
        return sb.toString();
    }

    public final File createFile(String str, boolean z) {
        File file = null;
        if (str == null) {
            return null;
        }
        if (!TextUtils.isEmpty(str)) {
            file = new File(str);
            if (!file.exists()) {
                if (!z) {
                    file.mkdirs();
                } else {
                    try {
                        File parentFile = file.getParentFile();
                        if (parentFile == null) {
                            n.LIZ();
                        }
                        if (!parentFile.exists()) {
                            parentFile.mkdirs();
                        }
                        file.createNewFile();
                    } catch (IOException e) {
                        0HH.LIZ(e);
                    }
                }
            }
        }
        return file;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:35:0x0062, code lost:
        if (r3 == null) goto L_0x006c;
     */
    /* JADX WARNING: Removed duplicated region for block: B:26:0x0054 A[SYNTHETIC, Splitter:B:26:0x0054] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final long writeToExternal(java.io.InputStream r10, java.lang.String r11) {
        /*
            r9 = this;
            r8 = 15588(0x3ce4, float:2.1843E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r8)
            X.M12.LIZ(r10)
            r6 = 0
            if (r11 != 0) goto L_0x0010
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r8)
            return r6
        L_0x0010:
            java.lang.Class<com.ss.android.ugc.effectmanager.common.utils.FileUtils> r5 = com.ss.android.ugc.effectmanager.common.utils.FileUtils.class
            monitor-enter(r5)
            java.io.File r4 = new java.io.File     // Catch:{ all -> 0x0070 }
            r4.<init>(r11)     // Catch:{ all -> 0x0070 }
            boolean r0 = r4.exists()     // Catch:{ all -> 0x0070 }
            if (r0 != 0) goto L_0x0028
            com.ss.android.ugc.effectmanager.common.utils.FileUtils r2 = INSTANCE     // Catch:{ all -> 0x0070 }
            java.lang.String r1 = r4.getPath()     // Catch:{ all -> 0x0070 }
            r0 = 1
            r2.createFile(r1, r0)     // Catch:{ all -> 0x0070 }
        L_0x0028:
            r3 = 0
            java.io.FileOutputStream r2 = new java.io.FileOutputStream     // Catch:{ Exception -> 0x004e }
            r2.<init>(r4)     // Catch:{ Exception -> 0x004e }
            com.ss.android.ugc.effectmanager.common.cachemanager.TotalLengthOutputStream r1 = new com.ss.android.ugc.effectmanager.common.cachemanager.TotalLengthOutputStream     // Catch:{ Exception -> 0x004b, all -> 0x0048 }
            r1.<init>(r2)     // Catch:{ Exception -> 0x004b, all -> 0x0048 }
            com.ss.android.ugc.effectmanager.common.utils.FileUtils r0 = INSTANCE     // Catch:{ Exception -> 0x004b, all -> 0x0048 }
            r0.copyStream(r10, r1)     // Catch:{ Exception -> 0x004b, all -> 0x0048 }
            r1.flush()     // Catch:{ Exception -> 0x004b, all -> 0x0048 }
            r1.close()     // Catch:{ Exception -> 0x004b, all -> 0x0048 }
            long r6 = r1.getLength()     // Catch:{ Exception -> 0x004b, all -> 0x0048 }
            r2.close()     // Catch:{ IOException -> 0x0046 }
            goto L_0x005c
        L_0x0046:
            r0 = move-exception
            goto L_0x0059
        L_0x0048:
            r1 = move-exception
            r3 = r2
            goto L_0x0064
        L_0x004b:
            r0 = move-exception
            r3 = r2
            goto L_0x004f
        L_0x004e:
            r0 = move-exception
        L_0x004f:
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x0061 }
            if (r3 == 0) goto L_0x005c
            r3.close()     // Catch:{ IOException -> 0x0058 }
            goto L_0x005c
        L_0x0058:
            r0 = move-exception
        L_0x0059:
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x0070 }
        L_0x005c:
            monitor-exit(r5)
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r8)
            return r6
        L_0x0061:
            r1 = move-exception
            if (r3 == 0) goto L_0x006c
        L_0x0064:
            r3.close()     // Catch:{ IOException -> 0x0068 }
            goto L_0x006c
        L_0x0068:
            r0 = move-exception
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x0070 }
        L_0x006c:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r8)     // Catch:{ all -> 0x0070 }
            throw r1     // Catch:{ all -> 0x0070 }
        L_0x0070:
            r0 = move-exception
            monitor-exit(r5)
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r8)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.effectmanager.common.utils.FileUtils.writeToExternal(java.io.InputStream, java.lang.String):long");
    }

    /* JADX WARNING: Code restructure failed: missing block: B:47:0x0070, code lost:
        if (r4 == null) goto L_0x007a;
     */
    /* JADX WARNING: Removed duplicated region for block: B:37:0x0060 A[SYNTHETIC, Splitter:B:37:0x0060] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final long writeToExternal(java.lang.String r9, java.lang.String r10) {
        /*
            r8 = this;
            r7 = 15586(0x3ce2, float:2.184E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r7)
            X.M12.LIZ(r9, r10)
            java.lang.Class<com.ss.android.ugc.effectmanager.common.utils.FileUtils> r6 = com.ss.android.ugc.effectmanager.common.utils.FileUtils.class
            monitor-enter(r6)
            java.io.File r5 = new java.io.File     // Catch:{ all -> 0x007e }
            r5.<init>(r10)     // Catch:{ all -> 0x007e }
            boolean r0 = r5.exists()     // Catch:{ all -> 0x007e }
            if (r0 != 0) goto L_0x0020
            com.ss.android.ugc.effectmanager.common.utils.FileUtils r2 = INSTANCE     // Catch:{ all -> 0x007e }
            java.lang.String r1 = r5.getPath()     // Catch:{ all -> 0x007e }
            r0 = 1
            r2.createFile(r1, r0)     // Catch:{ all -> 0x007e }
        L_0x0020:
            r4 = 0
            java.io.FileOutputStream r3 = new java.io.FileOutputStream     // Catch:{ Exception -> 0x0058 }
            r3.<init>(r5)     // Catch:{ Exception -> 0x0058 }
            java.io.OutputStreamWriter r2 = new java.io.OutputStreamWriter     // Catch:{ Exception -> 0x0055, all -> 0x0052 }
            r2.<init>(r3)     // Catch:{ Exception -> 0x0055, all -> 0x0052 }
            r0 = r2
            java.io.OutputStreamWriter r0 = (java.io.OutputStreamWriter) r0     // Catch:{ all -> 0x0045 }
            r0.write(r9)     // Catch:{ all -> 0x0045 }
            r0.flush()     // Catch:{ all -> 0x0045 }
            X.7y5.LIZ(r2, r4)     // Catch:{ Exception -> 0x0055, all -> 0x0052 }
            int r0 = r9.length()     // Catch:{ Exception -> 0x0055, all -> 0x0052 }
            long r1 = (long) r0     // Catch:{ Exception -> 0x0055, all -> 0x0052 }
            r3.close()     // Catch:{ IOException -> 0x0040 }
            goto L_0x006a
        L_0x0040:
            r0 = move-exception
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x007e }
            goto L_0x006a
        L_0x0045:
            r1 = move-exception
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r7)     // Catch:{ all -> 0x004a }
            throw r1     // Catch:{ all -> 0x004a }
        L_0x004a:
            r0 = move-exception
            X.7y5.LIZ(r2, r1)     // Catch:{ Exception -> 0x0055, all -> 0x0052 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r7)     // Catch:{ Exception -> 0x0055, all -> 0x0052 }
            throw r0     // Catch:{ Exception -> 0x0055, all -> 0x0052 }
        L_0x0052:
            r1 = move-exception
            r4 = r3
            goto L_0x0072
        L_0x0055:
            r0 = move-exception
            r4 = r3
            goto L_0x0059
        L_0x0058:
            r0 = move-exception
        L_0x0059:
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x006f }
            r0 = 0
            if (r4 == 0) goto L_0x0068
            r4.close()     // Catch:{ IOException -> 0x0064 }
            goto L_0x0068
        L_0x0064:
            r0 = move-exception
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x007e }
        L_0x0068:
            r1 = 0
        L_0x006a:
            monitor-exit(r6)
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r7)
            return r1
        L_0x006f:
            r1 = move-exception
            if (r4 == 0) goto L_0x007a
        L_0x0072:
            r4.close()     // Catch:{ IOException -> 0x0076 }
            goto L_0x007a
        L_0x0076:
            r0 = move-exception
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x007e }
        L_0x007a:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r7)     // Catch:{ all -> 0x007e }
            throw r1     // Catch:{ all -> 0x007e }
        L_0x007e:
            r0 = move-exception
            monitor-exit(r6)
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r7)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.effectmanager.common.utils.FileUtils.writeToExternal(java.lang.String, java.lang.String):long");
    }

    /* JADX WARNING: Removed duplicated region for block: B:59:0x0146 A[Catch:{ all -> 0x0148 }] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final void unZip(java.lang.String r12, java.lang.String r13) {
        /*
            r11 = this;
            java.lang.String r3 = ""
            r10 = 16457(0x4049, float:2.3061E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r10)
            if (r12 == 0) goto L_0x000b
            if (r13 != 0) goto L_0x000f
        L_0x000b:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r10)
            return
        L_0x000f:
            r2 = 0
            java.io.File r5 = new java.io.File     // Catch:{ Exception -> 0x0136 }
            r5.<init>(r13)     // Catch:{ Exception -> 0x0136 }
            boolean r0 = r5.exists()     // Catch:{ Exception -> 0x0136 }
            if (r0 == 0) goto L_0x001e
            r11.removeDir((java.io.File) r5)     // Catch:{ Exception -> 0x0136 }
        L_0x001e:
            java.lang.String r6 = r5.getCanonicalPath()     // Catch:{ Exception -> 0x0136 }
            java.util.zip.ZipInputStream r4 = new java.util.zip.ZipInputStream     // Catch:{ Exception -> 0x0136 }
            java.io.BufferedInputStream r1 = new java.io.BufferedInputStream     // Catch:{ Exception -> 0x0136 }
            java.io.FileInputStream r0 = new java.io.FileInputStream     // Catch:{ Exception -> 0x0136 }
            r0.<init>(r12)     // Catch:{ Exception -> 0x0136 }
            r1.<init>(r0)     // Catch:{ Exception -> 0x0136 }
            r4.<init>(r1)     // Catch:{ Exception -> 0x0136 }
            java.util.zip.ZipEntry r9 = r4.getNextEntry()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
        L_0x0035:
            if (r9 == 0) goto L_0x0129
            java.lang.String r1 = r9.getName()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            kotlin.h.b.n.LIZ(r1, r3)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = "../"
            boolean r0 = kotlin.o.z.LJ(r1, r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r8 = "FileUtils"
            if (r0 == 0) goto L_0x0055
            java.lang.String r0 = "file name contains illegal pattern with ..!"
            com.ss.android.ugc.effectmanager.common.logger.EPLog.e(r8, r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r4.closeEntry()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.util.zip.ZipEntry r9 = r4.getNextEntry()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            goto L_0x0035
        L_0x0055:
            java.io.File r2 = new java.io.File     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r2.<init>(r5, r1)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r2.getCanonicalPath()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            kotlin.h.b.n.LIZ(r0, r3)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            kotlin.h.b.n.LIZ(r6, r3)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r7 = 0
            boolean r0 = kotlin.o.y.LIZIZ(r0, r6, r7)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            if (r0 == 0) goto L_0x010e
            boolean r0 = r9.isDirectory()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            if (r0 == 0) goto L_0x0094
            boolean r0 = r2.mkdirs()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            if (r0 != 0) goto L_0x008c
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = "mkdir failed! file name:"
            r1.<init>(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r2.getName()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r1.append(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            com.ss.android.ugc.effectmanager.common.logger.EPLog.e(r8, r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
        L_0x008c:
            r4.closeEntry()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.util.zip.ZipEntry r9 = r4.getNextEntry()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            goto L_0x0035
        L_0x0094:
            java.io.File r9 = r2.getParentFile()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            if (r9 == 0) goto L_0x00dd
            boolean r0 = r9.exists()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            if (r0 != 0) goto L_0x00dd
            boolean r0 = r9.mkdirs()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            if (r0 != 0) goto L_0x00bb
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = "parent mkdir failed! file name:"
            r1.<init>(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r9.getName()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r1.append(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            com.ss.android.ugc.effectmanager.common.logger.EPLog.e(r8, r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
        L_0x00bb:
            java.io.BufferedOutputStream r9 = new java.io.BufferedOutputStream     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.io.FileOutputStream r0 = new java.io.FileOutputStream     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r0.<init>(r2)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r9.<init>(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            X.2us r8 = new X.2us     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r8.<init>()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            int r0 = BUFFER_SIZE     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            byte[] r2 = new byte[r0]     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
        L_0x00ce:
            int r1 = r4.read(r2)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r8.element = r1     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r0 = -1
            if (r1 == r0) goto L_0x00ff
            int r0 = r8.element     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r9.write(r2, r7, r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            goto L_0x00ce
        L_0x00dd:
            boolean r0 = r2.exists()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            if (r0 == 0) goto L_0x00bb
            boolean r0 = INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_FileUtils_com_ss_android_ugc_aweme_storage_FileLancet_delete(r2)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            if (r0 != 0) goto L_0x00bb
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = "delete file failed! file name:"
            r1.<init>(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r2.getName()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r1.append(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            com.ss.android.ugc.effectmanager.common.logger.EPLog.e(r8, r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            goto L_0x00bb
        L_0x00ff:
            r9.flush()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r9.close()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r4.closeEntry()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.util.zip.ZipEntry r9 = r4.getNextEntry()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            goto L_0x0035
        L_0x010e:
            com.ss.android.ugc.effectmanager.common.exception.UnzipException r2 = new com.ss.android.ugc.effectmanager.common.exception.UnzipException     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = "Entry is outside of the target dir: "
            r1.<init>(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r9.getName()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r1.append(r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            java.lang.String r0 = r1.toString()     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            r2.<init>((java.lang.String) r0)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r10)     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
            throw r2     // Catch:{ Exception -> 0x0133, all -> 0x0130 }
        L_0x0129:
            r11.closeQuietly(r4)
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r10)
            return
        L_0x0130:
            r0 = move-exception
            r2 = r4
            goto L_0x0149
        L_0x0133:
            r0 = move-exception
            r2 = r4
            goto L_0x0137
        L_0x0136:
            r0 = move-exception
        L_0x0137:
            com.ss.android.ugc.effectmanager.common.exception.UnzipException r1 = new com.ss.android.ugc.effectmanager.common.exception.UnzipException     // Catch:{ all -> 0x0148 }
            java.lang.String r0 = r0.getMessage()     // Catch:{ all -> 0x0148 }
            if (r0 != 0) goto L_0x0146
        L_0x013f:
            r1.<init>((java.lang.String) r3)     // Catch:{ all -> 0x0148 }
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r10)     // Catch:{ all -> 0x0148 }
            throw r1     // Catch:{ all -> 0x0148 }
        L_0x0146:
            r3 = r0
            goto L_0x013f
        L_0x0148:
            r0 = move-exception
        L_0x0149:
            r11.closeQuietly(r2)
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r10)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.effectmanager.common.utils.FileUtils.unZip(java.lang.String, java.lang.String):void");
    }

    public final synchronized void rename(String str, String str2, boolean z) {
        MethodCollector.i(10938);
        if (str == null) {
            MethodCollector.o(10938);
        } else if (str2 == null) {
            MethodCollector.o(10938);
        } else {
            rename$default(this, new File(str), new File(str2), false, 4, (Object) null);
            MethodCollector.o(10938);
        }
    }

    public final synchronized void rename(File file, File file2, boolean z) {
        MethodCollector.i(11160);
        if (file == null) {
            MethodCollector.o(11160);
        } else if (file2 == null) {
            MethodCollector.o(11160);
        } else {
            if (z) {
                if (file2.exists()) {
                    8Av.LJII(file2);
                }
            }
            if (file.renameTo(file2)) {
                MethodCollector.o(11160);
                return;
            }
            EPLog.e("FileUtils", "rename " + file.getName() + " to " + file2.getName() + " failed!");
            IOException iOException = new IOException();
            MethodCollector.o(11160);
            throw iOException;
        }
    }

    public final long copyStream(InputStream inputStream, OutputStream outputStream, long j, DownloadListener downloadListener) {
        MethodCollector.i(17520);
        M12.LIZ(inputStream, outputStream);
        byte[] bArr = new byte[BUFFER_SIZE];
        int read = inputStream.read(bArr);
        long j2 = 0;
        while (read > 0) {
            if (outputStream != null) {
                outputStream.write(bArr, 0, read);
            }
            j2 += (long) read;
            if (j2 < j && j > 0 && downloadListener != null) {
                downloadListener.onProgress((int) ((((float) j2) / ((float) j)) * 100.0f), j);
            }
            read = inputStream.read(bArr);
        }
        if (downloadListener != null) {
            downloadListener.onProgress(100, j);
        }
        MethodCollector.o(17520);
        return j2;
    }

    public final synchronized void rename(File file, File file2, boolean z, boolean z2) {
        MethodCollector.i(11162);
        if (file == null) {
            MethodCollector.o(11162);
        } else if (file2 == null) {
            MethodCollector.o(11162);
        } else {
            if (z && !z2) {
                if (file2.exists()) {
                    8Av.LJII(file2);
                }
            }
            if (!file.renameTo(file2)) {
                if (!z2 || !z) {
                    EPLog.e("FileUtils", "rename " + file.getName() + " to " + file2.getName() + " failed!");
                    IOException iOException = new IOException();
                    MethodCollector.o(11162);
                    throw iOException;
                }
                8Av.LJII(file2);
                if (!file.renameTo(file2)) {
                    EPLog.e("FileUtils", "rename " + file.getName() + " to " + file2.getName() + " failed!");
                    IOException iOException2 = new IOException();
                    MethodCollector.o(11162);
                    throw iOException2;
                }
            }
            MethodCollector.o(11162);
        }
    }

    public final synchronized void rename(String str, String str2, boolean z, boolean z2) {
        MethodCollector.i(10939);
        if (str == null) {
            MethodCollector.o(10939);
        } else if (str2 == null) {
            MethodCollector.o(10939);
        } else {
            rename(new File(str), new File(str2), z, z2);
            MethodCollector.o(10939);
        }
    }

    public static /* synthetic */ void rename$default(FileUtils fileUtils, File file, File file2, boolean z, int i, Object obj) {
        if ((i & 4) != 0) {
            z = true;
        }
        fileUtils.rename(file, file2, z);
    }

    public static /* synthetic */ void rename$default(FileUtils fileUtils, String str, String str2, boolean z, int i, Object obj) {
        if ((i & 4) != 0) {
            z = true;
        }
        fileUtils.rename(str, str2, z);
    }

    public static /* synthetic */ void rename$default(FileUtils fileUtils, File file, File file2, boolean z, boolean z2, int i, Object obj) {
        if ((i & 4) != 0) {
            z = true;
        }
        if ((i & 8) != 0) {
            z2 = false;
        }
        fileUtils.rename(file, file2, z, z2);
    }

    public static /* synthetic */ void rename$default(FileUtils fileUtils, String str, String str2, boolean z, boolean z2, int i, Object obj) {
        if ((i & 4) != 0) {
            z = true;
        }
        if ((i & 8) != 0) {
            z2 = false;
        }
        fileUtils.rename(str, str2, z, z2);
    }
}
