package com.kofax.mobile.sdk.j; import android.content.Context; import android.graphics.Bitmap; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import o.C15191gjl; import o.InterfaceC13391fZD; /* loaded from: classes3.dex */ public class d implements com.kofax.mobile.sdk._internal.c { private static final long zF = 20971520; private static final String zG = "BitmapDiskCache"; private final File zH; private final Object zI = new Object(); @InterfaceC13391fZD public d(Context context) { if (context == null) { throw new IllegalStateException("Context has not been set."); } StringBuilder sb = new StringBuilder(); sb.append(context.getCacheDir().getAbsolutePath()); sb.append(File.separator); sb.append(zG); File file = new File(sb.toString()); this.zH = file; if (!file.exists()) { file.mkdirs(); } trimToSize(); } @Override // com.kofax.mobile.sdk._internal.c public boolean a(String str, Bitmap bitmap) { Throwable th; FileOutputStream fileOutputStream; synchronized (this.zI) { File file = new File(this.zH, str); FileOutputStream fileOutputStream2 = null; try { try { fileOutputStream = new FileOutputStream(file); } catch (Throwable th2) { th = th2; fileOutputStream = null; } } catch (IOException unused) { } try { a(bitmap, fileOutputStream); try { fileOutputStream.close(); } catch (IOException unused2) { } trimToSize(); return true; } catch (IOException unused3) { fileOutputStream2 = fileOutputStream; if (file.exists()) { file.delete(); } if (fileOutputStream2 != null) { try { fileOutputStream2.close(); } catch (IOException unused4) { } } return false; } catch (Throwable th3) { th = th3; if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException unused5) { } } throw th; } } } @Override // com.kofax.mobile.sdk._internal.c public Bitmap getBitmap(String str) { Throwable th; FileInputStream fileInputStream; Throwable e; synchronized (this.zI) { File file = new File(this.zH, str); try { if (!file.exists()) { return null; } try { fileInputStream = new FileInputStream(file); try { Bitmap a2 = a(new FileInputStream(file)); try { fileInputStream.close(); } catch (IOException unused) { } return a2; } catch (IOException e2) { e = e2; throw new RuntimeException("Could not read bitmap file.", e); } catch (ClassNotFoundException e3) { e = e3; throw new RuntimeException("Could not read bitmap file.", e); } catch (Throwable th2) { th = th2; if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException unused2) { } } throw th; } } catch (IOException | ClassNotFoundException e4) { e = e4; } } catch (Throwable th3) { th = th3; fileInputStream = null; } } } @Override // com.kofax.mobile.sdk._internal.c public Bitmap w(String str) { Bitmap bitmap = getBitmap(str); synchronized (this.zI) { c(new File(this.zH, str)); } return bitmap; } private void c(File file) { synchronized (this.zI) { if (file != null) { if (file.exists() && !file.delete()) { StringBuilder sb = new StringBuilder("Could not delete bitmap file "); sb.append(file); throw new RuntimeException(sb.toString()); } } } } private void trimToSize() { List iZ = iZ(); while (getSize() > zF) { synchronized (this.zI) { File remove = iZ.remove(0); if (remove == null) { return; } else { c(remove); } } } } private long getSize() { return C15191gjl.c(this.zH); } private List iZ() { ArrayList arrayList = new ArrayList(Arrays.asList(this.zH.listFiles())); k(arrayList); return arrayList; } private void k(List list) { Collections.sort(list, new Comparator(this) { // from class: com.kofax.mobile.sdk.j.d.1 final d zJ; { this.zJ = this; } @Override // java.util.Comparator public int compare(File file, File file2) { if (file.lastModified() > file2.lastModified()) { return 1; } return file.lastModified() < file2.lastModified() ? -1 : 0; } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes3.dex */ public static class a implements Serializable { private static final long serialVersionUID = 6879914159857742532L; int height; int width; byte[] zK; Bitmap.Config zL; private a() { } } /* JADX WARN: Multi-variable type inference failed */ private void a(Bitmap bitmap, OutputStream outputStream) throws IOException { OutputStream outputStream2 = null; Object[] objArr = 0; try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); try { a aVar = new a(); aVar.zK = com.kofax.mobile.sdk.ak.a.n(bitmap); aVar.zL = bitmap.getConfig(); aVar.width = bitmap.getWidth(); aVar.height = bitmap.getHeight(); objectOutputStream.writeObject(aVar); objectOutputStream.close(); } catch (Throwable th) { th = th; outputStream2 = objectOutputStream; if (outputStream2 != null) { outputStream2.close(); } throw th; } } catch (Throwable th2) { th = th2; } } private Bitmap a(InputStream inputStream) throws IOException, ClassNotFoundException { if (inputStream == null) { return null; } ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); a aVar = (a) objectInputStream.readObject(); Bitmap a2 = com.kofax.mobile.sdk.ak.a.a(aVar.zK, aVar.width, aVar.height, aVar.zL); objectInputStream.close(); return a2; } }