package com.danikula.videocache; import android.text.TextUtils; import com.alipay.sdk.util.g; import com.danikula.videocache.u.a; import com.danikula.videocache.u.b; import com.danikula.videocache.v.c; import com.danikula.videocache.v.d; 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 */ public class k implements q { /* renamed from: f reason: collision with root package name */ private static final int f994f = 5; private final c a; /* renamed from: b reason: collision with root package name */ private final 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, d.a()); } /* JADX WARNING: Removed duplicated region for block: B:20:0x0060 */ /* JADX WARNING: Removed duplicated region for block: B:24:? A[RETURN, SYNTHETIC] */ private void c() throws ProxyCacheException { Throwable th; HttpURLConnection httpURLConnection; IOException e2; InputStream inputStream = null; try { httpURLConnection = a(0, 10000); try { long a2 = a(httpURLConnection); String contentType = httpURLConnection.getContentType(); inputStream = httpURLConnection.getInputStream(); this.f996c = new r(this.f996c.a, a2, contentType); this.a.a(this.f996c.a, this.f996c); p.a(inputStream); if (httpURLConnection == null) { return; } } catch (IOException e3) { e2 = e3; try { h.a("Error fetching info from " + this.f996c.a, e2); p.a(inputStream); if (httpURLConnection == null) { return; } httpURLConnection.disconnect(); } catch (Throwable th2) { th = th2; p.a(inputStream); if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th; } } } catch (IOException e4) { e2 = e4; httpURLConnection = null; h.a("Error fetching info from " + this.f996c.a, e2); p.a(inputStream); if (httpURLConnection == null) { } httpURLConnection.disconnect(); } catch (Throwable th3) { th = th3; httpURLConnection = null; p.a(inputStream); if (httpURLConnection != null) { } throw th; } httpURLConnection.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 (IllegalArgumentException | NullPointerException e2) { 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.", e2); } catch (ArrayIndexOutOfBoundsException e3) { 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 :(", e3); } } } @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); } } else { throw new ProxyCacheException("Error reading data from " + this.f996c.a + ": connection is absent!"); } } public String toString() { return "HttpUrlSource{sourceInfo='" + this.f996c + g.f558d; } public k(String str, c cVar) { this(str, cVar, new a()); } public k(String str, c cVar, b bVar) { this.a = (c) n.a(cVar); this.f995b = (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 a2 = a(httpURLConnection); if (i == 200) { return a2; } if (i == 206) { return a2 + j; } return 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 -1; } 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> a2 = this.f995b.a(str); if (a2 != null) { h.a("****** injectCustomHeaders ****** :" + a2.size()); for (Map.Entry<String, String> entry : a2.entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } } public synchronized String a() throws ProxyCacheException { if (TextUtils.isEmpty(this.f996c.f1013c)) { c(); } return this.f996c.f1013c; } }