package com.dowse.base.log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/log/FileTools.class */
public class FileTools {
    private static String TAG = "FileTools";

    public static void createDefaultDirs(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        if (file.exists() && !file.isDirectory()) {
            file.delete();
            file.mkdirs();
        }
    }

    public static int getFileLastModifiedTime(String filepath) {
        File file = new File(filepath);
        long time = file.lastModified();
        return (int) (time / 1000);
    }

    public static long getFileCreateTime(String filePath) {
        FileTime fileTime = null;
        try {
            fileTime = Files.readAttributes(Paths.get(filePath, new String[0]), BasicFileAttributes.class, new LinkOption[0]).creationTime();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long time = fileTime.toMillis();
        return time;
    }

    public static int getPartNumber(String filePath, int onePartLen) {
        File file = new File(filePath);
        if (!file.exists()) {
            DSLog.e(TAG, filePath + " not exists -2- !!!");
            return -1;
        }
        long fileLength = 0;
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            fileLength = raf.length();
        } catch (IOException e) {
            e.printStackTrace();
        }
        DSLog.d(TAG, " filePath == " + filePath + " len=" + fileLength);
        if (fileLength <= 0) {
            DSLog.e(TAG, "length is illegal!!!");
            return -1;
        }
        long totalParts = fileLength / onePartLen;
        if (fileLength % onePartLen > 0) {
            totalParts++;
        }
        DSLog.d(TAG, "文件分包- totalParts=" + totalParts + " filePath=" + filePath + "  onePartLen=" + onePartLen + "  fileLength=" + fileLength);
        return (int) totalParts;
    }

    public static byte[] getOneSampleDataFromFile(String filePath, int index, int onePacketLength) {
        File file = new File(filePath);
        if (!file.exists()) {
            DSLog.e(TAG, filePath + " not exists!!! -3-");
            return null;
        }
        int bufferSize = onePacketLength;
        RandomAccessFile raf = null;
        byte[] buffer = null;
        try {
            try {
                try {
                    raf = new RandomAccessFile(file, "r");
                    int fileLen = (int) raf.length();
                    if ((index + 1) * onePacketLength > fileLen) {
                        bufferSize = fileLen - (index * onePacketLength);
                    }
                    raf.seek(index * onePacketLength);
                    buffer = new byte[bufferSize];
                    raf.read(buffer);
                    if (raf != null) {
                        try {
                            raf.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (FileNotFoundException e2) {
                    e2.printStackTrace();
                    if (raf != null) {
                        try {
                            raf.close();
                        } catch (IOException e3) {
                            e3.printStackTrace();
                        }
                    }
                }
            } catch (IOException e4) {
                e4.printStackTrace();
                if (raf != null) {
                    try {
                        raf.close();
                    } catch (IOException e5) {
                        e5.printStackTrace();
                    }
                }
            }
            return buffer;
        } catch (Throwable th) {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e6) {
                    e6.printStackTrace();
                }
            }
            throw th;
        }
    }

    public static byte[] getByteDataFromFile(String filePath, long seekStart, int length) {
        File file = new File(filePath);
        if (!file.exists()) {
            DSLog.e(TAG, filePath + " not exists!!! -4-");
            return null;
        }
        RandomAccessFile raf = null;
        byte[] buffer = null;
        try {
            try {
                raf = new RandomAccessFile(file, "r");
                raf.seek(seekStart);
                buffer = new byte[length];
                raf.read(buffer);
                if (raf != null) {
                    try {
                        raf.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
                if (raf != null) {
                    try {
                        raf.close();
                    } catch (IOException e3) {
                        e3.printStackTrace();
                    }
                }
            } catch (IOException e4) {
                e4.printStackTrace();
                if (raf != null) {
                    try {
                        raf.close();
                    } catch (IOException e5) {
                        e5.printStackTrace();
                    }
                }
            }
            return buffer;
        } catch (Throwable th) {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e6) {
                    e6.printStackTrace();
                }
            }
            throw th;
        }
    }

    public static void deleteFileByDay(File file, int day) {
        deleteFileBySecond(file, day * 24 * 60 * 60);
    }

    public static void deleteFileBySecond(File file, int seconds) {
        if (file == null || seconds < 0) {
            return;
        }
        File[] files = file.listFiles();
        if (files != null && files.length > 0) {
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFileBySecond(f, seconds);
                } else {
                    int getModifiedTime = getFileLastModifiedTime(String.valueOf(f));
                    long sec = seconds;
                    long m = (System.currentTimeMillis() / 1000) - sec;
                    if (m > getModifiedTime) {
                        DSLog.v(TAG, "del 文件 - " + file.getAbsolutePath());
                        f.delete();
                    }
                }
            }
            return;
        }
        String path = file.getAbsolutePath() + "/";
        path.replace("//", "/");
        DSLog.v(TAG, "del 文件夹 - " + file.getAbsolutePath());
        file.delete();
    }
}
