package androidx.core.graphics; 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 androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.annotation.RestrictTo; import c.d.b.a.a; 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; @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) public class TypefaceCompatUtil { private static final String CACHE_FILE_PREFIX = ".font"; private static final String TAG = "TypefaceCompatUtil"; private TypefaceCompatUtil() { } public static void closeQuietly(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException unused) { } } } @Nullable @RequiresApi(19) public static ByteBuffer copyToDirectBuffer(Context context, Resources resources, int i) { File tempFile = getTempFile(context); if (tempFile == null) { return null; } try { if (!copyToFile(tempFile, resources, i)) { return null; } ByteBuffer mmap = mmap(tempFile); tempFile.delete(); return mmap; } finally { tempFile.delete(); } } public static boolean copyToFile(File file, Resources resources, int i) { InputStream inputStream; Throwable th; try { inputStream = resources.openRawResource(i); try { boolean copyToFile = copyToFile(file, inputStream); closeQuietly(inputStream); return copyToFile; } catch (Throwable th2) { th = th2; closeQuietly(inputStream); throw th; } } catch (Throwable th3) { th = th3; inputStream = null; closeQuietly(inputStream); throw th; } } public static boolean copyToFile(File file, InputStream inputStream) { Throwable th; IOException e; StrictMode.ThreadPolicy allowThreadDiskWrites = StrictMode.allowThreadDiskWrites(); FileOutputStream fileOutputStream = null; try { FileOutputStream fileOutputStream2 = new FileOutputStream(file, false); try { byte[] bArr = new byte[1024]; while (true) { int read = inputStream.read(bArr); if (read != -1) { fileOutputStream2.write(bArr, 0, read); } else { closeQuietly(fileOutputStream2); StrictMode.setThreadPolicy(allowThreadDiskWrites); return true; } } } catch (IOException e2) { e = e2; fileOutputStream = fileOutputStream2; try { Log.e("TypefaceCompatUtil", "Error copying resource contents to temp file: " + e.getMessage()); closeQuietly(fileOutputStream); StrictMode.setThreadPolicy(allowThreadDiskWrites); return false; } catch (Throwable th2) { th = th2; closeQuietly(fileOutputStream); StrictMode.setThreadPolicy(allowThreadDiskWrites); throw th; } } catch (Throwable th3) { th = th3; fileOutputStream = fileOutputStream2; closeQuietly(fileOutputStream); StrictMode.setThreadPolicy(allowThreadDiskWrites); throw th; } } catch (IOException e3) { e = e3; Log.e("TypefaceCompatUtil", "Error copying resource contents to temp file: " + e.getMessage()); closeQuietly(fileOutputStream); StrictMode.setThreadPolicy(allowThreadDiskWrites); return false; } } @Nullable public static File getTempFile(Context context) { File cacheDir = context.getCacheDir(); if (cacheDir == null) { return null; } StringBuilder L = a.L(".font"); L.append(Process.myPid()); L.append("-"); L.append(Process.myTid()); L.append("-"); String sb = L.toString(); for (int i = 0; i < 100; i++) { File file = new File(cacheDir, a.k(sb, i)); try { if (file.createNewFile()) { return file; } } catch (IOException unused) { } } return null; } @Nullable @RequiresApi(19) public static ByteBuffer mmap(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, 0, channel.size()); fileInputStream.close(); openFileDescriptor.close(); return map; } catch (Throwable th) { th.addSuppressed(th); } } catch (Throwable th2) { th.addSuppressed(th2); } throw th; throw th; } catch (IOException unused) { return null; } } @Nullable @RequiresApi(19) private static ByteBuffer mmap(File file) { try { FileInputStream fileInputStream = new FileInputStream(file); try { FileChannel channel = fileInputStream.getChannel(); MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); fileInputStream.close(); return map; } catch (Throwable th) { th.addSuppressed(th); } throw th; } catch (IOException unused) { return null; } } }