导航菜单

页面标题

页面副标题

Brevistay v5.8.2 - FileProviderService.java 源代码

正在查看: Brevistay v5.8.2 应用的 FileProviderService.java JAVA 源代码文件

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


package in.juspay.hypersdk.services;

import a.a.a.o.f$;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import com.google.firebase.crashlytics.internal.metadata.UserMetadata;
import com.google.firebase.sessions.settings.RemoteSettings;
import in.juspay.hyper.constants.Labels;
import in.juspay.hyper.constants.LogCategory;
import in.juspay.hyper.constants.LogLevel;
import in.juspay.hyper.constants.LogSubCategory;
import in.juspay.hyper.core.FileProviderInterface;
import in.juspay.hyper.core.JuspayLogger;
import in.juspay.hypersdk.core.Constants;
import in.juspay.hypersdk.core.JuspayServices;
import in.juspay.hypersdk.core.PaymentConstants;
import in.juspay.hypersdk.core.SdkTracker;
import in.juspay.hypersdk.data.SdkInfo;
import in.juspay.hypersdk.security.EncryptionHelper;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import org.json.JSONException;
import org.json.JSONObject;

public class FileProviderService implements FileProviderInterface {
    private static final String LOG_TAG = "FileProviderService";
    private final JuspayServices juspayServices;
    private final Workspace workspace;
    private final Map<String, String> fileCache = new HashMap();
    private final List<String> fileCacheWhiteList = new ArrayList();
    private final boolean shouldCheckInternalAssets = true;

    static class AnonymousClass1 {
        static final int[] $SwitchMap$in$juspay$hypersdk$services$FileProviderService$Mode;

