package com.facebook.internal; import com.facebook.FacebookSdk; import com.facebook.LoggingBehavior; import com.facebook.internal.FileLruCache; import com.facebook.internal.Logger; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.InvalidParameterException; import java.util.Date; import java.util.PriorityQueue; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; import kotlin.Unit; import kotlin.jvm.internal.DefaultConstructorMarker; import kotlin.jvm.internal.Intrinsics; import kotlin.text.Charsets; import kotlin.text.StringsKt__StringsJVMKt; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; /* loaded from: classes2.dex */ public final class FileLruCache { private static final String HEADER_CACHEKEY_KEY = "key"; private static final String HEADER_CACHE_CONTENT_TAG_KEY = "tag"; private final Condition condition; private final File directory; private boolean isTrimInProgress; private boolean isTrimPending; private final AtomicLong lastClearCacheTime; private final Limits limits; private final ReentrantLock lock; private final String tag; public static final Companion Companion = new Companion(null); private static final String TAG = FileLruCache.class.getSimpleName(); private static final AtomicLong bufferIndex = new AtomicLong(); public interface StreamCloseCallback { void onClose(); } public final InputStream get(String key) throws IOException { Intrinsics.checkNotNullParameter(key, "key"); return get$default(this, key, null, 2, null); } public final OutputStream openPutStream(String key) throws IOException { Intrinsics.checkNotNullParameter(key, "key"); return openPutStream$default(this, key, null, 2, null); } public FileLruCache(String tag, Limits limits) { Intrinsics.checkNotNullParameter(tag, "tag"); Intrinsics.checkNotNullParameter(limits, "limits"); this.tag = tag; this.limits = limits; File file = new File(FacebookSdk.getCacheDir(), tag); this.directory = file; ReentrantLock reentrantLock = new ReentrantLock(); this.lock = reentrantLock; this.condition = reentrantLock.newCondition(); this.lastClearCacheTime = new AtomicLong(0L); if (file.mkdirs() || file.isDirectory()) { BufferFile.INSTANCE.deleteAll(file); } } public final long sizeInBytesForTest() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); while (true) { try { if (!this.isTrimPending && !this.isTrimInProgress) { break; } try { this.condition.await(); } catch (InterruptedException unused) { } } catch (Throwable th) { reentrantLock.unlock(); throw th; } } Unit unit = Unit.INSTANCE; reentrantLock.unlock(); File[] listFiles = this.directory.listFiles(); long j = 0; if (listFiles != null) { int length = listFiles.length; int i = 0; while (i < length) { File file = listFiles[i]; i++; j += file.length(); } } return j; } public static /* synthetic */ InputStream get$default(FileLruCache fileLruCache, String str, String str2, int i, Object obj) throws IOException { if ((i & 2) != 0) { str2 = null; } return fileLruCache.get(str, str2); } public final InputStream get(String key, String str) throws IOException { Intrinsics.checkNotNullParameter(key, "key"); File file = new File(this.directory, Utility.md5hash(key)); try { BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file), 8192); try { JSONObject readHeader = StreamHeader.INSTANCE.readHeader(bufferedInputStream); if (readHeader == null) { return null; } if (!Intrinsics.areEqual(readHeader.optString("key"), key)) { return null; } String optString = readHeader.optString("tag", null); if (str == null && !Intrinsics.areEqual(str, optString)) { return null; } long time = new Date().getTime(); Logger.Companion companion = Logger.Companion; LoggingBehavior loggingBehavior = LoggingBehavior.CACHE; String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion.log(loggingBehavior, TAG2, "Setting lastModified to " + Long.valueOf(time) + " for " + ((Object) file.getName())); file.setLastModified(time); return bufferedInputStream; } finally { bufferedInputStream.close(); } } catch (IOException unused) { return null; } } public static /* synthetic */ OutputStream openPutStream$default(FileLruCache fileLruCache, String str, String str2, int i, Object obj) throws IOException { if ((i & 2) != 0) { str2 = null; } return fileLruCache.openPutStream(str, str2); } public final OutputStream openPutStream(final String key, String str) throws IOException { Intrinsics.checkNotNullParameter(key, "key"); final File newFile = BufferFile.INSTANCE.newFile(this.directory); newFile.delete(); if (!newFile.createNewFile()) { throw new IOException(Intrinsics.stringPlus("Could not create file at ", newFile.getAbsolutePath())); } try { FileOutputStream fileOutputStream = new FileOutputStream(newFile); final long currentTimeMillis = System.currentTimeMillis(); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new CloseCallbackOutputStream(fileOutputStream, new StreamCloseCallback() { // from class: com.facebook.internal.FileLruCache$openPutStream$renameToTargetCallback$1 @Override // com.facebook.internal.FileLruCache.StreamCloseCallback public void onClose() { AtomicLong atomicLong; long j = currentTimeMillis; atomicLong = this.lastClearCacheTime; if (j >= atomicLong.get()) { this.renameToTargetAndTrim(key, newFile); } else { newFile.delete(); } } }), 8192); try { try { JSONObject jSONObject = new JSONObject(); jSONObject.put("key", key); if (!Utility.isNullOrEmpty(str)) { jSONObject.put("tag", str); } StreamHeader.INSTANCE.writeHeader(bufferedOutputStream, jSONObject); return bufferedOutputStream; } catch (JSONException e) { Logger.Companion companion = Logger.Companion; LoggingBehavior loggingBehavior = LoggingBehavior.CACHE; String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion.log(loggingBehavior, 5, TAG2, Intrinsics.stringPlus("Error creating JSON header for cache file: ", e)); throw new IOException(e.getMessage()); } } catch (Throwable th) { bufferedOutputStream.close(); throw th; } } catch (FileNotFoundException e2) { Logger.Companion companion2 = Logger.Companion; LoggingBehavior loggingBehavior2 = LoggingBehavior.CACHE; String TAG3 = TAG; Intrinsics.checkNotNullExpressionValue(TAG3, "TAG"); companion2.log(loggingBehavior2, 5, TAG3, Intrinsics.stringPlus("Error creating buffer output stream: ", e2)); throw new IOException(e2.getMessage()); } } public final void clearCache() { final File[] listFiles = this.directory.listFiles(BufferFile.INSTANCE.excludeBufferFiles()); this.lastClearCacheTime.set(System.currentTimeMillis()); if (listFiles != null) { FacebookSdk.getExecutor().execute(new Runnable() { // from class: com.facebook.internal.FileLruCache$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { FileLruCache.m569clearCache$lambda1(listFiles); } }); } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: clearCache$lambda-1, reason: not valid java name */ public static final void m569clearCache$lambda1(File[] filesToDelete) { Intrinsics.checkNotNullExpressionValue(filesToDelete, "filesToDelete"); int length = filesToDelete.length; int i = 0; while (i < length) { File file = filesToDelete[i]; i++; file.delete(); } } public final String getLocation() { String path = this.directory.getPath(); Intrinsics.checkNotNullExpressionValue(path, "directory.path"); return path; } /* JADX INFO: Access modifiers changed from: private */ public final void renameToTargetAndTrim(String str, File file) { if (!file.renameTo(new File(this.directory, Utility.md5hash(str)))) { file.delete(); } postTrim(); } public final InputStream interceptAndPut(String key, InputStream input) throws IOException { Intrinsics.checkNotNullParameter(key, "key"); Intrinsics.checkNotNullParameter(input, "input"); return new CopyingInputStream(input, openPutStream$default(this, key, null, 2, null)); } public String toString() { return "{FileLruCache: tag:" + this.tag + " file:" + ((Object) this.directory.getName()) + '}'; } private final void postTrim() { ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { if (!this.isTrimPending) { this.isTrimPending = true; FacebookSdk.getExecutor().execute(new Runnable() { // from class: com.facebook.internal.FileLruCache$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { FileLruCache.m570postTrim$lambda3$lambda2(FileLruCache.this); } }); } Unit unit = Unit.INSTANCE; reentrantLock.unlock(); } catch (Throwable th) { reentrantLock.unlock(); throw th; } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: postTrim$lambda-3$lambda-2, reason: not valid java name */ public static final void m570postTrim$lambda3$lambda2(FileLruCache this$0) { Intrinsics.checkNotNullParameter(this$0, "this$0"); this$0.trim(); } private final void trim() { long j; ReentrantLock reentrantLock = this.lock; reentrantLock.lock(); try { this.isTrimPending = false; this.isTrimInProgress = true; Unit unit = Unit.INSTANCE; reentrantLock.unlock(); try { Logger.Companion companion = Logger.Companion; LoggingBehavior loggingBehavior = LoggingBehavior.CACHE; String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion.log(loggingBehavior, TAG2, "trim started"); PriorityQueue priorityQueue = new PriorityQueue(); File[] listFiles = this.directory.listFiles(BufferFile.INSTANCE.excludeBufferFiles()); long j2 = 0; if (listFiles != null) { int length = listFiles.length; int i = 0; j = 0; while (i < length) { File file = listFiles[i]; i++; Intrinsics.checkNotNullExpressionValue(file, "file"); ModifiedFile modifiedFile = new ModifiedFile(file); priorityQueue.add(modifiedFile); Logger.Companion companion2 = Logger.Companion; LoggingBehavior loggingBehavior2 = LoggingBehavior.CACHE; String TAG3 = TAG; Intrinsics.checkNotNullExpressionValue(TAG3, "TAG"); companion2.log(loggingBehavior2, TAG3, " trim considering time=" + Long.valueOf(modifiedFile.getModified()) + " name=" + ((Object) modifiedFile.getFile().getName())); j2 += file.length(); j++; listFiles = listFiles; } } else { j = 0; } while (true) { if (j2 <= this.limits.getByteCount() && j <= this.limits.getFileCount()) { this.lock.lock(); try { this.isTrimInProgress = false; this.condition.signalAll(); Unit unit2 = Unit.INSTANCE; return; } finally { } } File file2 = ((ModifiedFile) priorityQueue.remove()).getFile(); Logger.Companion companion3 = Logger.Companion; LoggingBehavior loggingBehavior3 = LoggingBehavior.CACHE; String TAG4 = TAG; Intrinsics.checkNotNullExpressionValue(TAG4, "TAG"); companion3.log(loggingBehavior3, TAG4, Intrinsics.stringPlus(" trim removing ", file2.getName())); j2 -= file2.length(); j--; file2.delete(); } } catch (Throwable th) { this.lock.lock(); try { this.isTrimInProgress = false; this.condition.signalAll(); Unit unit3 = Unit.INSTANCE; throw th; } finally { } } } finally { } } public static final class BufferFile { private static final String FILE_NAME_PREFIX = "buffer"; public static final BufferFile INSTANCE = new BufferFile(); private static final FilenameFilter filterExcludeBufferFiles = new FilenameFilter() { // from class: com.facebook.internal.FileLruCache$BufferFile$$ExternalSyntheticLambda0 @Override // java.io.FilenameFilter public final boolean accept(File file, String str) { boolean m571filterExcludeBufferFiles$lambda0; m571filterExcludeBufferFiles$lambda0 = FileLruCache.BufferFile.m571filterExcludeBufferFiles$lambda0(file, str); return m571filterExcludeBufferFiles$lambda0; } }; private static final FilenameFilter filterExcludeNonBufferFiles = new FilenameFilter() { // from class: com.facebook.internal.FileLruCache$BufferFile$$ExternalSyntheticLambda1 @Override // java.io.FilenameFilter public final boolean accept(File file, String str) { boolean m572filterExcludeNonBufferFiles$lambda1; m572filterExcludeNonBufferFiles$lambda1 = FileLruCache.BufferFile.m572filterExcludeNonBufferFiles$lambda1(file, str); return m572filterExcludeNonBufferFiles$lambda1; } }; public final FilenameFilter excludeBufferFiles() { return filterExcludeBufferFiles; } public final FilenameFilter excludeNonBufferFiles() { return filterExcludeNonBufferFiles; } private BufferFile() { } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: filterExcludeBufferFiles$lambda-0, reason: not valid java name */ public static final boolean m571filterExcludeBufferFiles$lambda0(File file, String filename) { Intrinsics.checkNotNullExpressionValue(filename, "filename"); return !StringsKt__StringsJVMKt.startsWith$default(filename, FILE_NAME_PREFIX, false, 2, null); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: filterExcludeNonBufferFiles$lambda-1, reason: not valid java name */ public static final boolean m572filterExcludeNonBufferFiles$lambda1(File file, String filename) { Intrinsics.checkNotNullExpressionValue(filename, "filename"); return StringsKt__StringsJVMKt.startsWith$default(filename, FILE_NAME_PREFIX, false, 2, null); } public final void deleteAll(File root) { Intrinsics.checkNotNullParameter(root, "root"); File[] listFiles = root.listFiles(excludeNonBufferFiles()); if (listFiles != null) { int length = listFiles.length; int i = 0; while (i < length) { File file = listFiles[i]; i++; file.delete(); } } } public final File newFile(File file) { return new File(file, Intrinsics.stringPlus(FILE_NAME_PREFIX, Long.valueOf(FileLruCache.bufferIndex.incrementAndGet()))); } } public static final class StreamHeader { private static final int HEADER_VERSION = 0; public static final StreamHeader INSTANCE = new StreamHeader(); private StreamHeader() { } public final void writeHeader(OutputStream stream, JSONObject header) throws IOException { Intrinsics.checkNotNullParameter(stream, "stream"); Intrinsics.checkNotNullParameter(header, "header"); String jSONObject = header.toString(); Intrinsics.checkNotNullExpressionValue(jSONObject, "header.toString()"); byte[] bytes = jSONObject.getBytes(Charsets.UTF_8); Intrinsics.checkNotNullExpressionValue(bytes, "(this as java.lang.String).getBytes(charset)"); stream.write(0); stream.write((bytes.length >> 16) & 255); stream.write((bytes.length >> 8) & 255); stream.write(bytes.length & 255); stream.write(bytes); } public final JSONObject readHeader(InputStream stream) throws IOException { Intrinsics.checkNotNullParameter(stream, "stream"); if (stream.read() != 0) { return null; } int i = 0; int i2 = 0; for (int i3 = 0; i3 < 3; i3++) { int read = stream.read(); if (read == -1) { Logger.Companion companion = Logger.Companion; LoggingBehavior loggingBehavior = LoggingBehavior.CACHE; String TAG = FileLruCache.Companion.getTAG(); Intrinsics.checkNotNullExpressionValue(TAG, "TAG"); companion.log(loggingBehavior, TAG, "readHeader: stream.read returned -1 while reading header size"); return null; } i2 = (i2 << 8) + (read & 255); } byte[] bArr = new byte[i2]; while (i < i2) { int read2 = stream.read(bArr, i, i2 - i); if (read2 < 1) { Logger.Companion companion2 = Logger.Companion; LoggingBehavior loggingBehavior2 = LoggingBehavior.CACHE; String TAG2 = FileLruCache.Companion.getTAG(); Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion2.log(loggingBehavior2, TAG2, "readHeader: stream.read stopped at " + Integer.valueOf(i) + " when expected " + i2); return null; } i += read2; } try { Object nextValue = new JSONTokener(new String(bArr, Charsets.UTF_8)).nextValue(); if (!(nextValue instanceof JSONObject)) { Logger.Companion companion3 = Logger.Companion; LoggingBehavior loggingBehavior3 = LoggingBehavior.CACHE; String TAG3 = FileLruCache.Companion.getTAG(); Intrinsics.checkNotNullExpressionValue(TAG3, "TAG"); companion3.log(loggingBehavior3, TAG3, Intrinsics.stringPlus("readHeader: expected JSONObject, got ", nextValue.getClass().getCanonicalName())); return null; } return (JSONObject) nextValue; } catch (JSONException e) { throw new IOException(e.getMessage()); } } } public static final class CloseCallbackOutputStream extends OutputStream { private final StreamCloseCallback callback; private final OutputStream innerStream; public final StreamCloseCallback getCallback() { return this.callback; } public final OutputStream getInnerStream() { return this.innerStream; } public CloseCallbackOutputStream(OutputStream innerStream, StreamCloseCallback callback) { Intrinsics.checkNotNullParameter(innerStream, "innerStream"); Intrinsics.checkNotNullParameter(callback, "callback"); this.innerStream = innerStream; this.callback = callback; } @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { try { this.innerStream.close(); } finally { this.callback.onClose(); } } @Override // java.io.OutputStream, java.io.Flushable public void flush() throws IOException { this.innerStream.flush(); } @Override // java.io.OutputStream public void write(byte[] buffer, int i, int i2) throws IOException { Intrinsics.checkNotNullParameter(buffer, "buffer"); this.innerStream.write(buffer, i, i2); } @Override // java.io.OutputStream public void write(byte[] buffer) throws IOException { Intrinsics.checkNotNullParameter(buffer, "buffer"); this.innerStream.write(buffer); } @Override // java.io.OutputStream public void write(int i) throws IOException { this.innerStream.write(i); } } public static final class CopyingInputStream extends InputStream { private final InputStream input; private final OutputStream output; public final InputStream getInput() { return this.input; } public final OutputStream getOutput() { return this.output; } @Override // java.io.InputStream public boolean markSupported() { return false; } public CopyingInputStream(InputStream input, OutputStream output) { Intrinsics.checkNotNullParameter(input, "input"); Intrinsics.checkNotNullParameter(output, "output"); this.input = input; this.output = output; } @Override // java.io.InputStream public int available() throws IOException { return this.input.available(); } @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { try { this.input.close(); } finally { this.output.close(); } } @Override // java.io.InputStream public void mark(int i) { throw new UnsupportedOperationException(); } @Override // java.io.InputStream public int read(byte[] buffer) throws IOException { Intrinsics.checkNotNullParameter(buffer, "buffer"); int read = this.input.read(buffer); if (read > 0) { this.output.write(buffer, 0, read); } return read; } @Override // java.io.InputStream public int read() throws IOException { int read = this.input.read(); if (read >= 0) { this.output.write(read); } return read; } @Override // java.io.InputStream public int read(byte[] buffer, int i, int i2) throws IOException { Intrinsics.checkNotNullParameter(buffer, "buffer"); int read = this.input.read(buffer, i, i2); if (read > 0) { this.output.write(buffer, i, read); } return read; } @Override // java.io.InputStream public synchronized void reset() { throw new UnsupportedOperationException(); } @Override // java.io.InputStream public long skip(long j) throws IOException { int read; byte[] bArr = new byte[1024]; long j2 = 0; while (j2 < j && (read = read(bArr, 0, (int) Math.min(j - j2, 1024))) >= 0) { j2 += read; } return j2; } } public static final class Limits { private int byteCount = 1048576; private int fileCount = 1024; public final int getByteCount() { return this.byteCount; } public final int getFileCount() { return this.fileCount; } public final void setByteCount(int i) { if (i < 0) { throw new InvalidParameterException("Cache byte-count limit must be >= 0"); } this.byteCount = i; } public final void setFileCount(int i) { if (i < 0) { throw new InvalidParameterException("Cache file count limit must be >= 0"); } this.fileCount = i; } } public static final class ModifiedFile implements Comparable { public static final Companion Companion = new Companion(null); private static final int HASH_MULTIPLIER = 37; private static final int HASH_SEED = 29; private final File file; private final long modified; public final File getFile() { return this.file; } public final long getModified() { return this.modified; } public ModifiedFile(File file) { Intrinsics.checkNotNullParameter(file, "file"); this.file = file; this.modified = file.lastModified(); } @Override // java.lang.Comparable public int compareTo(ModifiedFile another) { Intrinsics.checkNotNullParameter(another, "another"); long j = this.modified; long j2 = another.modified; if (j < j2) { return -1; } if (j > j2) { return 1; } return this.file.compareTo(another.file); } public boolean equals(Object obj) { return (obj instanceof ModifiedFile) && compareTo((ModifiedFile) obj) == 0; } public int hashCode() { return ((1073 + this.file.hashCode()) * 37) + ((int) (this.modified % Integer.MAX_VALUE)); } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } } } public static final class Companion { public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } private Companion() { } public final String getTAG() { return FileLruCache.TAG; } } }