package com.baidu.cloud.videocache; import com.ubt.jimu.unity.bluetooth.UnityActivity; import java.io.BufferedOutputStream; import java.io.OutputStream; import java.net.Socket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /* loaded from: classes.dex */ public class d extends f { private static final Logger o = LoggerFactory.getLogger("HlsProxyCache"); private e m; private b n; public d(e eVar, com.baidu.cloud.videocache.file.a aVar) { super(eVar, aVar); this.m = eVar; } private void f() { long j; Throwable th; long j2 = -1; try { this.m.a(0L); j2 = this.m.length(); byte[] bArr = new byte[UnityActivity.BLOCKLY_TYPE_NONE]; byte[] bArr2 = new byte[(int) this.m.length()]; j = 0; int i = 0; while (true) { try { int read = this.m.read(bArr); if (read != -1) { synchronized (this.d) { if (d()) { return; } System.arraycopy(bArr, 0, bArr2, i, read); i += read; } j += read; a(j, j2); } else { if (!d() && i == this.m.length()) { byte[] a = c.a(bArr2, this.m.b()); this.k.a(a, a.length); this.m.b(a.length); } c(); b(); o.debug("Read hls file to cache. available is" + this.k.available()); } } catch (Throwable th2) { th = th2; try { this.e.incrementAndGet(); a(th); return; } finally { e(); a(j, j2); } } } } catch (Throwable th3) { j = 0; th = th3; } } @Override // com.baidu.cloud.videocache.f public void a(b bVar, Socket socket) { BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream()); this.n = bVar; o.info("Hls request: {\n" + bVar + "}"); long j = bVar.b; if (a(bVar)) { a(bufferedOutputStream, j); } else { b(bufferedOutputStream, j); } } @Override // com.baidu.cloud.videocache.f protected void a(OutputStream outputStream, long j) { if (!this.k.b() && !d()) { f(); } if (this.k.b() && this.h != 100) { this.h = 100; a(100); } byte[] bArr = new byte[UnityActivity.BLOCKLY_TYPE_NONE]; outputStream.write(b(this.n).getBytes("UTF-8")); while (true) { int a = this.k.a(bArr, j, bArr.length); if (a == -1) { outputStream.flush(); return; } else { outputStream.write(bArr, 0, a); j += a; } } } @Override // com.baidu.cloud.videocache.f protected void b(OutputStream outputStream, long j) { e eVar = new e(this.m); try { eVar.a((int) j); byte[] bArr = new byte[UnityActivity.BLOCKLY_TYPE_NONE]; byte[] bArr2 = new byte[(int) this.m.length()]; int i = 0; while (true) { int read = eVar.read(bArr); if (read == -1) { byte[] a = c.a(bArr2, this.m.b()); this.m.b(a.length); outputStream.write(b(this.n).getBytes("UTF-8")); outputStream.write(a, 0, a.length); outputStream.flush(); o.info("Read from source bytes " + i); return; } System.arraycopy(bArr, 0, bArr2, i, read); i += read; } } finally { eVar.close(); } } }