IOUtil.java 源代码


package com.x8zs.plugin.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class IOUtil {
    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Throwable unused) {
            }
        }
    }

    public static boolean copyAsset(Context context, String str, File file) {
        return copyAsset(context.getAssets(), str, file);
    }

    public static boolean deleteFile(File file) {
        if (!file.exists()) {
            return false;
        }
        if (file.isFile()) {
            return file.delete();
        }
        for (File file2 : file.listFiles()) {
            deleteFile(file2);
        }
        return file.delete();
    }

    public static Serializable fromBytes(byte[] bArr) {
        ByteArrayInputStream byteArrayInputStream;
        ByteArrayInputStream byteArrayInputStream2 = null;
        try {
            byteArrayInputStream = new ByteArrayInputStream(bArr);
            try {
                Serializable serializable = (Serializable) new ObjectInputStream(byteArrayInputStream).readObject();
                close(byteArrayInputStream);
                return serializable;
            } catch (Exception unused) {
                close(byteArrayInputStream);
                return null;
            } catch (Throwable th) {
                th = th;
                byteArrayInputStream2 = byteArrayInputStream;
                close(byteArrayInputStream2);
                throw th;
            }
        } catch (Exception unused2) {
            byteArrayInputStream = null;
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public static byte[] read(InputStream inputStream, boolean z6) {
        try {
            byte[] bArr = new byte[8192];
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int read = inputStream.read(bArr);
                if (read == -1) {
                    break;
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
            return byteArrayOutputStream.toByteArray();
        } finally {
            if (z6) {
                close(inputStream);
            }
        }
    }

    public static byte[] readFile(String str) {
        return read(new FileInputStream(str), true);
    }

    public static boolean saveBitmap(Bitmap bitmap, File file) {
        FileOutputStream fileOutputStream;
        FileOutputStream fileOutputStream2 = null;
        try {
            try {
                fileOutputStream = new FileOutputStream(file);
            } catch (Exception e6) {
                e = e6;
            }
        } catch (Throwable th) {
            th = th;
        }
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            close(fileOutputStream);
            return true;
        } catch (Exception e7) {
            e = e7;
            fileOutputStream2 = fileOutputStream;
            e.printStackTrace();
            close(fileOutputStream2);
            return false;
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream2 = fileOutputStream;
            close(fileOutputStream2);
            throw th;
        }
    }

    public static byte[] toBytes(Serializable serializable) {
        if (serializable == null) {
            return null;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(serializable);
            objectOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e6) {
            e6.printStackTrace();
            return null;
        } finally {
            close(byteArrayOutputStream);
        }
    }

    public static void writeFile(String str, byte[] bArr) {
        FileOutputStream fileOutputStream;
        FileOutputStream fileOutputStream2 = null;
        try {
            fileOutputStream = new FileOutputStream(str);
        } catch (Throwable th) {
            th = th;
        }
        try {
            fileOutputStream.write(bArr);
            close(fileOutputStream);
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream2 = fileOutputStream;
            close(fileOutputStream2);
            throw th;
        }
    }

    public static boolean copyAsset(AssetManager assetManager, String str, File file) {
        FileOutputStream fileOutputStream;
        InputStream open;
        byte[] bArr;
        InputStream inputStream = null;
        try {
            open = assetManager.open(str);
            try {
                bArr = new byte[open.available()];
                open.read(bArr);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                fileOutputStream = new FileOutputStream(file);
            } catch (Throwable th) {
                th = th;
                fileOutputStream = null;
            }
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream = null;
        }
        try {
            fileOutputStream.write(bArr);
            close(open);
            close(fileOutputStream);
            return true;
        } catch (Throwable th3) {
            th = th3;
            inputStream = open;
            close(inputStream);
            close(fileOutputStream);
            throw th;
        }
    }
}