package com.facebook.internal; import android.net.Uri; import com.facebook.LoggingBehavior; import com.facebook.internal.FileLruCache; import com.facebook.internal.Logger; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import kotlin.jvm.internal.Intrinsics; import kotlin.text.StringsKt__StringsJVMKt; /* loaded from: classes2.dex */ public final class ImageResponseCache { public static final ImageResponseCache INSTANCE = new ImageResponseCache(); private static final String TAG = ImageResponseCache.class.getSimpleName(); private static FileLruCache imageCache; public final String getTAG() { return TAG; } private ImageResponseCache() { } public static final synchronized FileLruCache getCache() throws IOException { FileLruCache fileLruCache; synchronized (ImageResponseCache.class) { try { if (imageCache == null) { String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); imageCache = new FileLruCache(TAG2, new FileLruCache.Limits()); } fileLruCache = imageCache; if (fileLruCache == null) { Intrinsics.throwUninitializedPropertyAccessException("imageCache"); throw null; } } catch (Throwable th) { throw th; } } return fileLruCache; } public static final InputStream getCachedImageStream(Uri uri) { if (uri == null || !INSTANCE.isCDNURL(uri)) { return null; } try { FileLruCache cache = getCache(); String uri2 = uri.toString(); Intrinsics.checkNotNullExpressionValue(uri2, "uri.toString()"); return FileLruCache.get$default(cache, uri2, null, 2, null); } catch (IOException e) { Logger.Companion companion = Logger.Companion; LoggingBehavior loggingBehavior = LoggingBehavior.CACHE; String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion.log(loggingBehavior, 5, TAG2, e.toString()); return null; } } public static final InputStream interceptAndCacheImageStream(HttpURLConnection connection) throws IOException { Intrinsics.checkNotNullParameter(connection, "connection"); if (connection.getResponseCode() != 200) { return null; } Uri parse = Uri.parse(connection.getURL().toString()); InputStream inputStream = connection.getInputStream(); try { if (!INSTANCE.isCDNURL(parse)) { return inputStream; } FileLruCache cache = getCache(); String uri = parse.toString(); Intrinsics.checkNotNullExpressionValue(uri, "uri.toString()"); return cache.interceptAndPut(uri, new BufferedHttpInputStream(inputStream, connection)); } catch (IOException unused) { return inputStream; } } private final boolean isCDNURL(Uri uri) { String host; return (uri == null || (host = uri.getHost()) == null || (!Intrinsics.areEqual(host, "fbcdn.net") && !StringsKt__StringsJVMKt.endsWith$default(host, ".fbcdn.net", false, 2, null) && (!StringsKt__StringsJVMKt.startsWith$default(host, "fbcdn", false, 2, null) || !StringsKt__StringsJVMKt.endsWith$default(host, ".akamaihd.net", false, 2, null)))) ? false : true; } public static final void clearCache() { try { getCache().clearCache(); } catch (IOException e) { Logger.Companion companion = Logger.Companion; LoggingBehavior loggingBehavior = LoggingBehavior.CACHE; String TAG2 = TAG; Intrinsics.checkNotNullExpressionValue(TAG2, "TAG"); companion.log(loggingBehavior, 5, TAG2, Intrinsics.stringPlus("clearCache failed ", e.getMessage())); } } public static final class BufferedHttpInputStream extends BufferedInputStream { private HttpURLConnection connection; public final HttpURLConnection getConnection() { return this.connection; } public final void setConnection(HttpURLConnection httpURLConnection) { Intrinsics.checkNotNullParameter(httpURLConnection, ""); this.connection = httpURLConnection; } /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ public BufferedHttpInputStream(InputStream inputStream, HttpURLConnection connection) { super(inputStream, 8192); Intrinsics.checkNotNullParameter(connection, "connection"); this.connection = connection; } @Override // java.io.BufferedInputStream, java.io.FilterInputStream, java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { super.close(); Utility utility = Utility.INSTANCE; Utility.disconnectQuietly(this.connection); } } }