content: package androidx.core.graphics;
  
  import android.content.ContentResolver;
  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.core.provider.FontsContractCompat;
  import java.io.Closeable;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  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;
  
  public class TypefaceCompatUtil {
      private static final String CACHE_FILE_PREFIX = ".font";
      private static final String TAG = "TypefaceCompatUtil";
  
      static class Api19Impl {
          private Api19Impl() {
          }
  
          static ParcelFileDescriptor openFileDescriptor(ContentResolver contentResolver, Uri uri, String mode, CancellationSignal cancellationSignal) throws FileNotFoundException {
              return contentResolver.openFileDescriptor(uri, mode, cancellationSignal);
          }
      }
  
      private TypefaceCompatUtil() {
      }
  
      public static void closeQuietly(Closeable c) {
          if (c != null) {
              try {
                  c.close();
              } catch (IOException e) {
              }
          }
      }
  
      public static ByteBuffer copyToDirectBuffer(Context context, Resources res, int id) {
          File tempFile = getTempFile(context);
          if (tempFile == null) {
              return null;
          }
          try {
              if (!copyToFile(tempFile, res, id)) {
                  return null;
              }
              ByteBuffer mmap = mmap(tempFile);
              tempFile.delete();
              return mmap;
          } finally {
              tempFile.delete();
          }
      }
  
      public static boolean copyToFile(File file, Resources res, int id) {
          InputStream inputStream = null;
          try {
              inputStream = res.openRawResource(id);
              return copyToFile(file, inputStream);
          } finally {
              closeQuietly(inputStream);
          }
      }
  
      public static boolean copyToFile(File file, InputStream is) {
          FileOutputStream fileOutputStream = null;
          StrictMode.ThreadPolicy allowThreadDiskWrites = StrictMode.allowThreadDiskWrites();
          try {
              fileOutputStream = new FileOutputStream(file, false);
              byte[] bArr = new byte[1024];
              while (true) {
                  int read = is.read(bArr);
                  int i = read;
                  if (read != -1) {
                      fileOutputStream.write(bArr, 0, i);
                  } else {
                      return true;
                  }
              }
          } catch (IOException e) {
              Log.e(TAG, "Error copying resource contents to temp file: " + e.getMessage());
              return false;
          } finally {
              closeQuietly(fileOutputStream);
              StrictMode.setThreadPolicy(allowThreadDiskWrites);
          }
      }
  
      public static File getTempFile(Context context) {
          File cacheDir = context.getCacheDir();
          if (cacheDir == null) {
              return null;
          }
          String str = CACHE_FILE_PREFIX + Process.myPid() + "-" + Process.myTid() + "-";
          int i = 0;
          while (i < 100) {
              File file = new File(cacheDir, str + i);
              try {
                  if (file.createNewFile()) {
                      return file;
                  }
                  i++;
              } catch (IOException e) {
              }
          }
          return null;
      }
  
      public static ByteBuffer mmap(Context context, CancellationSignal cancellationSignal, Uri uri) {
          FileInputStream fileInputStream;
          try {
              ParcelFileDescriptor openFileDescriptor = Api19Impl.openFileDescriptor(context.getContentResolver(), uri, "r", cancellationSignal);
              if (openFileDescriptor == null) {
                  if (openFileDescriptor != null) {
                      openFileDescriptor.close();
                  }
                  return null;
              }
              try {
                  fileInputStream = new FileInputStream(openFileDescriptor.getFileDescriptor());
                  FileChannel channel = fileInputStream.getChannel();
                  MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
                  fileInputStream.close();
                  if (openFileDescriptor != null) {
                      openFileDescriptor.close();
                  }
                  return map;
              } catch (Throwable th) {
                  if (openFileDescriptor != null) {
                      openFileDescriptor.close();
                  }
                  throw th;
              }
              throw th;
          } catch (IOException e) {
              return null;
          } catch (Throwable th2) {
              th.addSuppressed(th2);
          }
      }
  
      private static ByteBuffer mmap(File file) {
          FileInputStream fileInputStream;
          try {
              fileInputStream = new FileInputStream(file);
              FileChannel channel = fileInputStream.getChannel();
              MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
              fileInputStream.close();
              return map;
          } catch (IOException e) {
              return null;
          } catch (Throwable th) {
              th.addSuppressed(th);
          }
          throw th;
      }
  
      public static Map<Uri, ByteBuffer> readFontInfoIntoByteBuffer(Context context, FontsContractCompat.FontInfo[] fonts, CancellationSignal cancellationSignal) {
          HashMap hashMap = new HashMap();
          for (FontsContractCompat.FontInfo fontInfo : fonts) {
              if (fontInfo.getResultCode() == 0) {
                  Uri uri = fontInfo.getUri();
                  if (!hashMap.containsKey(uri)) {
                      hashMap.put(uri, mmap(context, cancellationSignal, uri));
                  }
              }
          }
          return Collections.unmodifiableMap(hashMap);
      }
  }
  