导航菜单

页面标题

页面副标题

六分钟步行测试管理软件 v1.1.0.2112120029 - FileUtil.java 源代码

正在查看: 六分钟步行测试管理软件 v1.1.0.2112120029 应用的 FileUtil.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.sayeasytech.base.utils;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.FileUtils;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import com.sayeasytech.base.log.KLog;
import com.starcaretech.stardata.utils.StarFileUtil;
import io.netty.handler.codec.http.multipart.HttpPostBodyUtil;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtil {
    public static final int FAILED_FILE_EXIST = -2;
    public static final int FAILED_UNKNOW = -1;
    public static final int SUCCESS_CREATE_FILE = 0;
    private static Context applicationContext;

    public static void init(Context context) {
        applicationContext = context;
    }

    public static boolean createFolderIfNotExist(String str) {
        File file = new File(str);
        if (file.exists()) {
            return true;
        }
        KLog.i("Folder : " + str + " not exist, create it now");
        if (!file.mkdir()) {
            return false;
        }
        KLog.i("Folder : " + str + " create success");
        return true;
    }

    public static int createFileIfNotExist(String str) {
        File file = new File(str);
        try {
            if (!file.exists()) {
                return file.createNewFile() ? 0 : -1;
            }
            KLog.i("File exist");
            return -2;
        } catch (IOException e) {
            KLog.e(e);
            return -1;
        }
    }

    public static int createNewFile(String str) {
        File file = new File(str);
        try {
            if (file.exists()) {
                KLog.i("file exist, delete it now");
                if (!file.delete()) {
                    KLog.i("delete file error");
                    return -1;
                }
            }
            return file.createNewFile() ? 0 : -1;
        } catch (IOException e) {
            KLog.e("createNewFile failed " + str, e);
            return -1;
        }
    }

    public static boolean copyFile(java.io.File r11, java.lang.String r12) {
        throw new UnsupportedOperationException("Method not decompiled: com.sayeasytech.base.utils.FileUtil.copyFile(java.io.File, java.lang.String):boolean");
    }

    public static boolean copyFile(InputStream inputStream, OutputStream outputStream) {
        byte[] bArr = new byte[8192];
        while (true) {
            try {
                int read = inputStream.read(bArr);
                if (read != -1) {
                    outputStream.write(bArr, 0, read);
                    outputStream.flush();
                } else {
                    outputStream.close();
                    inputStream.close();
                    return true;
                }
            } catch (IOException e) {
                KLog.e("copyFile error.", e);
                return false;
            }
        }
    }

    public static void writeStringToFile(String str, String str2) {
        try {
            createNewFile(str2);
            File file = new File(str2);
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            randomAccessFile.seek(file.length());
            randomAccessFile.write(str.getBytes());
            randomAccessFile.close();
        } catch (Exception e) {
            KLog.e("Error on write file.", e);
        }
    }

    public static byte[] readBytesFromFile(File file) {
        byte[] bArr;
        RandomAccessFile randomAccessFile;
        RandomAccessFile randomAccessFile2 = 0;
        byte[] bArr2 = null;
        RandomAccessFile randomAccessFile3 = null;
        try {
            try {
                randomAccessFile = new RandomAccessFile(file, "r");
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e) {
            e = e;
            bArr = null;
        }
        try {
            bArr2 = new byte[(int) randomAccessFile.length()];
            randomAccessFile.readFully(bArr2);
            try {
                randomAccessFile.close();
                randomAccessFile2 = bArr2;
            } catch (Exception e2) {
                KLog.e("Error on read close file.", e2);
                randomAccessFile2 = bArr2;
            }
        } catch (Exception e3) {
            e = e3;
            bArr = bArr2;
            randomAccessFile3 = randomAccessFile;
            KLog.e("Error on read file.", e);
            if (randomAccessFile3 != null) {
                try {
                    randomAccessFile3.close();
                } catch (Exception e4) {
                    KLog.e("Error on read close file.", e4);
                }
            }
            randomAccessFile2 = bArr;
            return randomAccessFile2;
        } catch (Throwable th2) {
            th = th2;
            randomAccessFile2 = randomAccessFile;
            if (randomAccessFile2 != 0) {
                try {
                    randomAccessFile2.close();
                } catch (Exception e5) {
                    KLog.e("Error on read close file.", e5);
                }
            }
            throw th;
        }
        return randomAccessFile2;
    }

    public static byte[] readBytesFromFile(String str, int i) {
        byte[] bArr = new byte[i];
        try {
            FileChannel channel = new RandomAccessFile(str, "rw").getChannel();
            channel.map(FileChannel.MapMode.READ_WRITE, 0L, i).get(bArr, 0, i);
            channel.close();
        } catch (IOException e) {
            KLog.e("Error on read file.", e);
        }
        return bArr;
    }

    public static byte[] readBytesFromStreamFile(String str, int i) {
        byte[] bArr = new byte[i];
        try {
            FileInputStream fileInputStream = new FileInputStream(str);
            FileChannel channel = fileInputStream.getChannel();
            ByteBuffer allocate = ByteBuffer.allocate(i);
            if (channel.read(allocate) != -1) {
                allocate.flip();
                allocate.get(bArr);
                allocate.clear();
            }
            fileInputStream.close();
        } catch (IOException e) {
            KLog.e("Error on read stream file.", e);
        }
        return bArr;
    }

    public static void appendFileHeader(byte[] bArr, String str) {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(str, "rw");
            int length = (int) randomAccessFile.length();
            byte[] bArr2 = new byte[length];
            randomAccessFile.read(bArr2, 0, length);
            randomAccessFile.seek(0L);
            randomAccessFile.write(bArr);
            randomAccessFile.seek(bArr.length);
            randomAccessFile.write(bArr2);
            randomAccessFile.close();
        } catch (Exception e) {
            KLog.e("Error on append file header.", e);
        }
    }

    public static String getFileAbsolutePath(Context context, Uri uri) {
        Uri uri2 = null;
        if (context != null && uri != null) {
            if (Build.VERSION.SDK_INT < 19) {
                return getRealFilePath(context, uri);
            }
            if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 29 && DocumentsContract.isDocumentUri(context, uri)) {
                if (isExternalStorageDocument(uri)) {
                    String[] split = DocumentsContract.getDocumentId(uri).split(":");
                    if ("primary".equalsIgnoreCase(split[0])) {
                        return Environment.getExternalStorageDirectory() + "/" + split[1];
                    }
                } else {
                    if (isDownloadsDocument(uri)) {
                        return getDataColumn(context, ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(DocumentsContract.getDocumentId(uri)).longValue()), null, null);
                    }
                    if (isMediaDocument(uri)) {
                        String[] split2 = DocumentsContract.getDocumentId(uri).split(":");
                        String str = split2[0];
                        if ("image".equals(str)) {
                            uri2 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                        } else if ("video".equals(str)) {
                            uri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                        } else if (StarFileUtil.AUDIO_PARENT_PATH.equals(str)) {
                            uri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                        }
                        return getDataColumn(context, uri2, "_id=?", new String[]{split2[1]});
                    }
                }
            }
            if (Build.VERSION.SDK_INT >= 29) {
                return uriToFileApiQ(context, uri);
            }
            if ("content".equalsIgnoreCase(uri.getScheme())) {
                if (isGooglePhotosUri(uri)) {
                    return uri.getLastPathSegment();
                }
                return getDataColumn(context, uri, null, null);
            }
            if (HttpPostBodyUtil.FILE.equalsIgnoreCase(uri.getScheme())) {
                return uri.getPath();
            }
        }
        return null;
    }

    private static String getRealFilePath(Context context, Uri uri) {
        int columnIndex;
        String str = null;
        if (uri == null) {
            return null;
        }
        String scheme = uri.getScheme();
        if (scheme == null) {
            return uri.getPath();
        }
        if (HttpPostBodyUtil.FILE.equals(scheme)) {
            return uri.getPath();
        }
        if (!"content".equals(scheme)) {
            return null;
        }
        Cursor query = context.getContentResolver().query(uri, new String[]{"_data"}, null, null, null);
        if (query == null) {
            return null;
        }
        if (query.moveToFirst() && (columnIndex = query.getColumnIndex("_data")) > -1) {
            str = query.getString(columnIndex);
        }
        query.close();
        return str;
    }

    private static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    private static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    private static String getDataColumn(Context context, Uri uri, String str, String[] strArr) {
        Cursor cursor = null;
        try {
            Cursor query = context.getContentResolver().query(uri, new String[]{"_data"}, str, strArr, null);
            if (query != null) {
                try {
                    if (query.moveToFirst()) {
                        String string = query.getString(query.getColumnIndexOrThrow("_data"));
                        if (query != null) {
                            query.close();
                        }
                        return string;
                    }
                } catch (Throwable th) {
                    th = th;
                    cursor = query;
                    if (cursor != null) {
                        cursor.close();
                    }
                    throw th;
                }
            }
            if (query != null) {
                query.close();
            }
            return null;
        } catch (Throwable th2) {
            th = th2;
        }
    }

    private static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    private static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

    private static String getFileFromContentUri(Context context, Uri uri) {
        if (uri == null) {
            return null;
        }
        String[] strArr = {"_data", "_display_name"};
        Cursor query = context.getContentResolver().query(uri, strArr, null, null, null);
        if (query == null) {
            return "";
        }
        query.moveToFirst();
        try {
            return query.getString(query.getColumnIndex(strArr[0]));
        } catch (Exception unused) {
            return "";
        } finally {
            query.close();
        }
    }

    private static String uriToFileApiQ(Context context, Uri uri) {
        InputStream openInputStream;
        File file;
        FileOutputStream fileOutputStream;
        File file2 = null;
        if (uri.getScheme().equals(HttpPostBodyUtil.FILE)) {
            file2 = new File(uri.getPath());
        } else if (uri.getScheme().equals("content")) {
            ContentResolver contentResolver = context.getContentResolver();
            Cursor query = contentResolver.query(uri, null, null, null, null);
            if (query.moveToFirst()) {
                String string = query.getString(query.getColumnIndex("_display_name"));
                try {
                    openInputStream = contentResolver.openInputStream(uri);
                    file = new File(context.getExternalCacheDir().getAbsolutePath(), Math.round((Math.random() + 1.0d) * 1000.0d) + string);
                    fileOutputStream = new FileOutputStream(file);
                    FileUtils.copy(openInputStream, fileOutputStream);
                } catch (IOException e) {
                    e = e;
                }
                try {
                    fileOutputStream.close();
                    openInputStream.close();
                    file2 = file;
                } catch (IOException e2) {
                    e = e2;
                    file2 = file;
                    e.printStackTrace();
                    return file2.getAbsolutePath();
                }
            }
        }
        return file2.getAbsolutePath();
    }

    public static void scanFileAsync(String str) {
        if (applicationContext == null) {
            return;
        }
        Intent intent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        intent.setData(Uri.fromFile(new File(str)));
        applicationContext.sendBroadcast(intent);
    }

    public static boolean deleteDir(File file) {
        if (file != null && file.isDirectory()) {
            for (String str : file.list()) {
                if (!deleteDir(new File(file, str))) {
                    return false;
                }
            }
        }
        return file.delete();
    }

    public static void clearApplicationData(Context context) {
        File file = new File(context.getCacheDir().getParent());
        if (file.exists()) {
            for (String str : file.list()) {
                if (!str.equals("lib")) {
                    File file2 = new File(file, str);
                    KLog.i("delete dir " + file2.getAbsolutePath());
                    deleteDir(file2);
                }
            }
        }
    }

    public static <T> List<T> deepCopy(List<T> list) {
        ArrayList arrayList = new ArrayList();
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            new ObjectOutputStream(byteArrayOutputStream).writeObject(list);
            return (List) new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())).readObject();
        } catch (Exception e) {
            KLog.e("list deep copy error.", e);
            return arrayList;
        }
    }

    public static void zip(String str, String str2) {
        File file;
        ZipOutputStream zipOutputStream;
        ZipOutputStream zipOutputStream2 = null;
        zipOutputStream2 = null;
        zipOutputStream2 = null;
        try {
            try {
                try {
                    File file2 = new File(str2);
                    file = new File(str);
                    zipOutputStream = new ZipOutputStream(new FileOutputStream(file2));
                } catch (IOException e) {
                    e.printStackTrace();
                    zipOutputStream2 = zipOutputStream2;
                }
            } catch (IOException e2) {
                e = e2;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            ?? isFile = file.isFile();
            if (isFile != 0) {
                zipFileOrDirectory(zipOutputStream, file, "");
            } else {
                File[] listFiles = file.listFiles();
                isFile = 0;
                while (isFile < listFiles.length) {
                    zipFileOrDirectory(zipOutputStream, listFiles[isFile == true ? 1 : 0], "");
                    isFile = (isFile == true ? 1 : 0) + 1;
                }
            }
            zipOutputStream.close();
            zipOutputStream2 = isFile;
        } catch (IOException e3) {
            e = e3;
            zipOutputStream2 = zipOutputStream;
            KLog.e("zip failed.", e);
            if (zipOutputStream2 != null) {
                zipOutputStream2.close();
                zipOutputStream2 = zipOutputStream2;
            }
        } catch (Throwable th2) {
            th = th2;
            zipOutputStream2 = zipOutputStream;
            if (zipOutputStream2 != null) {
                try {
                    zipOutputStream2.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            throw th;
        }
    }

    private static void zipFileOrDirectory(ZipOutputStream zipOutputStream, File file, String str) {
        FileInputStream fileInputStream = null;
        try {
            try {
                try {
                    if (!file.isDirectory()) {
                        byte[] bArr = new byte[4096];
                        FileInputStream fileInputStream2 = new FileInputStream(file);
                        try {
                            zipOutputStream.putNextEntry(new ZipEntry(str + file.getName()));
                            while (true) {
                                int read = fileInputStream2.read(bArr);
                                if (read == -1) {
                                    break;
                                } else {
                                    zipOutputStream.write(bArr, 0, read);
                                }
                            }
                            zipOutputStream.closeEntry();
                            fileInputStream = fileInputStream2;
                        } catch (IOException e) {
                            e = e;
                            fileInputStream = fileInputStream2;
                            KLog.e("zip failed.", e);
                            if (fileInputStream != null) {
                                fileInputStream.close();
                            }
                            return;
                        } catch (Throwable th) {
                            th = th;
                            fileInputStream = fileInputStream2;
                            if (fileInputStream != null) {
                                try {
                                    fileInputStream.close();
                                } catch (IOException e2) {
                                    e2.printStackTrace();
                                }
                            }
                            throw th;
                        }
                    } else {
                        for (File file2 : file.listFiles()) {
                            zipFileOrDirectory(zipOutputStream, file2, str + file.getName() + "/");
                        }
                    }
                } catch (Throwable th2) {
                    th = th2;
                }
            } catch (IOException e3) {
                e = e3;
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        } catch (IOException e4) {
            e4.printStackTrace();
        }
    }

    public static void unzip(java.lang.String r12, java.lang.String r13) {
        throw new UnsupportedOperationException("Method not decompiled: com.sayeasytech.base.utils.FileUtil.unzip(java.lang.String, java.lang.String):void");
    }
}