package com.danikula.videocache; import android.text.TextUtils; import com.lzy.okgo.model.HttpHeaders; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; /* compiled from: HttpUrlSource.java */ /* loaded from: classes.dex */ public class k implements q { /* renamed from: f reason: collision with root package name */ private static final int f994f = 5; private final com.danikula.videocache.v.c a; /* renamed from: b reason: collision with root package name */ private final com.danikula.videocache.u.b f995b; /* renamed from: c reason: collision with root package name */ private r f996c; /* renamed from: d reason: collision with root package name */ private HttpURLConnection f997d; /* renamed from: e reason: collision with root package name */ private InputStream f998e; public k(String str) { this(str, com.danikula.videocache.v.d.a()); } /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Removed duplicated region for block: B:23:0x0060 */ /* JADX WARN: Type inference failed for: r0v0 */ /* JADX WARN: Type inference failed for: r0v1 */ /* JADX WARN: Type inference failed for: r0v3, types: [java.net.HttpURLConnection] */ /* JADX WARN: Type inference failed for: r0v4 */ /* JADX WARN: Type inference failed for: r0v6, types: [java.net.HttpURLConnection] */ /* JADX WARN: Type inference failed for: r0v8 */ /* JADX WARN: Type inference failed for: r0v9 */ /* Code decompiled incorrectly, please refer to instructions dump. */ private void c() throws ProxyCacheException { HttpURLConnection httpURLConnection; ?? r0 = 0; InputStream inputStream = null; try { try { httpURLConnection = a(0L, 10000); try { long a = a(httpURLConnection); String contentType = httpURLConnection.getContentType(); inputStream = httpURLConnection.getInputStream(); this.f996c = new r(this.f996c.a, a, contentType); this.a.a(this.f996c.a, this.f996c); p.a(inputStream); r0 = httpURLConnection; if (httpURLConnection == null) { return; } } catch (IOException e2) { e = e2; h.a("Error fetching info from " + this.f996c.a, e); p.a(inputStream); r0 = httpURLConnection; if (httpURLConnection == null) { return; } r0.disconnect(); } } catch (Throwable th) { th = th; p.a(inputStream); if (r0 != 0) { r0.disconnect(); } throw th; } } catch (IOException e3) { e = e3; httpURLConnection = null; } catch (Throwable th2) { th = th2; r0 = 0; p.a(inputStream); if (r0 != 0) { } throw th; } r0.disconnect(); } @Override // com.danikula.videocache.q public void a(long j) throws ProxyCacheException { try { this.f997d = a(j, -1); String contentType = this.f997d.getContentType(); this.f998e = new BufferedInputStream(this.f997d.getInputStream(), 8192); this.f996c = new r(this.f996c.a, a(this.f997d, j, this.f997d.getResponseCode()), contentType); this.a.a(this.f996c.a, this.f996c); } catch (IOException e2) { throw new ProxyCacheException("Error opening connection for " + this.f996c.a + " with offset " + j, e2); } } public String b() { return this.f996c.a; } @Override // com.danikula.videocache.q public void close() throws ProxyCacheException { HttpURLConnection httpURLConnection = this.f997d; if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (ArrayIndexOutOfBoundsException e2) { h.a("Error closing connection correctly. Should happen only on Android L. If anybody know how to fix it, please visit https://github.com/danikula/AndroidVideoCache/issues/88. Until good solution is not know, just ignore this issue :(", e2); } catch (IllegalArgumentException e3) { e = e3; throw new RuntimeException("Wait... but why? WTF!? Really shouldn't happen any more after fixing https://github.com/danikula/AndroidVideoCache/issues/43. If you read it on your device log, please, notify me danikula@gmail.com or create issue here https://github.com/danikula/AndroidVideoCache/issues.", e); } catch (NullPointerException e4) { e = e4; throw new RuntimeException("Wait... but why? WTF!? Really shouldn't happen any more after fixing https://github.com/danikula/AndroidVideoCache/issues/43. If you read it on your device log, please, notify me danikula@gmail.com or create issue here https://github.com/danikula/AndroidVideoCache/issues.", e); } } } @Override // com.danikula.videocache.q public synchronized long length() throws ProxyCacheException { if (this.f996c.f1012b == -2147483648L) { c(); } return this.f996c.f1012b; } @Override // com.danikula.videocache.q public int read(byte[] bArr) throws ProxyCacheException { InputStream inputStream = this.f998e; if (inputStream != null) { try { return inputStream.read(bArr, 0, bArr.length); } catch (InterruptedIOException e2) { throw new InterruptedProxyCacheException("Reading source " + this.f996c.a + " is interrupted", e2); } catch (IOException e3) { throw new ProxyCacheException("Error reading data from " + this.f996c.a, e3); } } throw new ProxyCacheException("Error reading data from " + this.f996c.a + ": connection is absent!"); } public String toString() { return "HttpUrlSource{sourceInfo='" + this.f996c + com.alipay.sdk.util.g.f558d; } public k(String str, com.danikula.videocache.v.c cVar) { this(str, cVar, new com.danikula.videocache.u.a()); } public k(String str, com.danikula.videocache.v.c cVar, com.danikula.videocache.u.b bVar) { this.a = (com.danikula.videocache.v.c) n.a(cVar); this.f995b = (com.danikula.videocache.u.b) n.a(bVar); r rVar = cVar.get(str); this.f996c = rVar == null ? new r(str, -2147483648L, p.d(str)) : rVar; } private long a(HttpURLConnection httpURLConnection, long j, int i) throws IOException { long a = a(httpURLConnection); return i == 200 ? a : i == 206 ? a + j : this.f996c.f1012b; } public k(k kVar) { this.f996c = kVar.f996c; this.a = kVar.a; this.f995b = kVar.f995b; } private long a(HttpURLConnection httpURLConnection) { String headerField = httpURLConnection.getHeaderField(HttpHeaders.HEAD_KEY_CONTENT_LENGTH); if (headerField == null) { return -1L; } return Long.parseLong(headerField); } private HttpURLConnection a(long j, int i) throws IOException, ProxyCacheException { HttpURLConnection httpURLConnection; boolean z; String str = this.f996c.a; int i2 = 0; do { httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); a(httpURLConnection, str); if (j > 0) { httpURLConnection.setRequestProperty(HttpHeaders.HEAD_KEY_RANGE, "bytes=" + j + "-"); } if (i > 0) { httpURLConnection.setConnectTimeout(i); httpURLConnection.setReadTimeout(i); } int responseCode = httpURLConnection.getResponseCode(); z = responseCode == 301 || responseCode == 302 || responseCode == 303; if (z) { str = httpURLConnection.getHeaderField(HttpHeaders.HEAD_KEY_LOCATION); i2++; httpURLConnection.disconnect(); } if (i2 > 5) { throw new ProxyCacheException("Too many redirects: " + i2); } } while (z); return httpURLConnection; } private void a(HttpURLConnection httpURLConnection, String str) { Map<String, String> a = this.f995b.a(str); if (a == null) { return; } h.a("****** injectCustomHeaders ****** :" + a.size()); for (Map.Entry<String, String> entry : a.entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } public synchronized String a() throws ProxyCacheException { if (TextUtils.isEmpty(this.f996c.f1013c)) { c(); } return this.f996c.f1013c; } }