        static {
            int[] iArr = new int[Mode.values().length];
            $SwitchMap$in$juspay$hypersdk$services$FileProviderService$Mode = iArr;
            try {
                iArr[Mode.NEW.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$in$juspay$hypersdk$services$FileProviderService$Mode[Mode.RE_OPEN.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    enum Mode {
        NEW,
        RE_OPEN
    }

    public final class TempWriter {
        private final File tempDir;

        TempWriter(String str, Mode mode) {
            File openInCache;
            int i = AnonymousClass1.$SwitchMap$in$juspay$hypersdk$services$FileProviderService$Mode[mode.ordinal()];
            if (i == 1) {
                openInCache = FileProviderService.this.workspace.openInCache(String.format("temp-%s-%s", str, Long.valueOf(System.currentTimeMillis())));
                openInCache.mkdir();
            } else if (i != 2) {
                openInCache = null;
            } else {
                openInCache = FileProviderService.this.workspace.openInCache(str);
                if (!openInCache.exists()) {
                    throw new FileNotFoundException(str + " does not exist in cache!");
                }
            }
            this.tempDir = openInCache;
        }

        public boolean copyToMain(String str, String str2) {
            StandardCopyOption standardCopyOption;
            String asDecryptedJSA = FileProviderService.asDecryptedJSA(FileProviderService.this.appendSdkNameAndVersion(str));
            if (Build.VERSION.SDK_INT < 26) {
                FileProviderService fileProviderService = FileProviderService.this;
                File file = new File(this.tempDir, asDecryptedJSA);
                FileProviderService fileProviderService2 = FileProviderService.this;
                return fileProviderService.copyFile(file, fileProviderService2.getFileFromInternalStorage(fileProviderService2.juspayServices.getContext(), str2 + RemoteSettings.FORWARD_SLASH_STRING + asDecryptedJSA));
            }
            try {
                Path m = f$.ExternalSyntheticApiModelOutline0.m(new File(this.tempDir, asDecryptedJSA));
                FileProviderService fileProviderService3 = FileProviderService.this;
                Path m2 = f$.ExternalSyntheticApiModelOutline0.m(fileProviderService3.getFileFromInternalStorage(fileProviderService3.juspayServices.getContext(), str2 + RemoteSettings.FORWARD_SLASH_STRING + asDecryptedJSA));
                standardCopyOption = StandardCopyOption.REPLACE_EXISTING;
                Files.copy(m, m2, standardCopyOption);
                return true;
            } catch (Exception unused) {
                return false;
            }
        }

        public String getDirName() {
            return this.tempDir.getName();
        }

        public String[] list() {
            return FileProviderService.listFiles(this.tempDir);
        }

        public boolean write(String str, byte[] bArr) {
            File file = new File(this.tempDir, FileProviderService.this.appendSdkNameAndVersion(str));
            FileProviderService fileProviderService = FileProviderService.this;
            return fileProviderService.writeToFile(fileProviderService.juspayServices.getContext(), file, bArr, false);
        }
    }

    public FileProviderService(JuspayServices juspayServices) {
        this.juspayServices = juspayServices;
        this.workspace = juspayServices.getWorkspace();
    }

    public static String asDecryptedJSA(String str) {
        return replaceExtension(str, ".jsa", Constants.DECRYPTED_JSA_EXT);
    }

    private static String asEncryptedJSA(String str) {
        return replaceExtension(str, Constants.DECRYPTED_JSA_EXT, ".jsa");
    }

    private void cacheFile(String str, String str2) {
        this.fileCache.put(str, str2);
        this.juspayServices.sdkDebug(LOG_TAG, "Caching file: " + str);
    }

    private void copyFile(Context context, String str, String str2) {
        copyFile(getFileFromInternalStorage(context, str), getFileFromInternalStorage(context, str2));
    }

    private void deleteAndRemoveMetadata(String str) {
        try {
            getFileFromInternalStorage(this.juspayServices.getContext(), str).delete();
            this.juspayServices.getRemoteAssetService().resetMetadata(str);
        } catch (Exception unused) {
        }
    }

    private void deleteFileFromCache(String str) {
        if (isFileCached(str)) {
            this.fileCache.remove(str);
        }
    }

    public File getFileFromInternalStorage(Context context, String str) {
        this.juspayServices.sdkDebug(LOG_TAG, "Context while reading Internal Storage :" + context);
        this.juspayServices.sdkDebug(LOG_TAG, "Getting file from internal storage. Filename: " + str);
        File open = this.workspace.open(str);
        File parentFile = open.getParentFile();
        if (parentFile != null && !parentFile.exists()) {
            parentFile.mkdirs();
        }
        return open;
    }

    private boolean isFileCached(String str) {
        return this.fileCache.containsKey(str);
    }

    private InputStream openAsset(Context context, String str) {
        return this.workspace.openAsset(str);
    }

    private String readFromAssets(Context context, String str) {
        SdkTracker sdkTracker = this.juspayServices.getSdkTracker();
        try {
            byte[] assetFileAsByte = getAssetFileAsByte(context, str);
            if (!str.endsWith("jsa")) {
                this.juspayServices.sdkDebug(LOG_TAG, "Done reading " + str + " from assets");
                return new String(assetFileAsByte);
            }
            this.juspayServices.sdkDebug(LOG_TAG, "Read JSA Asset file " + str + " with encrypted hash - " + EncryptionHelper.md5(assetFileAsByte));
            return new String(EncryptionHelper.decryptThenGunzip(assetFileAsByte, "v1"));
        } catch (Exception e) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception trying to read from file: " + str, e);
            return null;
        }
    }

    private void readFromInputStream(ByteArrayOutputStream byteArrayOutputStream, InputStream inputStream) {
        byte[] bArr = new byte[4096];
        while (true) {
            int read = inputStream.read(bArr);
            if (read == -1) {
                return;
            } else {
                byteArrayOutputStream.write(bArr, 0, read);
            }
        }
    }

    private String readFromInternalStorage(Context context, String str) {
        String appendSdkNameAndVersion = appendSdkNameAndVersion(str);
        if (this.juspayServices.getSdkInfo().usesLocalAssets()) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        SdkTracker sdkTracker = this.juspayServices.getSdkTracker();
        try {
            if (appendSdkNameAndVersion.endsWith("jsa")) {
                byte[] decryptGunzipInternalStorage = decryptGunzipInternalStorage(context, appendSdkNameAndVersion);
                if (decryptGunzipInternalStorage != null) {
                    return new String(decryptGunzipInternalStorage);
                }
                sdkTracker.trackAction(LogSubCategory.Action.SYSTEM, LogLevel.WARNING, Labels.System.FILE_PROVIDER_SERVICE, "readFromInternalStorage", "Returning null from internal storage for " + appendSdkNameAndVersion);
                return null;
            }
            FileInputStream fileInputStream = new FileInputStream(getFileFromInternalStorage(context, appendSdkNameAndVersion));
            try {
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                try {
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    while (true) {
                        try {
                            int read = bufferedReader.read();
                            if (read == -1) {
                                break;
                            }
                            sb.append((char) read);
                        } finally {
                        }
                    }
                    bufferedReader.close();
                    inputStreamReader.close();
                    sdkTracker.trackAction(LogSubCategory.Action.SYSTEM, LogLevel.DEBUG, Labels.System.FILE_PROVIDER_SERVICE, "readFromInternalStorage", "Returning the file content without decryption for " + appendSdkNameAndVersion);
                    if (appendSdkNameAndVersion.endsWith(".json")) {
                        try {
                            new JSONObject(String.valueOf(sb));
                        } catch (JSONException unused) {
                            deleteAndRemoveMetadata(appendSdkNameAndVersion);
                            fileInputStream.close();
                            return null;
                        }
                    }
                    String sb2 = sb.toString();
                    fileInputStream.close();
                    return sb2;
                } finally {
                }
            } finally {
            }
        } catch (Exception e) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "read from internal storage failed", e);
            return null;
        }
    }

    private static String replaceExtension(String str, String str2, String str3) {
        return str.endsWith(str2) ? str.replace(str2, str3) : str;
    }

    public static String stripSdkNameAndVersion(String str) {
        try {
            int lastIndexOf = str.lastIndexOf(46);
            return str.substring(0, str.lastIndexOf(95, str.lastIndexOf(95, lastIndexOf) - 1)) + str.substring(lastIndexOf);
        } catch (Exception unused) {
            return str;
        }
    }

    private void updateFallback(Context context, String str, String str2) {
        if (str2.endsWith("jsa") && isFilePresent(context, str2)) {
            this.juspayServices.sdkDebug(LOG_TAG, "updateFallback: starting" + str2 + "  " + str);
            try {
                String md5 = EncryptionHelper.md5(decryptGunzipInternalStorage(context, str2));
                JSONObject jSONObject = new JSONObject(this.workspace.getFromSharedPreference(PaymentConstants.JP_BLOCKED_HASH, "{}"));
                this.juspayServices.sdkDebug(LOG_TAG, "updateFallback: got the blocked hash");
                if (!jSONObject.has(str)) {
                    copyFile(context, str2, "fb/" + str2);
                    this.juspayServices.sdkDebug(LOG_TAG, "updateFallback: we didn;t get the file name from blocked hash " + str2);
                    this.juspayServices.sdkDebug(LOG_TAG, "updateFallback: wonderful.. copying to the fallback");
                    this.juspayServices.sdkDebug(LOG_TAG, "updateFallback: file copied");
                    return;
                }
                this.juspayServices.sdkDebug(LOG_TAG, "updateFallback: got the file name " + str);
                JSONObject jSONObject2 = jSONObject.getJSONObject(str);
                if (jSONObject2.has("latest_hash") && jSONObject2.getString("latest_hash").equals(md5)) {
                    return;
                }
                this.juspayServices.sdkDebug(LOG_TAG, "updateFallback: wonderful.. copying to the fallback");
                copyFile(context, str2, "fb/" + str2);
                jSONObject2.remove("latest_hash");
                jSONObject.put(str, jSONObject2);
                this.workspace.writeToSharedPreference(PaymentConstants.JP_BLOCKED_HASH, jSONObject.toString());
                this.juspayServices.sdkDebug(LOG_TAG, "updateFallback: file copied");
            } catch (FileNotFoundException e) {
                this.juspayServices.getSdkTracker().trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.HyperSdk.AUTO_FALLBACK, "File not found: " + str2, e);
            } catch (Exception e2) {
                this.juspayServices.getSdkTracker().trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.HyperSdk.AUTO_FALLBACK, "Exception: " + str2, e2);
            }
        }
    }

