package c.f.j.p; import android.net.Uri; import androidx.annotation.VisibleForTesting; import c.f.d.g.g; import c.f.i.c; import c.f.j.c.f; import c.f.j.c.i; import c.f.j.c.n; import c.f.j.j.e; import com.facebook.cache.common.CacheKey; import com.facebook.common.references.CloseableReference; import com.facebook.imagepipeline.memory.MemoryPooledByteBufferOutputStream; import com.facebook.imagepipeline.request.ImageRequest; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; /* compiled from: PartialDiskCacheProducer */ public class q0 implements v0 { public final f a; public final i b; /* renamed from: c reason: collision with root package name */ public final g f489c; public final c.f.d.g.a d; public final v0 e; /* compiled from: PartialDiskCacheProducer */ public static class a extends p { /* renamed from: c reason: collision with root package name */ public final f f490c; public final CacheKey d; public final g e; public final c.f.d.g.a f; public final e g; public a(l lVar, f fVar, CacheKey cacheKey, g gVar, c.f.d.g.a aVar, e eVar, o0 o0Var) { super(lVar); this.f490c = fVar; this.d = cacheKey; this.e = gVar; this.f = aVar; this.g = eVar; } @Override // c.f.j.p.b public void i(Object obj, int i) { e eVar = (e) obj; if (!b.f(i)) { e eVar2 = this.g; if (eVar2 == null || eVar.r == null) { if (b.m(i, 8) && b.e(i)) { eVar.w(); if (eVar.k != c.a) { this.f490c.f(this.d, eVar); this.b.b(eVar, i); return; } } this.b.b(eVar, i); return; } try { p(o(eVar2, eVar)); } catch (IOException e) { c.f.d.e.a.f("PartialDiskCacheProducer", "Error while merging image data", e); this.b.c(e); } catch (Throwable th) { eVar.close(); this.g.close(); throw th; } eVar.close(); this.g.close(); f fVar = this.f490c; CacheKey cacheKey = this.d; Objects.requireNonNull(fVar); Objects.requireNonNull(cacheKey); fVar.f.c(cacheKey); try { z.g.a(new c.f.j.c.g(fVar, null, cacheKey), fVar.e); } catch (Exception e2) { c.f.d.e.a.n(f.class, e2, "Failed to schedule disk-cache remove for %s", cacheKey.b()); z.g.c(e2); } } } public final void n(InputStream inputStream, OutputStream outputStream, int i) throws IOException { byte[] bArr = this.f.get(16384); int i2 = i; while (i2 > 0) { try { int read = inputStream.read(bArr, 0, Math.min(16384, i2)); if (read < 0) { break; } else if (read > 0) { outputStream.write(bArr, 0, read); i2 -= read; } } catch (Throwable th) { this.f.release(bArr); throw th; } } this.f.release(bArr); if (i2 > 0) { throw new IOException(String.format(null, "Failed to read %d bytes - finished %d short", Integer.valueOf(i), Integer.valueOf(i2))); } } public final c.f.d.g.i o(e eVar, e eVar2) throws IOException { c.f.d.g.i e = this.e.e(eVar2.f() + eVar2.r.a); n(eVar.e(), e, eVar2.r.a); n(eVar2.e(), e, eVar2.f()); return e; } /* JADX WARNING: Removed duplicated region for block: B:12:0x002a */ /* JADX WARNING: Removed duplicated region for block: B:14:0x002f */ public final void p(c.f.d.g.i iVar) { e eVar; Throwable th; CloseableReference C = CloseableReference.C(((MemoryPooledByteBufferOutputStream) iVar).b()); try { eVar = new e(C); try { eVar.p(); this.b.b(eVar, 1); eVar.close(); if (C != null) { C.close(); } } catch (Throwable th2) { th = th2; if (eVar != null) { eVar.close(); } if (C != null) { C.close(); } throw th; } } catch (Throwable th3) { eVar = null; th = th3; if (eVar != null) { } if (C != null) { } throw th; } } } public q0(f fVar, i iVar, g gVar, c.f.d.g.a aVar, v0 v0Var) { this.a = fVar; this.b = iVar; this.f489c = gVar; this.d = aVar; this.e = v0Var; } public static void c(q0 q0Var, l lVar, w0 w0Var, CacheKey cacheKey, e eVar) { q0Var.e.b(new a(lVar, q0Var.a, cacheKey, q0Var.f489c, q0Var.d, eVar, null), w0Var); } @VisibleForTesting public static Map d(y0 y0Var, w0 w0Var, boolean z2, int i) { if (!y0Var.g(w0Var, "PartialDiskCacheProducer")) { return null; } return z2 ? c.f.d.d.f.of("cached_value_found", String.valueOf(z2), "encodedImageSize", String.valueOf(i)) : c.f.d.d.f.of("cached_value_found", String.valueOf(z2)); } @Override // c.f.j.p.v0 public void b(l lVar, w0 w0Var) { ImageRequest e = w0Var.e(); if (!e.n) { this.e.b(lVar, w0Var); return; } w0Var.o().e(w0Var, "PartialDiskCacheProducer"); Uri build = e.f2182c.buildUpon().appendQueryParameter("fresco_partial", "true").build(); i iVar = this.b; w0Var.b(); Objects.requireNonNull((n) iVar); c.f.b.a.f fVar = new c.f.b.a.f(build.toString()); AtomicBoolean atomicBoolean = new AtomicBoolean(false); this.a.e(fVar, atomicBoolean).b(new o0(this, w0Var.o(), w0Var, lVar, fVar)); w0Var.f(new p0(this, atomicBoolean)); } }