package com.bytedance.adsdk.ro.yz;

import android.util.Pair;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: loaded from: classes.dex */
public class wsy {
    private final jnr fm;

    public wsy(jnr jnrVar) {
        this.fm = jnrVar;
    }

    private File ro(String str) throws FileNotFoundException {
        File file = new File(fm(), fm(str, lb.JSON, false));
        if (file.exists()) {
            return file;
        }
        File file2 = new File(fm(), fm(str, lb.ZIP, false));
        if (file2.exists()) {
            return file2;
        }
        return null;
    }

    public Pair<lb, InputStream> fm(String str) {
        try {
            File fileRo = ro(str);
            if (fileRo == null) {
                return null;
            }
            FileInputStream fileInputStream = new FileInputStream(fileRo);
            lb lbVar = fileRo.getAbsolutePath().endsWith(".zip") ? lb.ZIP : lb.JSON;
            fileRo.getAbsolutePath();
            return new Pair<>(lbVar, fileInputStream);
        } catch (FileNotFoundException unused) {
            return null;
        }
    }

    public File fm(String str, InputStream inputStream, lb lbVar) throws IOException {
        File file = new File(fm(), fm(str, lbVar, true));
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            try {
                byte[] bArr = new byte[1024];
                while (true) {
                    int i10 = inputStream.read(bArr);
                    if (i10 != -1) {
                        fileOutputStream.write(bArr, 0, i10);
                    } else {
                        fileOutputStream.flush();
                        fileOutputStream.close();
                        return file;
                    }
                }
            } catch (Throwable th) {
                fileOutputStream.close();
                throw th;
            }
        } finally {
            inputStream.close();
        }
    }

    public void fm(String str, lb lbVar) {
        File file = new File(fm(), fm(str, lbVar, true));
        File file2 = new File(file.getAbsolutePath().replace(".temp", ""));
        boolean zRenameTo = file.renameTo(file2);
        file2.toString();
        if (zRenameTo) {
            return;
        }
        file.getAbsolutePath();
        file2.getAbsolutePath();
    }

    private File fm() {
        File fileFm = this.fm.fm();
        if (fileFm.isFile()) {
            fileFm.delete();
        }
        if (!fileFm.exists()) {
            fileFm.mkdirs();
        }
        return fileFm;
    }

    private static String fm(String str, lb lbVar, boolean z10) {
        StringBuilder sb = new StringBuilder("lottie_cache_");
        sb.append(str.replaceAll("\\W+", ""));
        sb.append(z10 ? lbVar.fm() : lbVar.f6302lb);
        return sb.toString();
    }
}
