package com.baidu.cloud.videocache; import com.ubt.jimu.unity.bluetooth.UnityActivity; import java.lang.Thread; import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /* loaded from: classes.dex */ class u { private static final Logger i = LoggerFactory.getLogger("ProxyCache"); private final Source a; private final Cache b; protected final AtomicInteger e; private volatile Thread f; private volatile boolean g; private final Object c = new Object(); protected final Object d = new Object(); protected volatile int h = -1; public u(Source source, Cache cache) { t.a(source); this.a = source; t.a(cache); this.b = cache; this.e = new AtomicInteger(); } private void f() { int i2 = this.e.get(); if (i2 < 1) { return; } this.e.set(0); throw new x("Error reading source " + i2 + " times"); } private synchronized void g() { boolean z = (this.f == null || this.f.getState() == Thread.State.TERMINATED) ? false : true; if (!this.g && !this.b.b() && !z) { this.f = new Thread(new w(this), "Source reader for " + this.a); this.f.start(); } } private void h() { synchronized (this.c) { try { try { this.c.wait(1000L); } catch (InterruptedException e) { throw new x("Waiting source data is interrupted!", e); } } catch (Throwable th) { throw th; } } } /* JADX INFO: Access modifiers changed from: private */ public void i() { long j = -1; long j2 = 0; try { j2 = this.b.available(); this.a.a(j2); j = this.a.length(); byte[] bArr = new byte[UnityActivity.BLOCKLY_TYPE_NONE]; while (true) { int read = this.a.read(bArr); if (read == -1) { c(); b(); break; } synchronized (this.d) { if (d()) { return; } else { this.b.a(bArr, read); } } j2 += read; a(j2, j); } } finally { try { } finally { } } } public int a(byte[] bArr, long j, int i2) { z.a(bArr, j, i2); while (!this.b.b() && this.b.available() < i2 + j && !this.g) { g(); h(); f(); } int a = this.b.a(bArr, j, i2); if (this.b.b() && this.h != 100) { this.h = 100; a(100); } return a; } public void a() { synchronized (this.d) { i.debug("Shutdown proxy for " + this.a); try { this.g = true; if (this.f != null) { this.f.interrupt(); } this.b.close(); } catch (x e) { a(e); } } } protected void a(int i2) { throw null; } protected void a(long j, long j2) { b(j, j2); synchronized (this.c) { this.c.notifyAll(); } } protected final void a(Throwable th) { if (th instanceof p) { i.debug("ProxyCache is interrupted"); } else { i.error("ProxyCache error", th); } } protected void b() { this.h = 100; a(this.h); } protected void b(long j, long j2) { int i2 = (j2 > 0L ? 1 : (j2 == 0L ? 0 : -1)) == 0 ? 100 : (int) ((j / j2) * 100.0f); boolean z = i2 != this.h; if ((j2 >= 0) && z) { a(i2); } this.h = i2; } protected void c() { synchronized (this.d) { if (!d() && this.b.available() == this.a.length()) { this.b.a(); } } } protected boolean d() { return Thread.currentThread().isInterrupted() || this.g; } protected void e() { try { this.a.close(); } catch (x e) { a(new x("Error closing source " + this.a, e)); } } }