    private boolean writeToFile(Context context, String str, byte[] bArr, boolean z) {
        String appendSdkNameAndVersion = appendSdkNameAndVersion(str);
        updateFallback(context, str, appendSdkNameAndVersion);
        deleteFileFromCache(appendSdkNameAndVersion);
        return writeToFile(context, getFileFromInternalStorage(context, appendSdkNameAndVersion), bArr, z);
    }

    public void addToFileCacheWhiteList(String str) {
        this.fileCacheWhiteList.add(str);
    }

    public String appendSdkNameAndVersion(String str) {
        SdkInfo sdkInfo = this.juspayServices.getSdkInfo();
        int lastIndexOf = str.lastIndexOf(46);
        if (lastIndexOf <= 0 || lastIndexOf >= str.length() - 1) {
            return str + "_" + sdkInfo.getSdkName() + "_" + sdkInfo.getSdkVersion();
        }
        return str.substring(0, lastIndexOf) + "_" + sdkInfo.getSdkName() + "_" + sdkInfo.getSdkVersion() + str.substring(lastIndexOf);
    }

    public byte[] decryptGunzipAssetFile(Context context, String str) {
        SdkTracker sdkTracker = this.juspayServices.getSdkTracker();
        byte[] bArr = new byte[0];
        try {
            bArr = getAssetFileAsByte(context, str);
        } catch (Exception e) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception in reading " + str + " from assets", e);
        }
        return EncryptionHelper.decryptThenGunzip(bArr, "v1");
    }

    public byte[] decryptGunzipInternalStorage(Context context, String str) {
        SdkTracker sdkTracker = this.juspayServices.getSdkTracker();
        try {
            try {
                File fileFromInternalStorage = getFileFromInternalStorage(context, asDecryptedJSA(str));
                if (fileFromInternalStorage.exists()) {
                    int length = (int) fileFromInternalStorage.length();
                    byte[] bArr = new byte[length];
                    FileInputStream fileInputStream = new FileInputStream(fileFromInternalStorage);
                    try {
                        if (fileInputStream.read(bArr) == length) {
                            fileInputStream.close();
                            return bArr;
                        }
                        fileInputStream.close();
                    } finally {
                    }
                }
            } catch (Exception e) {
                sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception in reading " + asDecryptedJSA(str) + " from internal storage", e);
            }
            byte[] internalStorageFileAsByte = getInternalStorageFileAsByte(context, str);
            this.juspayServices.sdkDebug(LOG_TAG, "Read Encrypted file from internalStorage - " + str + " with encrypted hash - " + EncryptionHelper.md5(internalStorageFileAsByte));
            return EncryptionHelper.decryptThenGunzip(internalStorageFileAsByte, "v1");
        } catch (FileNotFoundException e2) {
            this.juspayServices.sdkDebug(LOG_TAG, "No File to decrypt in internal storage: " + str);
            throw e2;
        } catch (Exception e3) {
            deleteAndRemoveMetadata(asDecryptedJSA(str));
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception in reading " + str + " from internal storage", e3);
            return null;
        }
    }

    public boolean deleteFileFromInternalStorage(Context context, String str) {
        SdkTracker sdkTracker = this.juspayServices.getSdkTracker();
        RemoteAssetService remoteAssetService = this.juspayServices.getRemoteAssetService();
        File fileFromInternalStorage = getFileFromInternalStorage(context, str);
        if (!fileFromInternalStorage.exists()) {
            JuspayLogger.e(LOG_TAG, str + " not found");
            return false;
        }
        this.juspayServices.sdkDebug(LOG_TAG, "Deleting " + str + " from internal storage");
        JuspayLogger.e(LOG_TAG, "FILE CORRUPTED. DISABLING SDK");
        JuspayLogger.d(LOG_TAG, "Deleted file " + str);
        sdkTracker.trackAction(LogSubCategory.Action.SYSTEM, LogLevel.WARNING, Labels.System.FILE_PROVIDER_SERVICE, "file_corrupted", str);
        try {
            remoteAssetService.resetMetadata(str.replace(".zip", ".jsa"));
        } catch (Exception e) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Error while resetting etag", e);
        }
        return fileFromInternalStorage.delete();
    }

    public byte[] getAssetFileAsByte(String str) {
        return getAssetFileAsByte(this.juspayServices.getContext(), str);
    }

    public byte[] getInternalStorageFileAsByte(Context context, String str) {
        SdkTracker sdkTracker = this.juspayServices.getSdkTracker();
        RemoteAssetService remoteAssetService = this.juspayServices.getRemoteAssetService();
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                FileInputStream fileInputStream = new FileInputStream(getFileFromInternalStorage(context, str));
                try {
                    readFromInputStream(byteArrayOutputStream, fileInputStream);
                    fileInputStream.close();
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    byteArrayOutputStream.close();
                    return byteArray;
                } finally {
                }
            } finally {
            }
        } catch (FileNotFoundException e) {
            this.juspayServices.sdkDebug(LOG_TAG, "File not found " + str);
            try {
                remoteAssetService.resetMetadata(str.replace(".zip", ".jsa"));
                throw e;
            } catch (JSONException unused) {
                sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Couldn't reset " + str, e);
                throw e;
            }
        } catch (IOException e2) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Could not read " + str, e2);
            deleteFileFromInternalStorage(context, str);
            throw new RuntimeException(e2);
        } catch (Exception e3) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception: Could not read " + str, e3);
            deleteFileFromInternalStorage(context, str);
            throw new RuntimeException(e3);
        }
    }

    public boolean isFilePresent(Context context, String str) {
        if (this.workspace.open(appendSdkNameAndVersion(str)).exists()) {
            return true;
        }
        try {
            InputStream openAsset = openAsset(context, str);
            if (openAsset != null) {
                openAsset.close();
            }
            return true;
        } catch (IOException unused) {
            return false;
        }
    }

    public String[] listFiles(String str) {
        return listFiles(this.workspace.open(str));
    }

    public TempWriter newTempWriter(String str) {
        try {
            return new TempWriter(str, Mode.NEW);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public TempWriter reOpenTempWriter(String str) {
        return new TempWriter(str, Mode.RE_OPEN);
    }

    public String readFromCache(String str) {
        if (!isFileCached(str)) {
            return null;
        }
        String str2 = this.fileCache.get(str);
        this.juspayServices.sdkDebug(LOG_TAG, "Returning cached value of the file: " + str);
        this.juspayServices.sdkDebug(LOG_TAG, "Cached: " + str2);
        return str2;
    }

    public String readFromFile(String str) {
        return readFromFile(this.juspayServices.getContext(), str, true);
    }

    @Override
    public void renewFile(String str, String str2, long j) {
        this.juspayServices.getRemoteAssetService().renewFile(this.juspayServices.getContext(), str, null, str2, j);
    }

    public boolean updateCertificate(Context context, String str, byte[] bArr) {
        return writeToFile(context, str, bArr, true);
    }

    public boolean updateFile(Context context, String str, byte[] bArr) {
        return writeToFile(context, str, bArr, false);
    }

    public String writeFileToDisk(Context context, String str, String str2) {
        try {
            File file = new File(context.getExternalFilesDirs(Environment.DIRECTORY_DOWNLOADS)[0].getAbsolutePath());
            file.mkdirs();
            File file2 = new File(file, str2);
            file2.createNewFile();
            if (!file2.exists()) {
                JuspayLogger.d(LOG_TAG, "Exception in creating the file");
                return String.format("{\"error\":\"true\",\"data\":\"%s\"}", "unknown_error::Exception in creating the file");
            }
            FileWriter fileWriter = new FileWriter(file2);
            fileWriter.write(str);
            fileWriter.flush();
            fileWriter.close();
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("error", "false");
            jSONObject.put("data", file);
            return jSONObject.toString();
        } catch (Exception e) {
            JuspayLogger.d(LOG_TAG, "Exception in downloading the file :" + e);
            return String.format("{\"error\":\"true\",\"data\":\"%s\"}", "unknown_error::" + e);
        }
    }

    public boolean copyFile(File file, File file2) {
        try {
            this.juspayServices.sdkDebug(LOG_TAG, "copyFile: " + file.getAbsolutePath() + "   " + file2.getAbsolutePath());
            FileInputStream fileInputStream = new FileInputStream(file);
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file2);
                try {
                    byte[] bArr = new byte[UserMetadata.MAX_ATTRIBUTE_SIZE];
                    while (true) {
                        int read = fileInputStream.read(bArr);
                        if (read == -1) {
                            fileOutputStream.flush();
                            fileOutputStream.close();
                            fileInputStream.close();
                            return true;
                        }
                        fileOutputStream.write(bArr, 0, read);
                    }
                } finally {
                }
            } finally {
            }
        } catch (FileNotFoundException e) {
            this.juspayServices.getSdkTracker().trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "File not found: " + file.getName(), e);
            return false;
        } catch (Exception e2) {
            this.juspayServices.getSdkTracker().trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception: " + file.getName(), e2);
            return false;
        }
    }

    public byte[] getAssetFileAsByte(Context context, String str) {
        SdkTracker sdkTracker = this.juspayServices.getSdkTracker();
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                InputStream openAsset = openAsset(context, str);
                try {
                    readFromInputStream(byteArrayOutputStream, openAsset);
                    if (openAsset != null) {
                        openAsset.close();
                    }
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    byteArrayOutputStream.close();
                    return byteArray;
                } finally {
                }
            } finally {
            }
        } catch (FileNotFoundException e) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Could not read " + str, e);
            throw new RuntimeException(e);
        } catch (IOException e2) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Could not read " + str, e2);
            deleteFileFromInternalStorage(context, str);
            throw new RuntimeException(e2);
        } catch (Exception e3) {
            sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception: Could not read " + str, e3);
            deleteFileFromInternalStorage(context, str);
            return new byte[0];
        }
    }

    @Override
    public String readFromFile(Context context, String str) {
        return readFromFile(context, str, true);
    }

    public boolean updateFile(String str, byte[] bArr) {
        return writeToFile(this.juspayServices.getContext(), str, bArr, false);
    }

    public static String[] listFiles(File file) {
        if (!file.exists()) {
            return null;
        }
        File[] listFiles = file.listFiles();
        TreeSet treeSet = new TreeSet();
        if (listFiles != null) {
            for (File file2 : listFiles) {
                if (file2.isFile()) {
                    treeSet.add(asEncryptedJSA(stripSdkNameAndVersion(file2.getName())));
                }
            }
        }
        return (String[]) treeSet.toArray(new String[0]);
    }

    public String readFromFile(Context context, String str, boolean z) {
        String readFromCache = z ? readFromCache(str) : null;
        if (readFromCache == null) {
            readFromCache = readFromInternalStorage(context, str);
        }
        if (readFromCache == null) {
            readFromCache = readFromAssets(context, str);
        }
        if (this.fileCacheWhiteList.contains(str) && readFromCache != null) {
            cacheFile(str, readFromCache);
        }
        return readFromCache == null ? "" : readFromCache;
    }

    public boolean writeToFile(Context context, File file, byte[] bArr, boolean z) {
        SdkTracker sdkTracker = this.juspayServices.getSdkTracker();
        if (!z) {
            try {
                if (file.getName().contains(".jsa")) {
                    String asDecryptedJSA = asDecryptedJSA(file.getName());
                    File file2 = new File(file.getParentFile(), "temp_" + asDecryptedJSA);
                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(file2);
                        try {
                            fileOutputStream.write(EncryptionHelper.decryptThenGunzip(bArr, "v1"));
                            file2.renameTo(new File(file.getParentFile(), asDecryptedJSA));
                            fileOutputStream.close();
                            return true;
                        } finally {
                        }
                    } catch (Exception e) {
                        sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception writing decrypted js file " + asDecryptedJSA, e);
                        return true;
                    }
                }
            } catch (FileNotFoundException e2) {
                sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "File not found: " + file.getName(), e2);
                return false;
            } catch (IOException e3) {
                sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "IOException: " + file.getName(), e3);
                return false;
            } catch (Exception e4) {
                sdkTracker.trackException(LogCategory.ACTION, LogSubCategory.Action.SYSTEM, Labels.System.FILE_PROVIDER_SERVICE, "Exception: " + file.getName(), e4);
                return false;
            }
        }
        FileOutputStream fileOutputStream2 = new FileOutputStream(file);
        try {
            fileOutputStream2.write(bArr);
            fileOutputStream2.close();
            return true;
        } finally {
        }
    }
}