package com.opos.videocache; import android.text.TextUtils; import com.mbridge.msdk.foundation.download.Command; import com.opos.videocache.b.a; import com.opos.videocache.c.b; import com.opos.videocache.c.c; 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; public class d implements i { private final b a; private final com.opos.videocache.b.b b; private j c; private HttpURLConnection d; /* renamed from: e reason: collision with root package name */ private InputStream f15222e; public d(d dVar) { this.c = dVar.c; this.a = dVar.a; this.b = dVar.b; } public d(String str) { this(str, c.a()); } public d(String str, b bVar) { this(str, bVar, new a()); } public d(String str, b bVar, com.opos.videocache.b.b bVar2) { this.a = (b) f.a(bVar); this.b = (com.opos.videocache.b.b) f.a(bVar2); j a2 = bVar.a(str); this.c = a2 == null ? new j(str, -2147483648L, h.a(str)) : a2; } private long a(HttpURLConnection httpURLConnection) { String headerField = httpURLConnection.getHeaderField("Content-Length"); if (headerField == null) { return -1; } return Long.parseLong(headerField); } private long a(HttpURLConnection httpURLConnection, long j, int i2) { long a2 = a(httpURLConnection); return i2 == 200 ? a2 : i2 == 206 ? a2 + j : this.c.b; } private HttpURLConnection a(long j, int i2) { String str; HttpURLConnection httpURLConnection; boolean z; String str2 = this.c.a; int i3 = 0; do { StringBuilder sb = new StringBuilder(); sb.append("Open connection "); int i4 = (j > 0 ? 1 : (j == 0 ? 0 : -1)); if (i4 > 0) { str = " with offset " + j; } else { str = ""; } sb.append(str); sb.append(" to "); sb.append(str2); com.opos.cmn.an.f.a.b("HttpUrlSource", sb.toString()); httpURLConnection = (HttpURLConnection) new URL(str2).openConnection(); a(httpURLConnection, str2); if (i4 > 0) { httpURLConnection.setRequestProperty(Command.HTTP_HEADER_RANGE, "bytes=" + j + "-"); } if (i2 > 0) { httpURLConnection.setConnectTimeout(i2); httpURLConnection.setReadTimeout(i2); } int responseCode = httpURLConnection.getResponseCode(); z = responseCode == 301 || responseCode == 302 || responseCode == 303; if (z) { str2 = httpURLConnection.getHeaderField("Location"); i3++; httpURLConnection.disconnect(); } if (i3 > 5) { throw new g("Too many redirects: " + i3); } } while (z); return httpURLConnection; } private void a(HttpURLConnection httpURLConnection, String str) { for (Map.Entry<String, String> entry : this.b.a(str).entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } /* JADX WARNING: Removed duplicated region for block: B:20:0x008c */ /* JADX WARNING: Removed duplicated region for block: B:24:? A[RETURN, SYNTHETIC] */ private void e() { Throwable th; HttpURLConnection httpURLConnection; IOException e2; com.opos.cmn.an.f.a.b("HttpUrlSource", "Read content info from " + this.c.a); InputStream inputStream = null; try { httpURLConnection = a(0, 10000); try { long a2 = a(httpURLConnection); String contentType = httpURLConnection.getContentType(); inputStream = httpURLConnection.getInputStream(); j jVar = new j(this.c.a, a2, contentType); this.c = jVar; this.a.a(jVar.a, jVar); com.opos.cmn.an.f.a.b("HttpUrlSource", "Source info fetched: " + this.c); h.a(inputStream); if (httpURLConnection == null) { return; } } catch (IOException e3) { e2 = e3; try { com.opos.cmn.an.f.a.d("HttpUrlSource", "Error fetching info from " + this.c.a, e2); h.a(inputStream); if (httpURLConnection == null) { return; } httpURLConnection.disconnect(); } catch (Throwable th2) { th = th2; h.a(inputStream); if (httpURLConnection != null) { httpURLConnection.disconnect(); } throw th; } } } catch (IOException e4) { e2 = e4; httpURLConnection = null; com.opos.cmn.an.f.a.d("HttpUrlSource", "Error fetching info from " + this.c.a, e2); h.a(inputStream); if (httpURLConnection == null) { } httpURLConnection.disconnect(); } catch (Throwable th3) { th = th3; httpURLConnection = null; h.a(inputStream); if (httpURLConnection != null) { } throw th; } httpURLConnection.disconnect(); } @Override // com.opos.videocache.i public int a(byte[] bArr) { InputStream inputStream = this.f15222e; if (inputStream != null) { try { return inputStream.read(bArr, 0, bArr.length); } catch (InterruptedIOException e2) { throw new e("Reading source " + this.c.a + " is interrupted", e2); } catch (IOException e3) { throw new g("Error reading data from " + this.c.a, e3); } } else { throw new g("Error reading data from " + this.c.a + ": connection is absent!"); } } @Override // com.opos.videocache.i public synchronized long a() { if (this.c.b == -2147483648L) { e(); } return this.c.b; } @Override // com.opos.videocache.i public void a(long j) { try { HttpURLConnection a2 = a(j, -1); this.d = a2; String contentType = a2.getContentType(); this.f15222e = new BufferedInputStream(this.d.getInputStream(), 8192); j jVar = new j(this.c.a, a(this.d, j, this.d.getResponseCode()), contentType); this.c = jVar; this.a.a(jVar.a, jVar); } catch (IOException e2) { throw new g("Error opening connection for " + this.c.a + " with offset " + j, e2); } } @Override // com.opos.videocache.i public void b() { HttpURLConnection httpURLConnection = this.d; 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 .If you read it on your device log, please, notify me danikula@gmail.com or create issue here.", e2); } catch (ArrayIndexOutOfBoundsException e3) { com.opos.cmn.an.f.a.d("HttpUrlSource", "Error closing connection correctly. Should happen only on Android L. If anybody know how to fix it, please visit. Until good solution is not know, just ignore this issue :(", e3); } } } public synchronized String c() { if (TextUtils.isEmpty(this.c.c)) { e(); } return this.c.c; } public String d() { return this.c.a; } public String toString() { return "HttpUrlSource{sourceInfo='" + this.c + "}"; } }