package q; import android.content.Context; import android.content.res.Resources; import android.net.Uri; import android.os.CancellationSignal; import android.os.ParcelFileDescriptor; import android.os.Process; import android.os.StrictMode; import android.util.Log; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.Collections; import java.util.HashMap; import java.util.Map; import w.f; /* loaded from: classes.dex */ public abstract class p { public static void a(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException unused) { } } } public static ByteBuffer b(Context context, Resources resources, int i10) { File e10 = e(context); if (e10 == null) { return null; } try { if (c(e10, resources, i10)) { return g(e10); } return null; } finally { e10.delete(); } } public static boolean c(File file, Resources resources, int i10) { InputStream inputStream; try { inputStream = resources.openRawResource(i10); } catch (Throwable th) { th = th; inputStream = null; } try { boolean d10 = d(file, inputStream); a(inputStream); return d10; } catch (Throwable th2) { th = th2; a(inputStream); throw th; } } public static boolean d(File file, InputStream inputStream) { StrictMode.ThreadPolicy allowThreadDiskWrites = StrictMode.allowThreadDiskWrites(); FileOutputStream fileOutputStream = null; try { try { FileOutputStream fileOutputStream2 = new FileOutputStream(file, false); try { byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr); if (read == -1) { a(fileOutputStream2); StrictMode.setThreadPolicy(allowThreadDiskWrites); return true; } fileOutputStream2.write(bArr, 0, read); } } catch (IOException e10) { e = e10; fileOutputStream = fileOutputStream2; Log.e("TypefaceCompatUtil", "Error copying resource contents to temp file: " + e.getMessage()); a(fileOutputStream); StrictMode.setThreadPolicy(allowThreadDiskWrites); return false; } catch (Throwable th) { th = th; fileOutputStream = fileOutputStream2; a(fileOutputStream); StrictMode.setThreadPolicy(allowThreadDiskWrites); throw th; } } catch (Throwable th2) { th = th2; } } catch (IOException e11) { e = e11; } } public static File e(Context context) { File cacheDir = context.getCacheDir(); if (cacheDir == null) { return null; } String str = ".font" + Process.myPid() + "-" + Process.myTid() + "-"; for (int i10 = 0; i10 < 100; i10++) { File file = new File(cacheDir, str + i10); if (file.createNewFile()) { return file; } } return null; } public static ByteBuffer f(Context context, CancellationSignal cancellationSignal, Uri uri) { try { ParcelFileDescriptor openFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r", cancellationSignal); if (openFileDescriptor == null) { if (openFileDescriptor != null) { openFileDescriptor.close(); } return null; } try { FileInputStream fileInputStream = new FileInputStream(openFileDescriptor.getFileDescriptor()); try { FileChannel channel = fileInputStream.getChannel(); MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, channel.size()); fileInputStream.close(); openFileDescriptor.close(); return map; } finally { } } finally { } } catch (IOException unused) { return null; } } private static ByteBuffer g(File file) { try { FileInputStream fileInputStream = new FileInputStream(file); try { FileChannel channel = fileInputStream.getChannel(); MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, channel.size()); fileInputStream.close(); return map; } finally { } } catch (IOException unused) { return null; } } public static Map h(Context context, f.b[] bVarArr, CancellationSignal cancellationSignal) { HashMap hashMap = new HashMap(); for (f.b bVar : bVarArr) { if (bVar.b() == 0) { Uri d10 = bVar.d(); if (!hashMap.containsKey(d10)) { hashMap.put(d10, f(context, cancellationSignal, d10)); } } } return Collections.unmodifiableMap(hashMap); } }