FileUtils.java 源代码


package com.x8zs.plugin.utils;

import android.os.Parcel;
import android.system.Os;
import android.text.TextUtils;
import com.inmobi.media.fd;
import java.io.BufferedOutputStream;
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.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FileUtils {

    public static class FileLock {
        private static FileLock singleton;
        private Map<String, FileLockCount> mRefCountMap = new ConcurrentHashMap();

        public class FileLockCount {
            FileChannel fChannel;
            RandomAccessFile fOs;
            java.nio.channels.FileLock mFileLock;
            int mRefCount;

            FileLockCount(java.nio.channels.FileLock fileLock, int i6, RandomAccessFile randomAccessFile, FileChannel fileChannel) {
                this.mFileLock = fileLock;
                this.mRefCount = i6;
                this.fOs = randomAccessFile;
                this.fChannel = fileChannel;
            }
        }

        private int RefCntDec(String str) {
            if (this.mRefCountMap.containsKey(str)) {
                FileLockCount fileLockCount = this.mRefCountMap.get(str);
                int i6 = fileLockCount.mRefCount - 1;
                fileLockCount.mRefCount = i6;
                if (i6 <= 0) {
                    this.mRefCountMap.remove(str);
                    return i6;
                }
                return i6;
            }
            return 0;
        }

        private int RefCntInc(String str, java.nio.channels.FileLock fileLock, RandomAccessFile randomAccessFile, FileChannel fileChannel) {
            if (this.mRefCountMap.containsKey(str)) {
                FileLockCount fileLockCount = this.mRefCountMap.get(str);
                int i6 = fileLockCount.mRefCount;
                fileLockCount.mRefCount = i6 + 1;
                return i6;
            }
            this.mRefCountMap.put(str, new FileLockCount(fileLock, 1, randomAccessFile, fileChannel));
            return 1;
        }

        public static FileLock getInstance() {
            if (singleton == null) {
                singleton = new FileLock();
            }
            return singleton;
        }

        public boolean LockExclusive(File file) {
            if (file == null) {
                return false;
            }
            try {
                File file2 = new File(file.getParentFile().getAbsolutePath().concat("/lock"));
                if (!file2.exists()) {
                    file2.createNewFile();
                }
                RandomAccessFile randomAccessFile = new RandomAccessFile(file2.getAbsolutePath(), "rw");
                FileChannel channel = randomAccessFile.getChannel();
                java.nio.channels.FileLock lock = channel.lock();
                if (!lock.isValid()) {
                    return false;
                }
                RefCntInc(file2.getAbsolutePath(), lock, randomAccessFile, channel);
                return true;
            } catch (Exception unused) {
                return false;
            }
        }

        public void unLock(File file) {
            FileLockCount fileLockCount;
            File file2 = new File(file.getParentFile().getAbsolutePath().concat("/lock"));
            if (file2.exists() && this.mRefCountMap.containsKey(file2.getAbsolutePath()) && (fileLockCount = this.mRefCountMap.get(file2.getAbsolutePath())) != null) {
                java.nio.channels.FileLock fileLock = fileLockCount.mFileLock;
                RandomAccessFile randomAccessFile = fileLockCount.fOs;
                FileChannel fileChannel = fileLockCount.fChannel;
                try {
                    if (RefCntDec(file2.getAbsolutePath()) <= 0) {
                        if (fileLock != null && fileLock.isValid()) {
                            fileLock.release();
                        }
                        if (randomAccessFile != null) {
                            randomAccessFile.close();
                        }
                        if (fileChannel != null) {
                            fileChannel.close();
                        }
                    }
                } catch (IOException e6) {
                    e6.printStackTrace();
                }
            }
        }
    }

    public interface FileMode {
        public static final int MODE_755 = 493;
        public static final int MODE_IRGRP = 32;
        public static final int MODE_IROTH = 4;
        public static final int MODE_IRUSR = 256;
        public static final int MODE_ISGID = 1024;
        public static final int MODE_ISUID = 2048;
        public static final int MODE_ISVTX = 512;
        public static final int MODE_IWGRP = 16;
        public static final int MODE_IWOTH = 2;
        public static final int MODE_IWUSR = 128;
        public static final int MODE_IXGRP = 8;
        public static final int MODE_IXOTH = 1;
        public static final int MODE_IXUSR = 64;
    }

    public static String buildValidExtFilename(String str) {
        if (!TextUtils.isEmpty(str) && !".".equals(str) && !"..".equals(str)) {
            StringBuilder sb = new StringBuilder(str.length());
            for (int i6 = 0; i6 < str.length(); i6++) {
                char charAt = str.charAt(i6);
                if (isValidExtFilenameChar(charAt)) {
                    sb.append(charAt);
                } else {
                    sb.append('_');
                }
            }
            return sb.toString();
        }
        return "(invalid)";
    }

    public static void chmod(String str, int i6) {
        try {
            Os.chmod(str, i6);
        } catch (Exception unused) {
            String str2 = "chmod ";
            if (new File(str).isDirectory()) {
                str2 = "chmod  -R ";
            }
            String format = String.format("%o", Integer.valueOf(i6));
            Runtime.getRuntime().exec(str2 + format + " " + str).waitFor();
        }
    }

    public static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception unused) {
            }
        }
    }

    public static void copyFile(File file, File file2) {
        FileOutputStream fileOutputStream;
        FileInputStream fileInputStream = null;
        try {
            FileInputStream fileInputStream2 = new FileInputStream(file);
            try {
                fileOutputStream = new FileOutputStream(file2);
                try {
                    FileChannel channel = fileInputStream2.getChannel();
                    FileChannel channel2 = fileOutputStream.getChannel();
                    ByteBuffer allocate = ByteBuffer.allocate(1024);
                    while (true) {
                        allocate.clear();
                        if (channel.read(allocate) == -1) {
                            closeQuietly(fileInputStream2);
                            closeQuietly(fileOutputStream);
                            return;
                        } else {
                            allocate.limit(allocate.position());
                            allocate.position(0);
                            channel2.write(allocate);
                        }
                    }
                } catch (Throwable th) {
                    th = th;
                    fileInputStream = fileInputStream2;
                    closeQuietly(fileInputStream);
                    closeQuietly(fileOutputStream);
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
                fileOutputStream = null;
            }
        } catch (Throwable th3) {
            th = th3;
            fileOutputStream = null;
        }
    }

    public static void createSymlink(String str, String str2) {
        Os.symlink(str, str2);
    }

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

    public static boolean isSymlink(File file) {
        if (file != null) {
            if (file.getParent() != null) {
                file = new File(file.getParentFile().getCanonicalFile(), file.getName());
            }
            return !file.getCanonicalFile().equals(file.getAbsoluteFile());
        }
        throw new NullPointerException("File must not be null");
    }

    public static boolean isValidExtFilename(String str) {
        return str != null && str.equals(buildValidExtFilename(str));
    }

    private static boolean isValidExtFilenameChar(char c6) {
        return (c6 == 0 || c6 == '/') ? false : true;
    }

    public static int peekInt(byte[] bArr, int i6, ByteOrder byteOrder) {
        int i7;
        int i8;
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
            int i9 = i6 + 1;
            int i10 = i9 + 1;
            i7 = ((bArr[i6] & fd.i.NETWORK_LOAD_LIMIT_DISABLED) << 24) | ((bArr[i9] & fd.i.NETWORK_LOAD_LIMIT_DISABLED) << 16) | ((bArr[i10] & fd.i.NETWORK_LOAD_LIMIT_DISABLED) << 8);
            i8 = bArr[i10 + 1] & fd.i.NETWORK_LOAD_LIMIT_DISABLED;
        } else {
            int i11 = i6 + 1;
            int i12 = i11 + 1;
            i7 = (bArr[i6] & fd.i.NETWORK_LOAD_LIMIT_DISABLED) | ((bArr[i11] & fd.i.NETWORK_LOAD_LIMIT_DISABLED) << 8) | ((bArr[i12] & fd.i.NETWORK_LOAD_LIMIT_DISABLED) << 16);
            i8 = (bArr[i12 + 1] & fd.i.NETWORK_LOAD_LIMIT_DISABLED) << 24;
        }
        return i8 | i7;
    }

    public static byte[] toByteArray(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[100];
        while (true) {
            int read = inputStream.read(bArr, 0, 100);
            if (read > 0) {
                byteArrayOutputStream.write(bArr, 0, read);
            } else {
                return byteArrayOutputStream.toByteArray();
            }
        }
    }

    public static void writeParcelToFile(Parcel parcel, File file) {
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(parcel.marshall());
        fileOutputStream.close();
    }

    public static void writeToFile(InputStream inputStream, File file) {
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
        byte[] bArr = new byte[1024];
        while (true) {
            int read = inputStream.read(bArr, 0, 1024);
            if (read != -1) {
                bufferedOutputStream.write(bArr, 0, read);
            } else {
                bufferedOutputStream.close();
                return;
            }
        }
    }

    public static void writeToFile(byte[] bArr, File file) {
        FileChannel fileChannel;
        ReadableByteChannel readableByteChannel;
        FileOutputStream fileOutputStream = null;
        FileChannel fileChannel2 = null;
        fileOutputStream = null;
        try {
            readableByteChannel = Channels.newChannel(new ByteArrayInputStream(bArr));
            try {
                FileOutputStream fileOutputStream2 = new FileOutputStream(file);
                try {
                    fileChannel2 = fileOutputStream2.getChannel();
                    fileChannel2.transferFrom(readableByteChannel, 0L, bArr.length);
                    fileOutputStream2.close();
                    if (readableByteChannel != null) {
                        readableByteChannel.close();
                    }
                    fileChannel2.close();
                } catch (Throwable th) {
                    th = th;
                    fileChannel = fileChannel2;
                    fileOutputStream = fileOutputStream2;
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                    if (readableByteChannel != null) {
                        readableByteChannel.close();
                    }
                    if (fileChannel != null) {
                        fileChannel.close();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
                fileChannel = null;
            }
        } catch (Throwable th3) {
            th = th3;
            fileChannel = null;
            readableByteChannel = null;
        }
    }

    public static boolean deleteDir(String str) {
        return deleteDir(new File(str));
    }
}