package com.airbnb.lottie.network; import android.util.Pair; import com.airbnb.lottie.utils.Logger; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /* loaded from: classes.dex */ public class NetworkCache { private final LottieNetworkCacheProvider cacheProvider; public NetworkCache(LottieNetworkCacheProvider lottieNetworkCacheProvider) { this.cacheProvider = lottieNetworkCacheProvider; } public void clear() { File parentDir = parentDir(); if (parentDir.exists()) { File[] listFiles = parentDir.listFiles(); if (listFiles != null && listFiles.length > 0) { for (File file : parentDir.listFiles()) { file.delete(); } } parentDir.delete(); } } /* JADX INFO: Access modifiers changed from: package-private */ public Pair fetch(String str) { FileExtension fileExtension; try { File cachedFile = getCachedFile(str); if (cachedFile == null) { return null; } FileInputStream fileInputStream = new FileInputStream(cachedFile); if (cachedFile.getAbsolutePath().endsWith(".zip")) { fileExtension = FileExtension.ZIP; } else { fileExtension = FileExtension.JSON; } StringBuilder sb = new StringBuilder("Cache hit for "); sb.append(str); sb.append(" at "); sb.append(cachedFile.getAbsolutePath()); Logger.debug(sb.toString()); return new Pair<>(fileExtension, fileInputStream); } catch (FileNotFoundException unused) { return null; } } /* JADX INFO: Access modifiers changed from: package-private */ public File writeTempCacheFile(String str, InputStream inputStream, FileExtension fileExtension) throws IOException { File file = new File(parentDir(), filenameForUrl(str, fileExtension, true)); try { FileOutputStream fileOutputStream = new FileOutputStream(file); try { byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr); if (read != -1) { fileOutputStream.write(bArr, 0, read); } else { fileOutputStream.flush(); return file; } } } finally { fileOutputStream.close(); } } finally { inputStream.close(); } } /* JADX INFO: Access modifiers changed from: package-private */ public void renameTempFile(String str, FileExtension fileExtension) { File file = new File(parentDir(), filenameForUrl(str, fileExtension, true)); File file2 = new File(file.getAbsolutePath().replace(".temp", "")); boolean renameTo = file.renameTo(file2); StringBuilder sb = new StringBuilder("Copying temp file to real file ("); sb.append(file2); sb.append(")"); Logger.debug(sb.toString()); if (renameTo) { return; } StringBuilder sb2 = new StringBuilder("Unable to rename cache file "); sb2.append(file.getAbsolutePath()); sb2.append(" to "); sb2.append(file2.getAbsolutePath()); sb2.append("."); Logger.warning(sb2.toString()); } private File getCachedFile(String str) throws FileNotFoundException { File file = new File(parentDir(), filenameForUrl(str, FileExtension.JSON, false)); if (file.exists()) { return file; } File file2 = new File(parentDir(), filenameForUrl(str, FileExtension.ZIP, false)); if (file2.exists()) { return file2; } return null; } private File parentDir() { File cacheDir = this.cacheProvider.getCacheDir(); if (cacheDir.isFile()) { cacheDir.delete(); } if (!cacheDir.exists()) { cacheDir.mkdirs(); } return cacheDir; } private static String filenameForUrl(String str, FileExtension fileExtension, boolean z) { StringBuilder sb = new StringBuilder("lottie_cache_"); sb.append(str.replaceAll("\\W+", "")); sb.append(z ? fileExtension.tempExtension() : fileExtension.extension); return sb.toString(); } }