package com.applovin.impl; import android.net.Uri; import android.support.v4.media.session.PlaybackStateCompat; import com.applovin.exoplayer2.common.base.Predicate; import com.applovin.impl.qa; import com.google.firebase.perf.network.FirebasePerfUrlConnection; import com.mbridge.msdk.foundation.download.Command; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.io.OutputStream; import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.zip.GZIPInputStream; import org.apache.http.protocol.HTTP; /* loaded from: classes.dex */ public class d6 extends a2 implements qa { private final boolean e; private final int f; private final int g; private final String h; private final qa.f i; private final qa.f j; private final boolean k; private Predicate l; private l5 m; private HttpURLConnection n; private InputStream o; private boolean p; private int q; private long r; private long s; public static final class b implements qa.b { private xo b; private Predicate c; private String d; private boolean g; private boolean h; private final qa.f a = new qa.f(); private int e = 8000; private int f = 8000; public b a(String str) { this.d = str; return this; } @Override // com.applovin.impl.qa.b, com.applovin.impl.i5.a /* renamed from: b, reason: merged with bridge method [inline-methods] */ public d6 a() { d6 d6Var = new d6(this.d, this.e, this.f, this.g, this.a, this.c, this.h); xo xoVar = this.b; if (xoVar != null) { d6Var.a(xoVar); } return d6Var; } } private d6(String str, int i, int i2, boolean z, qa.f fVar, Predicate predicate, boolean z2) { super(true); this.h = str; this.f = i; this.g = i2; this.e = z; this.i = fVar; this.l = predicate; this.j = new qa.f(); this.k = z2; } @Override // com.applovin.impl.i5 public Uri c() { HttpURLConnection httpURLConnection = this.n; if (httpURLConnection == null) { return null; } return Uri.parse(httpURLConnection.getURL().toString()); } @Override // com.applovin.impl.i5 public Map e() { HttpURLConnection httpURLConnection = this.n; return httpURLConnection == null ? Collections.emptyMap() : httpURLConnection.getHeaderFields(); } @Override // com.applovin.impl.i5 public void close() { try { InputStream inputStream = this.o; if (inputStream != null) { long j = this.r; long j2 = -1; if (j != -1) { j2 = j - this.s; } a(this.n, j2); try { inputStream.close(); } catch (IOException e) { throw new qa.c(e, (l5) xp.a(this.m), 2000, 3); } } } finally { this.o = null; h(); if (this.p) { this.p = false; g(); } } } /* JADX WARN: Code restructure failed: missing block: B:22:0x00b0, code lost: return r0; */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ private java.net.HttpURLConnection d(com.applovin.impl.l5 r26) { /* Method dump skipped, instructions count: 217 To view this dump add '--comments-level debug' option */ throw new UnsupportedOperationException("Method not decompiled: com.applovin.impl.d6.d(com.applovin.impl.l5):java.net.HttpURLConnection"); } private URL a(URL url, String str, l5 l5Var) { if (str != null) { try { URL url2 = new URL(url, str); String protocol = url2.getProtocol(); if (!"https".equals(protocol) && !"http".equals(protocol)) { throw new qa.c("Unsupported protocol redirect: " + protocol, l5Var, 2001, 1); } if (this.e || protocol.equals(url.getProtocol())) { return url2; } throw new qa.c("Disallowed cross-protocol redirect (" + url.getProtocol() + " to " + protocol + ")", l5Var, 2001, 1); } catch (MalformedURLException e) { throw new qa.c(e, l5Var, 2001, 1); } } throw new qa.c("Null location redirect", l5Var, 2001, 1); } private void h() { HttpURLConnection httpURLConnection = this.n; if (httpURLConnection != null) { try { httpURLConnection.disconnect(); } catch (Exception e) { pc.a("DefaultHttpDataSource", "Unexpected error while disconnecting", e); } this.n = null; } } private int e(byte[] bArr, int i, int i2) { if (i2 == 0) { return 0; } long j = this.r; if (j != -1) { long j2 = j - this.s; if (j2 == 0) { return -1; } i2 = (int) Math.min(i2, j2); } int read = ((InputStream) xp.a((Object) this.o)).read(bArr, i, i2); if (read == -1) { return -1; } this.s += read; d(read); return read; } private static boolean a(HttpURLConnection httpURLConnection) { return "gzip".equalsIgnoreCase(httpURLConnection.getHeaderField(HTTP.CONTENT_ENCODING)); } private HttpURLConnection a(URL url, int i, byte[] bArr, long j, long j2, boolean z, boolean z2, Map map) { HttpURLConnection a2 = a(url); a2.setConnectTimeout(this.f); a2.setReadTimeout(this.g); HashMap hashMap = new HashMap(); qa.f fVar = this.i; if (fVar != null) { hashMap.putAll(fVar.a()); } hashMap.putAll(this.j.a()); hashMap.putAll(map); for (Map.Entry entry : hashMap.entrySet()) { a2.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); } String a3 = sa.a(j, j2); if (a3 != null) { a2.setRequestProperty(Command.HTTP_HEADER_RANGE, a3); } String str = this.h; if (str != null) { a2.setRequestProperty("User-Agent", str); } a2.setRequestProperty("Accept-Encoding", z ? "gzip" : HTTP.IDENTITY_CODING); a2.setInstanceFollowRedirects(z2); a2.setDoOutput(bArr != null); a2.setRequestMethod(l5.a(i)); if (bArr != null) { a2.setFixedLengthStreamingMode(bArr.length); a2.connect(); OutputStream outputStream = a2.getOutputStream(); outputStream.write(bArr); outputStream.close(); } else { a2.connect(); } return a2; } private static void a(HttpURLConnection httpURLConnection, long j) { int i; if (httpURLConnection == null || (i = xp.a) < 19 || i > 20) { return; } try { InputStream inputStream = httpURLConnection.getInputStream(); if (j == -1) { if (inputStream.read() == -1) { return; } } else if (j <= PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH) { return; } String name = inputStream.getClass().getName(); if ("com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream".equals(name) || "com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream".equals(name)) { Method declaredMethod = ((Class) b1.a(inputStream.getClass().getSuperclass())).getDeclaredMethod("unexpectedEndOfInput", new Class[0]); declaredMethod.setAccessible(true); declaredMethod.invoke(inputStream, new Object[0]); } } catch (Exception unused) { } } @Override // com.applovin.impl.i5 public long a(l5 l5Var) { byte[] bArr; this.m = l5Var; long j = 0; this.s = 0L; this.r = 0L; b(l5Var); try { HttpURLConnection d = d(l5Var); this.n = d; this.q = d.getResponseCode(); String responseMessage = d.getResponseMessage(); int i = this.q; if (i >= 200 && i <= 299) { String contentType = d.getContentType(); Predicate predicate = this.l; if (predicate != null && !predicate.apply(contentType)) { h(); throw new qa.d(contentType, l5Var); } if (this.q == 200) { long j2 = l5Var.g; if (j2 != 0) { j = j2; } } boolean a2 = a(d); if (!a2) { long j3 = l5Var.h; if (j3 != -1) { this.r = j3; } else { long a3 = sa.a(d.getHeaderField(HTTP.CONTENT_LEN), d.getHeaderField("Content-Range")); this.r = a3 != -1 ? a3 - j : -1L; } } else { this.r = l5Var.h; } try { this.o = d.getInputStream(); if (a2) { this.o = new GZIPInputStream(this.o); } this.p = true; c(l5Var); try { a(j, l5Var); return this.r; } catch (IOException e) { h(); if (e instanceof qa.c) { throw ((qa.c) e); } throw new qa.c(e, l5Var, 2000, 1); } } catch (IOException e2) { h(); throw new qa.c(e2, l5Var, 2000, 1); } } Map> headerFields = d.getHeaderFields(); if (this.q == 416) { if (l5Var.g == sa.a(d.getHeaderField("Content-Range"))) { this.p = true; c(l5Var); long j4 = l5Var.h; if (j4 != -1) { return j4; } return 0L; } } InputStream errorStream = d.getErrorStream(); try { if (errorStream != null) { bArr = xp.a(errorStream); } else { bArr = xp.f; } } catch (IOException unused) { bArr = xp.f; } byte[] bArr2 = bArr; h(); throw new qa.e(this.q, responseMessage, this.q == 416 ? new j5(2008) : null, headerFields, l5Var, bArr2); } catch (IOException e3) { h(); throw qa.c.a(e3, l5Var, 1); } } public HttpURLConnection a(URL url) { return (HttpURLConnection) ((URLConnection) FirebasePerfUrlConnection.instrument(url.openConnection())); } @Override // com.applovin.impl.g5 public int a(byte[] bArr, int i, int i2) { try { return e(bArr, i, i2); } catch (IOException e) { throw qa.c.a(e, (l5) xp.a(this.m), 2); } } private void a(long j, l5 l5Var) { if (j == 0) { return; } byte[] bArr = new byte[4096]; while (j > 0) { int read = ((InputStream) xp.a((Object) this.o)).read(bArr, 0, (int) Math.min(j, 4096)); if (Thread.currentThread().isInterrupted()) { throw new qa.c(new InterruptedIOException(), l5Var, 2000, 1); } if (read != -1) { j -= read; d(read); } else { throw new qa.c(l5Var, 2008, 1); } } } }