package e6; import android.text.TextUtils; import android.webkit.MimeTypeMap; import h6.b; import java.io.BufferedInputStream; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import java.util.Objects; public class h implements n { /* renamed from: a reason: collision with root package name */ public final b f205743a; /* renamed from: b reason: collision with root package name */ public final g6.b f205744b; /* renamed from: c reason: collision with root package name */ public o f205745c; /* renamed from: d reason: collision with root package name */ public HttpURLConnection f205746d; /* renamed from: e reason: collision with root package name */ public InputStream f205747e; public h(String str, b bVar, g6.b bVar2) { bVar.getClass(); this.f205743a = bVar; bVar2.getClass(); this.f205744b = bVar2; o oVar = bVar.get(str); if (oVar == null) { MimeTypeMap singleton = MimeTypeMap.getSingleton(); String fileExtensionFromUrl = MimeTypeMap.getFileExtensionFromUrl(str); oVar = new o(str, -2147483648L, TextUtils.isEmpty(fileExtensionFromUrl) ? null : singleton.getMimeTypeFromExtension(fileExtensionFromUrl)); } this.f205745c = oVar; } @Override // e6.n public void a(long j15) { long j16; try { HttpURLConnection c15 = c(j15, -1); this.f205746d = c15; String contentType = c15.getContentType(); this.f205747e = new BufferedInputStream(this.f205746d.getInputStream(), 8192); HttpURLConnection httpURLConnection = this.f205746d; int responseCode = httpURLConnection.getResponseCode(); String headerField = httpURLConnection.getHeaderField("Content-Length"); if (headerField == null) { j16 = -1; } else { j16 = Long.parseLong(headerField); } if (responseCode != 200) { if (responseCode == 206) { j16 += j15; } else { j16 = this.f205745c.f205762b; } } String str = this.f205745c.f205761a; o oVar = new o(str, j16, contentType); this.f205745c = oVar; this.f205743a.a(str, oVar); } catch (IOException e15) { throw new l("Error opening connection for " + this.f205745c.f205761a + " with offset " + j15, e15); } } /* JADX WARNING: Code restructure failed: missing block: B:13:0x003f, code lost: r1 = move-exception; */ /* JADX WARNING: Code restructure failed: missing block: B:20:?, code lost: r1 = r8.f205745c.f205761a; */ /* JADX WARNING: Code restructure failed: missing block: B:21:0x004c, code lost: if (0 != 0) goto L_0x004e; */ /* JADX WARNING: Code restructure failed: missing block: B:23:?, code lost: r3.close(); */ /* JADX WARNING: Code restructure failed: missing block: B:24:0x0051, code lost: if (r0 == null) goto L_?; */ /* JADX WARNING: Code restructure failed: missing block: B:28:0x0057, code lost: r3 = null; r0 = r1; r1 = r0; */ /* JADX WARNING: Code restructure failed: missing block: B:37:?, code lost: return; */ /* JADX WARNING: Failed to process nested try/catch */ /* JADX WARNING: Missing exception handler attribute for start block: B:19:0x0048 */ /* JADX WARNING: Removed duplicated region for block: B:30:0x005d A[SYNTHETIC, Splitter:B:30:0x005d] */ /* JADX WARNING: Removed duplicated region for block: B:34:0x0062 */ public final void b() { HttpURLConnection httpURLConnection; long j15; String str = this.f205745c.f205761a; Closeable closeable = null; try { httpURLConnection = c(0, 10000); String headerField = httpURLConnection.getHeaderField("Content-Length"); if (headerField == null) { j15 = -1; } else { j15 = Long.parseLong(headerField); } String contentType = httpURLConnection.getContentType(); InputStream inputStream = httpURLConnection.getInputStream(); String str2 = this.f205745c.f205761a; o oVar = new o(str2, j15, contentType); this.f205745c = oVar; this.f205743a.a(str2, oVar); Objects.toString(this.f205745c); if (inputStream != null) { try { inputStream.close(); } catch (IOException unused) { } } } catch (IOException unused2) { httpURLConnection = null; } catch (Throwable th5) { Throwable th6 = th5; HttpURLConnection httpURLConnection2 = null; if (closeable != null) { try { closeable.close(); } catch (IOException unused3) { } } if (httpURLConnection2 != null) { httpURLConnection2.disconnect(); } throw th6; } httpURLConnection.disconnect(); } public final HttpURLConnection c(long j15, int i15) { HttpURLConnection httpURLConnection; boolean z15; String str = this.f205745c.f205761a; int i16 = 0; do { int i17 = (j15 > 0 ? 1 : (j15 == 0 ? 0 : -1)); httpURLConnection = (HttpURLConnection) new URL(str).openConnection(); for (Map.Entry<String, String> entry : this.f205744b.a(str).entrySet()) { httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue()); } if (i17 > 0) { httpURLConnection.setRequestProperty("Range", "bytes=" + j15 + "-"); } if (i15 > 0) { httpURLConnection.setConnectTimeout(i15); httpURLConnection.setReadTimeout(i15); } int responseCode = httpURLConnection.getResponseCode(); z15 = responseCode == 301 || responseCode == 302 || responseCode == 303; if (z15) { str = httpURLConnection.getHeaderField("Location"); i16++; httpURLConnection.disconnect(); } if (i16 > 5) { throw new l("Too many redirects: " + i16); } } while (z15); return httpURLConnection; } @Override // e6.n public void close() { HttpURLConnection httpURLConnection = this.f205746d; if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (IllegalArgumentException | NullPointerException e15) { 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.", e15); } catch (ArrayIndexOutOfBoundsException unused) { } } } @Override // e6.n public synchronized long length() { if (this.f205745c.f205762b == -2147483648L) { b(); } return this.f205745c.f205762b; } @Override // e6.n public int read(byte[] bArr) { InputStream inputStream = this.f205747e; if (inputStream != null) { try { return inputStream.read(bArr, 0, bArr.length); } catch (InterruptedIOException e15) { throw new j("Reading source " + this.f205745c.f205761a + " is interrupted", e15); } catch (IOException e16) { throw new l("Error reading data from " + this.f205745c.f205761a, e16); } } else { throw new l("Error reading data from " + this.f205745c.f205761a + ": connection is absent!"); } } public String toString() { return "HttpUrlSource{sourceInfo='" + this.f205745c + "}"; } public h(h hVar) { this.f205745c = hVar.f205745c; this.f205743a = hVar.f205743a; this.f205744b = hVar.f205744b; } }