package com.kwad.sdk.core.videocache; import android.text.TextUtils; import com.kwad.sdk.core.network.p; import com.kwad.sdk.core.network.r; import com.kwad.sdk.core.videocache.b.b; import com.kwad.sdk.core.videocache.d.c; import com.kwad.sdk.utils.ap; import com.mbridge.msdk.foundation.download.Command; 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 final class h extends p { private n aCJ; private HttpURLConnection aCK; private InputStream aCL; private final c aCl; private final b aCm; public h(String str, c cVar, b bVar) { this.aCl = (c) ap.checkNotNull(cVar); this.aCm = (b) ap.checkNotNull(bVar); n eP = cVar.eP(str); this.aCJ = eP == null ? new n(str, -2147483648L, l.eN(str)) : eP; } /* JADX WARNING: Can't wrap try/catch for region: R(2:12|13) */ /* JADX WARNING: Code restructure failed: missing block: B:13:?, code lost: com.kwad.sdk.core.e.c.e("HttpUrlSource", "Error fetching info from " + r8.aCJ.url); */ /* JADX WARNING: Code restructure failed: missing block: B:7:0x0058, code lost: r1 = th; */ /* JADX WARNING: Failed to process nested try/catch */ /* JADX WARNING: Missing exception handler attribute for start block: B:12:0x005e */ private void GM() { HttpURLConnection httpURLConnection; com.kwad.sdk.core.e.c.d("HttpUrlSource", "Read content info from " + this.aCJ.url); InputStream inputStream = null; try { httpURLConnection = c(0, 10000); long d = d(httpURLConnection); String contentType = httpURLConnection.getContentType(); inputStream = httpURLConnection.getInputStream(); n nVar = new n(this.aCJ.url, d, contentType); this.aCJ = nVar; this.aCl.a(nVar.url, nVar); com.kwad.sdk.core.e.c.d("HttpUrlSource", "Source info fetched: " + this.aCJ); } catch (IOException unused) { httpURLConnection = null; } catch (Throwable th) { Throwable th2 = th; httpURLConnection = null; com.kwad.sdk.crash.utils.b.closeQuietly(inputStream); com.kwad.sdk.crash.utils.b.a(httpURLConnection); throw th2; } com.kwad.sdk.crash.utils.b.closeQuietly(inputStream); com.kwad.sdk.crash.utils.b.a(httpURLConnection); } private void GO() { n eP; c cVar = this.aCl; if (cVar != null && (cVar instanceof com.kwad.sdk.core.videocache.d.b) && (eP = cVar.eP(getUrl())) != null && !TextUtils.isEmpty(eP.aDa) && eP.aCZ != -2147483648L) { this.aCJ = eP; } } private long a(HttpURLConnection httpURLConnection, long j, int i2) { long d = d(httpURLConnection); if (i2 == 200) { return d; } if (i2 == 206) { return d + j; } return this.aCJ.aCZ; } private HttpURLConnection c(long j, int i2) { String str; HttpURLConnection httpURLConnection; boolean z; String str2 = this.aCJ.url; int i3 = 0; do { StringBuilder sb = new StringBuilder("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.kwad.sdk.core.e.c.d("HttpUrlSource", sb.toString()); httpURLConnection = (HttpURLConnection) new URL(str2).openConnection(); r.wrapHttpURLConnection(httpURLConnection); a(httpURLConnection, str2); if (i4 > 0) { httpURLConnection.setRequestProperty(Command.HTTP_HEADER_RANGE, "bytes=" + j + "-"); } if (i2 > 0) { httpURLConnection.setConnectTimeout(i2); httpURLConnection.setReadTimeout(i2); } p.b(httpURLConnection); 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 ProxyCacheException("Too many redirects: " + i3); } } while (z); return httpURLConnection; } private static long d(HttpURLConnection httpURLConnection) { String headerField = httpURLConnection.getHeaderField("Content-Length"); if (headerField == null) { return -1; } return Long.parseLong(headerField); } @Override // com.kwad.sdk.core.videocache.p public final synchronized String GN() { if (TextUtils.isEmpty(this.aCJ.aDa)) { GO(); } if (TextUtils.isEmpty(this.aCJ.aDa)) { GM(); } return this.aCJ.aDa; } @Override // com.kwad.sdk.core.videocache.m public final void aA(long j) { try { HttpURLConnection c = c(j, -1); this.aCK = c; String contentType = c.getContentType(); this.aCL = new BufferedInputStream(this.aCK.getInputStream(), 1024); n nVar = new n(this.aCJ.url, a(this.aCK, j, this.aCK.getResponseCode()), contentType); this.aCJ = nVar; this.aCl.a(nVar.url, nVar); } catch (IOException e2) { throw new ProxyCacheException("Error opening connection for " + this.aCJ.url + " with offset " + j, e2); } } @Override // com.kwad.sdk.core.videocache.m public final void close() { HttpURLConnection httpURLConnection = this.aCK; 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 unused) { com.kwad.sdk.core.e.c.e("HttpUrlSource", "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."); } } } @Override // com.kwad.sdk.core.videocache.p public final String getUrl() { return this.aCJ.url; } @Override // com.kwad.sdk.core.videocache.m public final synchronized long length() { if (this.aCJ.aCZ == -2147483648L) { GO(); } if (this.aCJ.aCZ == -2147483648L) { GM(); } return this.aCJ.aCZ; } @Override // com.kwad.sdk.core.videocache.m public final int read(byte[] bArr) { InputStream inputStream = this.aCL; if (inputStream != null) { try { return inputStream.read(bArr, 0, 1024); } catch (InterruptedIOException e2) { throw new InterruptedProxyCacheException("Reading source " + this.aCJ.url + " is interrupted", e2); } catch (IOException e3) { throw new ProxyCacheException("Error reading data from " + this.aCJ.url, e3); } } else { throw new ProxyCacheException("Error reading data from " + this.aCJ.url + ": connection is absent!"); } } @Override // com.kwad.sdk.core.videocache.p public final String toString() { return "HttpUrlSource{sourceInfo='" + this.aCJ + "}"; } private void a(HttpURLConnection httpURLConnection, String str) { for (Map.Entry<String, String> entry : this.aCm.GV().entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } } public h(h hVar) { this.aCJ = hVar.aCJ; this.aCl = hVar.aCl; this.aCm = hVar.aCm; } }