package com.bytedance.sdk.component.utils;

import android.content.Context;
import com.google.android.gms.measurement.internal.u1;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import u.a;

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

    /* JADX INFO: renamed from: com.bytedance.sdk.component.utils.wsy$1, reason: invalid class name */
    public static class AnonymousClass1 implements Comparator<File> {
        @Override // java.util.Comparator
        public /* bridge */ /* synthetic */ int compare(File file, File file2) {
            throw null;
        }
    }

    public static final class fm implements Comparator<File> {
        private fm() {
        }

        public /* synthetic */ fm(AnonymousClass1 anonymousClass1) {
            this();
        }

        private int fm(long j10, long j11) {
            if (j10 < j11) {
                return -1;
            }
            return j10 == j11 ? 0 : 1;
        }

        @Override // java.util.Comparator
        /* JADX INFO: renamed from: fm, reason: merged with bridge method [inline-methods] */
        public int compare(File file, File file2) {
            return fm(file.lastModified(), file2.lastModified());
        }
    }

    private static void ajl(File file) throws IOException {
        if (!file.delete() || !file.createNewFile()) {
            throw new IOException("Error recreate zero-size file ".concat(String.valueOf(file)));
        }
    }

    public static File fm(Context context, File file, String str) {
        if (file != null) {
            try {
                if (!file.exists()) {
                    file.mkdirs();
                }
            } catch (Throwable unused) {
                return new File(file, str);
            }
        }
        if (sds.fm(context)) {
            return new File(file, str);
        }
        return new File(file, str + "_" + sds.lb(context));
    }

    private static void jnr(File file) throws IOException {
        RandomAccessFile randomAccessFile;
        long j10;
        long length = file.length();
        if (length == 0) {
            ajl(file);
            return;
        }
        try {
            randomAccessFile = new RandomAccessFile(file, "rwd");
            j10 = length - 1;
        } catch (Throwable unused) {
            randomAccessFile = null;
        }
        try {
            randomAccessFile.seek(j10);
            byte b10 = randomAccessFile.readByte();
            randomAccessFile.seek(j10);
            randomAccessFile.write(b10);
            randomAccessFile.close();
        } catch (Throwable unused2) {
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
        }
    }

    public static void lb(File file) {
        if (file == null || !file.exists()) {
            return;
        }
        if (file.isFile()) {
            try {
                file.delete();
                return;
            } catch (Throwable unused) {
                return;
            }
        }
        File[] fileArrListFiles = file.listFiles();
        if (fileArrListFiles != null && fileArrListFiles.length > 0) {
            for (File file2 : fileArrListFiles) {
                if (file2.isDirectory()) {
                    lb(file2);
                } else {
                    try {
                        file2.delete();
                    } catch (Throwable unused2) {
                    }
                }
            }
        }
        try {
            file.delete();
        } catch (Throwable unused3) {
        }
    }

    public static File ro(Context context, String str) {
        String absolutePath = context.getCacheDir().getAbsolutePath();
        if (absolutePath != null) {
            String str2 = File.separator;
            if (!absolutePath.endsWith(str2)) {
                absolutePath = u1.j(absolutePath, str2);
            }
        }
        File file = new File(u1.j(absolutePath, str));
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    public static byte[] yz(File file) {
        FileInputStream fileInputStream;
        Long lValueOf;
        if (file != null && file.isFile() && file.exists() && file.canRead() && file.length() > 0) {
            try {
                lValueOf = Long.valueOf(file.length());
                fileInputStream = new FileInputStream(file);
            } catch (Throwable unused) {
                fileInputStream = null;
            }
            try {
                byte[] bArr = new byte[lValueOf.intValue()];
                if (fileInputStream.read(bArr) == lValueOf.longValue()) {
                    try {
                        fileInputStream.close();
                    } catch (Throwable unused2) {
                    }
                    return bArr;
                }
            } catch (Throwable unused3) {
                if (fileInputStream != null) {
                }
                return null;
            }
            try {
                fileInputStream.close();
            } catch (Throwable unused4) {
            }
        }
        return null;
    }

    public static File fm(Context context, String str, String str2) {
        String strFm = fm(context);
        if (!sds.fm(context)) {
            StringBuilder sbD = a.d(str, "_");
            sbD.append(sds.lb(context));
            str = sbD.toString();
        }
        if (strFm != null) {
            String str3 = File.separator;
            if (!strFm.endsWith(str3)) {
                strFm = u1.j(strFm, str3);
            }
        }
        String strJ = u1.j(strFm, str);
        File file = new File(strJ);
        if (!file.exists()) {
            file.mkdirs();
        }
        return new File(strJ, str2);
    }

    public static void ro(File file) throws IOException {
        if (file.exists()) {
            long jCurrentTimeMillis = System.currentTimeMillis();
            if (file.setLastModified(jCurrentTimeMillis)) {
                return;
            }
            jnr(file);
            if (file.lastModified() < jCurrentTimeMillis) {
                new Date(file.lastModified()).toString();
                file.getAbsolutePath();
            }
        }
    }

    public static File fm(Context context, String str) {
        String absolutePath = context.getCacheDir().getAbsolutePath();
        if (!sds.fm(context)) {
            StringBuilder sbD = a.d(str, "_");
            sbD.append(sds.lb(context));
            str = sbD.toString();
        }
        if (absolutePath != null) {
            String str2 = File.separator;
            if (!absolutePath.endsWith(str2)) {
                absolutePath = u1.j(absolutePath, str2);
            }
        }
        File file = new File(u1.j(absolutePath, str));
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    private static String fm(Context context) {
        File cacheDir;
        if (context == null || (cacheDir = context.getCacheDir()) == null) {
            return null;
        }
        return cacheDir.getPath();
    }

    public static List<File> fm(File file) {
        LinkedList linkedList = new LinkedList();
        File[] fileArrListFiles = file.listFiles();
        if (fileArrListFiles == null) {
            return linkedList;
        }
        List<File> listAsList = Arrays.asList(fileArrListFiles);
        Collections.sort(listAsList, new fm(null));
        return listAsList;
    }
}
