package c.i.a.c.h2.y; import android.content.ContentValues; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.util.SparseArray; import android.util.SparseBooleanArray; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; import c.i.a.c.i2.f; import c.i.a.c.i2.f0; import c.i.a.c.i2.y; import com.airbnb.lottie.parser.AnimatableValueParser; import com.google.android.exoplayer2.database.DatabaseIOException; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Set; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /* compiled from: CachedContentIndex */ public class j { public final HashMap a; public final SparseArray b; /* renamed from: c reason: collision with root package name */ public final SparseBooleanArray f897c; public final SparseBooleanArray d; public c e; @Nullable public c f; /* compiled from: CachedContentIndex */ public static final class a implements c { public static final String[] a = {"id", "key", "metadata"}; public final c.i.a.c.w1.a b; /* renamed from: c reason: collision with root package name */ public final SparseArray f898c = new SparseArray<>(); public String d; public String e; public a(c.i.a.c.w1.a aVar) { this.b = aVar; } @Override // c.i.a.c.h2.y.j.c public void a(i iVar, boolean z2) { if (z2) { this.f898c.delete(iVar.a); } else { this.f898c.put(iVar.a, null); } } @Override // c.i.a.c.h2.y.j.c public void b(HashMap hashMap) throws IOException { try { SQLiteDatabase writableDatabase = this.b.getWritableDatabase(); writableDatabase.beginTransactionNonExclusive(); try { j(writableDatabase); for (i iVar : hashMap.values()) { i(writableDatabase, iVar); } writableDatabase.setTransactionSuccessful(); this.f898c.clear(); } finally { writableDatabase.endTransaction(); } } catch (SQLException e) { throw new DatabaseIOException(e); } } @Override // c.i.a.c.h2.y.j.c public void c(i iVar) { this.f898c.put(iVar.a, iVar); } @Override // c.i.a.c.h2.y.j.c public boolean d() throws DatabaseIOException { SQLiteDatabase readableDatabase = this.b.getReadableDatabase(); String str = this.d; Objects.requireNonNull(str); return AnimatableValueParser.Q0(readableDatabase, 1, str) != -1; } @Override // c.i.a.c.h2.y.j.c public void e(HashMap hashMap) throws IOException { if (this.f898c.size() != 0) { try { SQLiteDatabase writableDatabase = this.b.getWritableDatabase(); writableDatabase.beginTransactionNonExclusive(); for (int i = 0; i < this.f898c.size(); i++) { try { i valueAt = this.f898c.valueAt(i); if (valueAt == null) { int keyAt = this.f898c.keyAt(i); String str = this.e; Objects.requireNonNull(str); writableDatabase.delete(str, "id = ?", new String[]{Integer.toString(keyAt)}); } else { i(writableDatabase, valueAt); } } finally { writableDatabase.endTransaction(); } } writableDatabase.setTransactionSuccessful(); this.f898c.clear(); writableDatabase.endTransaction(); } catch (SQLException e) { throw new DatabaseIOException(e); } } } @Override // c.i.a.c.h2.y.j.c public void f(long j) { String hexString = Long.toHexString(j); this.d = hexString; this.e = c.d.b.a.a.s("ExoPlayerCacheIndex", hexString); } /* JADX WARNING: Code restructure failed: missing block: B:25:0x0082, code lost: r2 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:26:0x0083, code lost: if (r0 != null) goto L_0x0085; */ /* JADX WARNING: Code restructure failed: missing block: B:28:?, code lost: r0.close(); */ /* JADX WARNING: Code restructure failed: missing block: B:29:0x0089, code lost: r0 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:30:0x008a, code lost: r1.addSuppressed(r0); */ /* JADX WARNING: Code restructure failed: missing block: B:31:0x008d, code lost: throw r2; */ @Override // c.i.a.c.h2.y.j.c public void g(HashMap hashMap, SparseArray sparseArray) throws IOException { AnimatableValueParser.D(this.f898c.size() == 0); try { SQLiteDatabase readableDatabase = this.b.getReadableDatabase(); String str = this.d; Objects.requireNonNull(str); if (AnimatableValueParser.Q0(readableDatabase, 1, str) != 1) { SQLiteDatabase writableDatabase = this.b.getWritableDatabase(); writableDatabase.beginTransactionNonExclusive(); try { j(writableDatabase); writableDatabase.setTransactionSuccessful(); } finally { writableDatabase.endTransaction(); } } SQLiteDatabase readableDatabase2 = this.b.getReadableDatabase(); String str2 = this.e; Objects.requireNonNull(str2); Cursor query = readableDatabase2.query(str2, a, null, null, null, null, null); while (query.moveToNext()) { int i = query.getInt(0); String string = query.getString(1); hashMap.put(string, new i(i, string, j.a(new DataInputStream(new ByteArrayInputStream(query.getBlob(2)))))); sparseArray.put(i, string); } query.close(); } catch (SQLiteException e) { hashMap.clear(); sparseArray.clear(); throw new DatabaseIOException(e); } } @Override // c.i.a.c.h2.y.j.c public void h() throws DatabaseIOException { c.i.a.c.w1.a aVar = this.b; String str = this.d; Objects.requireNonNull(str); try { String str2 = "ExoPlayerCacheIndex" + str; SQLiteDatabase writableDatabase = aVar.getWritableDatabase(); writableDatabase.beginTransactionNonExclusive(); try { if (f0.I(writableDatabase, "ExoPlayerVersions")) { writableDatabase.delete("ExoPlayerVersions", "feature = ? AND instance_uid = ?", new String[]{Integer.toString(1), str}); } writableDatabase.execSQL("DROP TABLE IF EXISTS " + str2); writableDatabase.setTransactionSuccessful(); writableDatabase.endTransaction(); } catch (SQLException e) { throw new DatabaseIOException(e); } } catch (SQLException e2) { throw new DatabaseIOException(e2); } } public final void i(SQLiteDatabase sQLiteDatabase, i iVar) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); j.b(iVar.e, new DataOutputStream(byteArrayOutputStream)); byte[] byteArray = byteArrayOutputStream.toByteArray(); ContentValues contentValues = new ContentValues(); contentValues.put("id", Integer.valueOf(iVar.a)); contentValues.put("key", iVar.b); contentValues.put("metadata", byteArray); String str = this.e; Objects.requireNonNull(str); sQLiteDatabase.replaceOrThrow(str, null, contentValues); } public final void j(SQLiteDatabase sQLiteDatabase) throws DatabaseIOException { String str = this.d; Objects.requireNonNull(str); AnimatableValueParser.A2(sQLiteDatabase, 1, str, 1); String str2 = this.e; Objects.requireNonNull(str2); sQLiteDatabase.execSQL("DROP TABLE IF EXISTS " + str2); sQLiteDatabase.execSQL("CREATE TABLE " + this.e + " (id INTEGER PRIMARY KEY NOT NULL,key TEXT NOT NULL,metadata BLOB NOT NULL)"); } } /* compiled from: CachedContentIndex */ public static class b implements c { public final boolean a; @Nullable public final Cipher b; @Nullable /* renamed from: c reason: collision with root package name */ public final SecretKeySpec f899c; @Nullable public final SecureRandom d; public final f e; public boolean f; @Nullable public y g; public b(File file, @Nullable byte[] bArr, boolean z2) { SecretKeySpec secretKeySpec; Cipher cipher; boolean z3 = false; AnimatableValueParser.D(bArr != null || !z2); SecureRandom secureRandom = null; if (bArr != null) { AnimatableValueParser.k(bArr.length == 16 ? true : z3); try { if (f0.a == 18) { try { cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING", "BC"); } catch (Throwable unused) { } secretKeySpec = new SecretKeySpec(bArr, "AES"); } cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); secretKeySpec = new SecretKeySpec(bArr, "AES"); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new IllegalStateException(e); } } else { AnimatableValueParser.k(!z2); cipher = null; secretKeySpec = null; } this.a = z2; this.b = cipher; this.f899c = secretKeySpec; this.d = z2 ? new SecureRandom() : secureRandom; this.e = new f(file); } @Override // c.i.a.c.h2.y.j.c public void a(i iVar, boolean z2) { this.f = true; } /* JADX WARNING: Removed duplicated region for block: B:33:0x00b3 A[SYNTHETIC, Splitter:B:33:0x00b3] */ @Override // c.i.a.c.h2.y.j.c public void b(HashMap hashMap) throws IOException { Throwable th; DataOutputStream dataOutputStream = null; try { OutputStream c2 = this.e.c(); y yVar = this.g; if (yVar == null) { this.g = new y(c2); } else { yVar.a(c2); } y yVar2 = this.g; DataOutputStream dataOutputStream2 = new DataOutputStream(yVar2); try { dataOutputStream2.writeInt(2); dataOutputStream2.writeInt(this.a ? 1 : 0); if (this.a) { byte[] bArr = new byte[16]; SecureRandom secureRandom = this.d; int i = f0.a; secureRandom.nextBytes(bArr); dataOutputStream2.write(bArr); try { this.b.init(1, this.f899c, new IvParameterSpec(bArr)); dataOutputStream2.flush(); dataOutputStream2 = new DataOutputStream(new CipherOutputStream(yVar2, this.b)); } catch (InvalidAlgorithmParameterException | InvalidKeyException e) { throw new IllegalStateException(e); } } dataOutputStream2.writeInt(hashMap.size()); int i2 = 0; for (i iVar : hashMap.values()) { dataOutputStream2.writeInt(iVar.a); dataOutputStream2.writeUTF(iVar.b); j.b(iVar.e, dataOutputStream2); i2 += i(iVar, 2); } dataOutputStream2.writeInt(i2); f fVar = this.e; Objects.requireNonNull(fVar); dataOutputStream2.close(); fVar.b.delete(); int i3 = f0.a; this.f = false; } catch (Throwable th2) { th = th2; dataOutputStream = dataOutputStream2; int i4 = f0.a; if (dataOutputStream != null) { try { dataOutputStream.close(); } catch (IOException unused) { } } throw th; } } catch (Throwable th3) { th = th3; int i4 = f0.a; if (dataOutputStream != null) { } throw th; } } @Override // c.i.a.c.h2.y.j.c public void c(i iVar) { this.f = true; } @Override // c.i.a.c.h2.y.j.c public boolean d() { return this.e.a(); } @Override // c.i.a.c.h2.y.j.c public void e(HashMap hashMap) throws IOException { if (this.f) { b(hashMap); } } @Override // c.i.a.c.h2.y.j.c public void f(long j) { } /* JADX WARNING: Removed duplicated region for block: B:48:0x00b6 */ /* JADX WARNING: Removed duplicated region for block: B:55:0x00bf */ /* JADX WARNING: Removed duplicated region for block: B:59:0x00c7 */ /* JADX WARNING: Removed duplicated region for block: B:62:? A[RETURN, SYNTHETIC] */ @Override // c.i.a.c.h2.y.j.c public void g(HashMap hashMap, SparseArray sparseArray) { Throwable th; boolean z2 = true; AnimatableValueParser.D(!this.f); if (this.e.a()) { DataInputStream dataInputStream = null; try { BufferedInputStream bufferedInputStream = new BufferedInputStream(this.e.b()); DataInputStream dataInputStream2 = new DataInputStream(bufferedInputStream); try { int readInt = dataInputStream2.readInt(); if (readInt >= 0) { if (readInt <= 2) { if ((dataInputStream2.readInt() & 1) != 0) { if (this.b != null) { byte[] bArr = new byte[16]; dataInputStream2.readFully(bArr); IvParameterSpec ivParameterSpec = new IvParameterSpec(bArr); try { Cipher cipher = this.b; SecretKeySpec secretKeySpec = this.f899c; int i = f0.a; cipher.init(2, secretKeySpec, ivParameterSpec); dataInputStream2 = new DataInputStream(new CipherInputStream(bufferedInputStream, this.b)); } catch (InvalidAlgorithmParameterException | InvalidKeyException e) { throw new IllegalStateException(e); } } } else if (this.a) { this.f = true; } int readInt2 = dataInputStream2.readInt(); int i2 = 0; for (int i3 = 0; i3 < readInt2; i3++) { i j = j(readInt, dataInputStream2); hashMap.put(j.b, j); sparseArray.put(j.a, j.b); i2 += i(j, readInt); } int readInt3 = dataInputStream2.readInt(); boolean z3 = dataInputStream2.read() == -1; if (readInt3 == i2 && z3) { int i4 = f0.a; try { dataInputStream2.close(); } catch (IOException unused) { } } } } int i5 = f0.a; try { dataInputStream2.close(); } catch (IOException unused2) { } } catch (IOException unused3) { dataInputStream = dataInputStream2; if (dataInputStream != null) { } z2 = false; if (!z2) { } } catch (Throwable th2) { th = th2; dataInputStream = dataInputStream2; if (dataInputStream != null) { } throw th; } } catch (IOException unused4) { if (dataInputStream != null) { int i6 = f0.a; dataInputStream.close(); } z2 = false; if (!z2) { } } catch (Throwable th3) { th = th3; if (dataInputStream != null) { int i7 = f0.a; try { dataInputStream.close(); } catch (IOException unused5) { } } throw th; } z2 = false; } if (!z2) { hashMap.clear(); sparseArray.clear(); f fVar = this.e; fVar.a.delete(); fVar.b.delete(); } } @Override // c.i.a.c.h2.y.j.c public void h() { f fVar = this.e; fVar.a.delete(); fVar.b.delete(); } public final int i(i iVar, int i) { int hashCode = iVar.b.hashCode() + (iVar.a * 31); if (i >= 2) { return (hashCode * 31) + iVar.e.hashCode(); } long a = k.a(iVar.e); return (hashCode * 31) + ((int) (a ^ (a >>> 32))); } public final i j(int i, DataInputStream dataInputStream) throws IOException { n nVar; int readInt = dataInputStream.readInt(); String readUTF = dataInputStream.readUTF(); if (i < 2) { long readLong = dataInputStream.readLong(); m mVar = new m(); m.a(mVar, readLong); nVar = n.a.a(mVar); } else { nVar = j.a(dataInputStream); } return new i(readInt, readUTF, nVar); } } /* compiled from: CachedContentIndex */ public interface c { void a(i iVar, boolean z2); void b(HashMap hashMap) throws IOException; void c(i iVar); boolean d() throws IOException; void e(HashMap hashMap) throws IOException; void f(long j); void g(HashMap hashMap, SparseArray sparseArray) throws IOException; void h() throws IOException; } public j(@Nullable c.i.a.c.w1.a aVar, @Nullable File file, @Nullable byte[] bArr, boolean z2, boolean z3) { AnimatableValueParser.D((aVar == null && file == null) ? false : true); this.a = new HashMap<>(); this.b = new SparseArray<>(); this.f897c = new SparseBooleanArray(); this.d = new SparseBooleanArray(); b bVar = null; a aVar2 = aVar != null ? new a(aVar) : null; bVar = file != null ? new b(new File(file, "cached_content_index.exi"), bArr, z2) : bVar; if (aVar2 == null || (bVar != null && z3)) { int i = f0.a; this.e = bVar; this.f = aVar2; return; } this.e = aVar2; this.f = bVar; } public static n a(DataInputStream dataInputStream) throws IOException { int readInt = dataInputStream.readInt(); HashMap hashMap = new HashMap(); for (int i = 0; i < readInt; i++) { String readUTF = dataInputStream.readUTF(); int readInt2 = dataInputStream.readInt(); if (readInt2 >= 0) { int min = Math.min(readInt2, 10485760); byte[] bArr = f0.f; int i2 = 0; while (i2 != readInt2) { int i3 = i2 + min; bArr = Arrays.copyOf(bArr, i3); dataInputStream.readFully(bArr, i2, min); min = Math.min(readInt2 - i3, 10485760); i2 = i3; } hashMap.put(readUTF, bArr); } else { throw new IOException(c.d.b.a.a.j("Invalid value size: ", readInt2)); } } return new n(hashMap); } public static void b(n nVar, DataOutputStream dataOutputStream) throws IOException { Set> entrySet = nVar.f900c.entrySet(); dataOutputStream.writeInt(entrySet.size()); for (Map.Entry entry : entrySet) { dataOutputStream.writeUTF(entry.getKey()); byte[] value = entry.getValue(); dataOutputStream.writeInt(value.length); dataOutputStream.write(value); } } @Nullable public i c(String str) { return this.a.get(str); } public i d(String str) { i iVar = this.a.get(str); if (iVar != null) { return iVar; } SparseArray sparseArray = this.b; int size = sparseArray.size(); int i = 0; int keyAt = size == 0 ? 0 : sparseArray.keyAt(size - 1) + 1; if (keyAt < 0) { while (i < size && i == sparseArray.keyAt(i)) { i++; } keyAt = i; } i iVar2 = new i(keyAt, str, n.a); this.a.put(str, iVar2); this.b.put(keyAt, str); this.d.put(keyAt, true); this.e.c(iVar2); return iVar2; } @WorkerThread public void e(long j) throws IOException { c cVar; this.e.f(j); c cVar2 = this.f; if (cVar2 != null) { cVar2.f(j); } if (this.e.d() || (cVar = this.f) == null || !cVar.d()) { this.e.g(this.a, this.b); } else { this.f.g(this.a, this.b); this.e.b(this.a); } c cVar3 = this.f; if (cVar3 != null) { cVar3.h(); this.f = null; } } public void f(String str) { i iVar = this.a.get(str); if (iVar != null && iVar.f896c.isEmpty() && iVar.d.isEmpty()) { this.a.remove(str); int i = iVar.a; boolean z2 = this.d.get(i); this.e.a(iVar, z2); if (z2) { this.b.remove(i); this.d.delete(i); return; } this.b.put(i, null); this.f897c.put(i, true); } } @WorkerThread public void g() throws IOException { this.e.e(this.a); int size = this.f897c.size(); for (int i = 0; i < size; i++) { this.b.remove(this.f897c.keyAt(i)); } this.f897c.clear(); this.d.clear(); } }