package com.google.android.play.core.assetpacks; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.util.Properties; /* loaded from: classes2.dex */ final class b3 { /* renamed from: h, reason: collision with root package name */ private static final r3.h0 f4802h = new r3.h0("SliceMetadataManager"); /* renamed from: b, reason: collision with root package name */ private final e0 f4804b; /* renamed from: c, reason: collision with root package name */ private final String f4805c; /* renamed from: d, reason: collision with root package name */ private final int f4806d; /* renamed from: e, reason: collision with root package name */ private final long f4807e; /* renamed from: f, reason: collision with root package name */ private final String f4808f; /* renamed from: a, reason: collision with root package name */ private final byte[] f4803a = new byte[8192]; /* renamed from: g, reason: collision with root package name */ private int f4809g = -1; b3(e0 e0Var, String str, int i10, long j10, String str2) { this.f4804b = e0Var; this.f4805c = str; this.f4806d = i10; this.f4807e = j10; this.f4808f = str2; } private final File n() { File A = this.f4804b.A(this.f4805c, this.f4806d, this.f4807e, this.f4808f); if (!A.exists()) { A.mkdirs(); } return A; } private final File o() { File z10 = this.f4804b.z(this.f4805c, this.f4806d, this.f4807e, this.f4808f); z10.getParentFile().mkdirs(); z10.createNewFile(); return z10; } final int a() { File z10 = this.f4804b.z(this.f4805c, this.f4806d, this.f4807e, this.f4808f); if (!z10.exists()) { return 0; } FileInputStream fileInputStream = new FileInputStream(z10); try { Properties properties = new Properties(); properties.load(fileInputStream); fileInputStream.close(); if (Integer.parseInt(properties.getProperty("fileStatus", "-1")) == 4) { return -1; } if (properties.getProperty("previousChunk") != null) { return Integer.parseInt(properties.getProperty("previousChunk")) + 1; } throw new d1("Slice checkpoint file corrupt."); } catch (Throwable th) { try { fileInputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final a3 b() { File z10 = this.f4804b.z(this.f4805c, this.f4806d, this.f4807e, this.f4808f); if (!z10.exists()) { throw new d1("Slice checkpoint file does not exist."); } Properties properties = new Properties(); FileInputStream fileInputStream = new FileInputStream(z10); try { properties.load(fileInputStream); fileInputStream.close(); if (properties.getProperty("fileStatus") == null || properties.getProperty("previousChunk") == null) { throw new d1("Slice checkpoint file corrupt."); } try { int parseInt = Integer.parseInt(properties.getProperty("fileStatus")); String property = properties.getProperty("fileName"); long parseLong = Long.parseLong(properties.getProperty("fileOffset", "-1")); long parseLong2 = Long.parseLong(properties.getProperty("remainingBytes", "-1")); int parseInt2 = Integer.parseInt(properties.getProperty("previousChunk")); this.f4809g = Integer.parseInt(properties.getProperty("metadataFileCounter", "0")); return new l0(parseInt, property, parseLong, parseLong2, parseInt2); } catch (NumberFormatException e10) { throw new d1("Slice checkpoint file corrupt.", e10); } } catch (Throwable th) { try { fileInputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final File c() { return new File(n(), String.format("%s-NAM.dat", Integer.valueOf(this.f4809g))); } final void d(InputStream inputStream, long j10) { int read; RandomAccessFile randomAccessFile = new RandomAccessFile(c(), "rw"); try { randomAccessFile.seek(j10); do { read = inputStream.read(this.f4803a); if (read > 0) { randomAccessFile.write(this.f4803a, 0, read); } } while (read >= 0); randomAccessFile.close(); } catch (Throwable th) { try { randomAccessFile.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final void e(long j10, byte[] bArr, int i10, int i11) { RandomAccessFile randomAccessFile = new RandomAccessFile(c(), "rw"); try { randomAccessFile.seek(j10); randomAccessFile.write(bArr, i10, i11); randomAccessFile.close(); } catch (Throwable th) { try { randomAccessFile.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final void f(int i10) { Properties properties = new Properties(); properties.put("fileStatus", "3"); properties.put("fileOffset", String.valueOf(c().length())); properties.put("previousChunk", String.valueOf(i10)); properties.put("metadataFileCounter", String.valueOf(this.f4809g)); FileOutputStream fileOutputStream = new FileOutputStream(o()); try { properties.store(fileOutputStream, (String) null); fileOutputStream.close(); } catch (Throwable th) { try { fileOutputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final void g(String str, long j10, long j11, int i10) { Properties properties = new Properties(); properties.put("fileStatus", "1"); properties.put("fileName", str); properties.put("fileOffset", String.valueOf(j10)); properties.put("remainingBytes", String.valueOf(j11)); properties.put("previousChunk", String.valueOf(i10)); properties.put("metadataFileCounter", String.valueOf(this.f4809g)); FileOutputStream fileOutputStream = new FileOutputStream(o()); try { properties.store(fileOutputStream, (String) null); fileOutputStream.close(); } catch (Throwable th) { try { fileOutputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final void h(byte[] bArr, int i10) { Properties properties = new Properties(); properties.put("fileStatus", "2"); properties.put("previousChunk", String.valueOf(i10)); properties.put("metadataFileCounter", String.valueOf(this.f4809g)); FileOutputStream fileOutputStream = new FileOutputStream(o()); try { properties.store(fileOutputStream, (String) null); fileOutputStream.close(); File y10 = this.f4804b.y(this.f4805c, this.f4806d, this.f4807e, this.f4808f); if (y10.exists()) { y10.delete(); } fileOutputStream = new FileOutputStream(y10); try { fileOutputStream.write(bArr); fileOutputStream.close(); } finally { try { fileOutputStream.close(); } catch (Throwable th) { th.addSuppressed(th); } } } catch (Throwable th2) { throw th2; } } final void i(int i10) { Properties properties = new Properties(); properties.put("fileStatus", "4"); properties.put("previousChunk", String.valueOf(i10)); properties.put("metadataFileCounter", String.valueOf(this.f4809g)); FileOutputStream fileOutputStream = new FileOutputStream(o()); try { properties.store(fileOutputStream, (String) null); fileOutputStream.close(); } catch (Throwable th) { try { fileOutputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final void j(byte[] bArr) { this.f4809g++; try { FileOutputStream fileOutputStream = new FileOutputStream(new File(n(), String.format("%s-LFH.dat", Integer.valueOf(this.f4809g)))); try { fileOutputStream.write(bArr); fileOutputStream.close(); } finally { } } catch (IOException e10) { throw new d1("Could not write metadata file.", e10); } } final void k(byte[] bArr, InputStream inputStream) { this.f4809g++; FileOutputStream fileOutputStream = new FileOutputStream(c()); try { fileOutputStream.write(bArr); int read = inputStream.read(this.f4803a); while (read > 0) { fileOutputStream.write(this.f4803a, 0, read); read = inputStream.read(this.f4803a); } fileOutputStream.close(); } catch (Throwable th) { try { fileOutputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final void l(byte[] bArr, int i10, int i11) { this.f4809g++; FileOutputStream fileOutputStream = new FileOutputStream(c()); try { fileOutputStream.write(bArr, 0, i11); fileOutputStream.close(); } catch (Throwable th) { try { fileOutputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } final boolean m() { File z10 = this.f4804b.z(this.f4805c, this.f4806d, this.f4807e, this.f4808f); if (!z10.exists()) { return false; } try { FileInputStream fileInputStream = new FileInputStream(z10); try { Properties properties = new Properties(); properties.load(fileInputStream); fileInputStream.close(); if (properties.getProperty("fileStatus") != null) { return Integer.parseInt(properties.getProperty("fileStatus")) == 4; } f4802h.b("Slice checkpoint file corrupt while checking if extraction finished.", new Object[0]); return false; } finally { } } catch (IOException e10) { f4802h.b("Could not read checkpoint while checking if extraction finished. %s", e10); return false; } } }