package com.google.android.gms.internal.ads;

import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;

/* JADX INFO: loaded from: classes2.dex */
public final class f50 extends qr1 implements n72 {

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public static final Pattern f11024s = Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$");

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public static final AtomicReference f11025t = new AtomicReference();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final e50 f11026d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final int f11027e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final int f11028f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final String f11029g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final d72 f11030h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public HttpURLConnection f11031i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public InputStream f11032j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public boolean f11033k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public int f11034l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public long f11035m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public long f11036n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public long f11037o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public long f11038p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public int f11039q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final HashSet f11040r;

    public f50(String str, n50 n50Var, int i10, int i11, int i12) {
        super(true);
        this.f11026d = new e50(this);
        this.f11040r = new HashSet();
        ki0.zza(str);
        this.f11029g = str;
        this.f11030h = new d72();
        this.f11027e = i10;
        this.f11028f = i11;
        this.f11039q = i12;
        if (n50Var != null) {
            g(n50Var);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:104:0x0297 A[Catch: IOException -> 0x0041, TryCatch #4 {IOException -> 0x0041, blocks: (B:3:0x000c, B:11:0x002a, B:13:0x0038, B:16:0x0044, B:17:0x005c, B:19:0x0062, B:28:0x0093, B:30:0x00b8, B:32:0x00de, B:33:0x00e3, B:35:0x00ec, B:36:0x00f3, B:49:0x011e, B:102:0x028a, B:104:0x0297, B:106:0x02a8, B:109:0x02b1, B:110:0x02be, B:112:0x02c6, B:113:0x02cd, B:114:0x02ce, B:115:0x02f0), top: B:126:0x000c }] */
    /* JADX WARN: Removed duplicated region for block: B:128:0x02c6 A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:35:0x00ec A[Catch: IOException -> 0x0041, TryCatch #4 {IOException -> 0x0041, blocks: (B:3:0x000c, B:11:0x002a, B:13:0x0038, B:16:0x0044, B:17:0x005c, B:19:0x0062, B:28:0x0093, B:30:0x00b8, B:32:0x00de, B:33:0x00e3, B:35:0x00ec, B:36:0x00f3, B:49:0x011e, B:102:0x028a, B:104:0x0297, B:106:0x02a8, B:109:0x02b1, B:110:0x02be, B:112:0x02c6, B:113:0x02cd, B:114:0x02ce, B:115:0x02f0), top: B:126:0x000c }] */
    /* JADX WARN: Removed duplicated region for block: B:74:0x0194  */
    /* JADX WARN: Removed duplicated region for block: B:87:0x021c  */
    @Override // com.google.android.gms.internal.ads.sv1
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long b(dy1 dy1Var) throws zzhq {
        long j10;
        long j11;
        String str;
        int i10;
        int responseCode;
        String headerField;
        long jMax;
        String headerField2;
        long j12 = 0;
        this.f11038p = 0L;
        this.f11037o = 0L;
        try {
            String str2 = "Too many redirects: ";
            URL url = new URL(dy1Var.f10564a.toString());
            int i11 = dy1Var.f10568e;
            boolean z10 = (i11 & 1) == 1;
            int i12 = 0;
            while (true) {
                int i13 = i12 + 1;
                if (i12 > 20) {
                    StringBuilder sb = new StringBuilder(String.valueOf(i13).length() + 20);
                    sb.append(str2);
                    sb.append(i13);
                    throw new NoRouteToHostException(sb.toString());
                }
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                if (httpURLConnection instanceof HttpsURLConnection) {
                    ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(this.f11026d);
                }
                httpURLConnection.setConnectTimeout(this.f11027e);
                httpURLConnection.setReadTimeout(this.f11028f);
                for (Map.Entry entry : this.f11030h.a().entrySet()) {
                    httpURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
                }
                long j13 = dy1Var.f10566c;
                long j14 = dy1Var.f10567d;
                if (j13 != j12) {
                    j10 = j13;
                } else if (j14 != -1) {
                    j10 = 0;
                } else {
                    str = str2;
                    j11 = j13;
                    i10 = i13;
                    httpURLConnection.setRequestProperty(RtspHeaders.USER_AGENT, this.f11029g);
                    if (!z10) {
                        httpURLConnection.setRequestProperty("Accept-Encoding", "identity");
                    }
                    httpURLConnection.setInstanceFollowRedirects(false);
                    httpURLConnection.setDoOutput(false);
                    httpURLConnection.connect();
                    responseCode = httpURLConnection.getResponseCode();
                    if (responseCode == 300 && responseCode != 301 && responseCode != 302 && responseCode != 303 && responseCode != 307 && responseCode != 308) {
                        this.f11031i = httpURLConnection;
                        try {
                            int responseCode2 = httpURLConnection.getResponseCode();
                            this.f11034l = responseCode2;
                            if (responseCode2 < 200 || responseCode2 > 299) {
                                Map<String, List<String>> headerFields = this.f11031i.getHeaderFields();
                                p();
                                zzhs zzhsVar = new zzhs(this.f11034l, null, null, headerFields, dy1Var, bb1.f9475b);
                                if (this.f11034l != 416) {
                                    throw zzhsVar;
                                }
                                zzhsVar.initCause(new zzhc(2008));
                                throw zzhsVar;
                            }
                            this.f11035m = (responseCode2 != 200 || j13 == j12) ? 0L : j11;
                            if ((i11 & 1) != 1 && j14 == -1) {
                                HttpURLConnection httpURLConnection2 = this.f11031i;
                                String headerField3 = httpURLConnection2.getHeaderField(RtspHeaders.CONTENT_LENGTH);
                                if (TextUtils.isEmpty(headerField3)) {
                                    jMax = -1;
                                    headerField2 = httpURLConnection2.getHeaderField("Content-Range");
                                    if (!TextUtils.isEmpty(headerField2)) {
                                    }
                                    this.f11036n = jMax != -1 ? jMax - this.f11035m : -1L;
                                } else {
                                    try {
                                        jMax = Long.parseLong(headerField3);
                                    } catch (NumberFormatException unused) {
                                        StringBuilder sb2 = new StringBuilder(String.valueOf(headerField3).length() + 28);
                                        sb2.append("Unexpected Content-Length [");
                                        sb2.append(headerField3);
                                        sb2.append("]");
                                        q7.j.b(sb2.toString());
                                        jMax = -1;
                                    }
                                    headerField2 = httpURLConnection2.getHeaderField("Content-Range");
                                    if (!TextUtils.isEmpty(headerField2)) {
                                        Matcher matcher = f11024s.matcher(headerField2);
                                        if (matcher.find()) {
                                            try {
                                                long j15 = (Long.parseLong(matcher.group(2)) - Long.parseLong(matcher.group(1))) + 1;
                                                if (jMax < 0) {
                                                    jMax = j15;
                                                } else if (jMax != j15) {
                                                    StringBuilder sb3 = new StringBuilder(String.valueOf(headerField3).length() + 25 + String.valueOf(headerField2).length() + 1);
                                                    sb3.append("Inconsistent headers [");
                                                    sb3.append(headerField3);
                                                    sb3.append("] [");
                                                    sb3.append(headerField2);
                                                    sb3.append("]");
                                                    q7.j.d(sb3.toString());
                                                    jMax = Math.max(jMax, j15);
                                                }
                                            } catch (NumberFormatException unused2) {
                                                StringBuilder sb4 = new StringBuilder(String.valueOf(headerField2).length() + 27);
                                                sb4.append("Unexpected Content-Range [");
                                                sb4.append(headerField2);
                                                sb4.append("]");
                                                q7.j.b(sb4.toString());
                                            }
                                        }
                                    }
                                    this.f11036n = jMax != -1 ? jMax - this.f11035m : -1L;
                                }
                            } else {
                                this.f11036n = j14;
                            }
                            try {
                                this.f11032j = this.f11031i.getInputStream();
                                this.f11033k = true;
                                m(dy1Var);
                                return this.f11036n;
                            } catch (IOException e10) {
                                p();
                                throw new zzhq(e10, 2000, 1);
                            }
                        } catch (IOException e11) {
                            p();
                            throw new zzhq("Unable to connect to ".concat(String.valueOf(dy1Var.f10564a.toString())), e11, dy1Var, 2000, 1);
                        }
                    }
                    headerField = httpURLConnection.getHeaderField(RtspHeaders.LOCATION);
                    httpURLConnection.disconnect();
                    if (headerField != null) {
                        throw new ProtocolException("Null location redirect");
                    }
                    URL url2 = new URL(url, headerField);
                    String protocol = url2.getProtocol();
                    if (!"https".equals(protocol) && !"http".equals(protocol)) {
                        throw new ProtocolException("Unsupported protocol redirect: ".concat(String.valueOf(protocol)));
                    }
                    url = url2;
                    j12 = 0;
                    str2 = str;
                    i12 = i10;
                }
                j11 = j13;
                StringBuilder sb5 = new StringBuilder(String.valueOf(j10).length() + 7);
                sb5.append("bytes=");
                i10 = i13;
                long j16 = j10;
                sb5.append(j16);
                sb5.append("-");
                String string = sb5.toString();
                if (j14 != -1) {
                    long j17 = (j16 + j14) - 1;
                    str = str2;
                    StringBuilder sb6 = new StringBuilder(string.length() + String.valueOf(j17).length());
                    sb6.append(string);
                    sb6.append(j17);
                    string = sb6.toString();
                } else {
                    str = str2;
                }
                httpURLConnection.setRequestProperty(RtspHeaders.RANGE, string);
                httpURLConnection.setRequestProperty(RtspHeaders.USER_AGENT, this.f11029g);
                if (!z10) {
                }
                httpURLConnection.setInstanceFollowRedirects(false);
                httpURLConnection.setDoOutput(false);
                httpURLConnection.connect();
                responseCode = httpURLConnection.getResponseCode();
                if (responseCode == 300) {
                }
                headerField = httpURLConnection.getHeaderField(RtspHeaders.LOCATION);
                httpURLConnection.disconnect();
                if (headerField != null) {
                }
            }
        } catch (IOException e12) {
            throw new zzhq("Unable to connect to ".concat(String.valueOf(dy1Var.f10564a.toString())), e12, dy1Var, 2000, 1);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:31:0x0079 A[Catch: IOException -> 0x001b, TryCatch #0 {IOException -> 0x001b, blocks: (B:2:0x0000, B:23:0x0058, B:25:0x0060, B:28:0x006b, B:29:0x0071, B:31:0x0079, B:34:0x0080, B:35:0x0085, B:36:0x0086, B:5:0x000b, B:7:0x0016, B:10:0x001e, B:12:0x0026, B:15:0x003c, B:16:0x0046, B:17:0x004b, B:18:0x004c, B:19:0x0051, B:20:0x0052), top: B:40:0x0000 }] */
    /* JADX WARN: Removed duplicated region for block: B:36:0x0086 A[Catch: IOException -> 0x001b, TRY_LEAVE, TryCatch #0 {IOException -> 0x001b, blocks: (B:2:0x0000, B:23:0x0058, B:25:0x0060, B:28:0x006b, B:29:0x0071, B:31:0x0079, B:34:0x0080, B:35:0x0085, B:36:0x0086, B:5:0x000b, B:7:0x0016, B:10:0x001e, B:12:0x0026, B:15:0x003c, B:16:0x0046, B:17:0x004b, B:18:0x004c, B:19:0x0051, B:20:0x0052), top: B:40:0x0000 }] */
    @Override // com.google.android.gms.internal.ads.eh2
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int d(byte[] bArr, int i10, int i11) throws zzhq {
        int i12;
        try {
            if (this.f11037o != this.f11035m) {
                AtomicReference atomicReference = f11025t;
                byte[] bArr2 = (byte[]) atomicReference.getAndSet(null);
                if (bArr2 == null) {
                    bArr2 = new byte[4096];
                }
                while (true) {
                    long j10 = this.f11037o;
                    long j11 = this.f11035m;
                    if (j10 == j11) {
                        atomicReference.set(bArr2);
                        break;
                    }
                    int i13 = this.f11032j.read(bArr2, 0, (int) Math.min(j11 - j10, bArr2.length));
                    if (Thread.interrupted()) {
                        throw new InterruptedIOException();
                    }
                    if (i13 == -1) {
                        throw new EOFException();
                    }
                    this.f11037o += (long) i13;
                    n(i13);
                }
            }
            if (i11 == 0) {
                return 0;
            }
            long j12 = this.f11036n;
            if (j12 != -1) {
                long j13 = j12 - this.f11038p;
                if (j13 != 0) {
                    i11 = (int) Math.min(i11, j13);
                    i12 = this.f11032j.read(bArr, i10, i11);
                    if (i12 == -1) {
                        this.f11038p += (long) i12;
                        n(i12);
                        return i12;
                    }
                    if (this.f11036n != -1) {
                        throw new EOFException();
                    }
                }
            } else {
                i12 = this.f11032j.read(bArr, i10, i11);
                if (i12 == -1) {
                }
            }
            return -1;
        } catch (IOException e10) {
            throw new zzhq(e10, 2000, 2);
        }
    }

    public final void p() {
        HttpURLConnection httpURLConnection = this.f11031i;
        if (httpURLConnection != null) {
            try {
                httpURLConnection.disconnect();
            } catch (Exception unused) {
            }
            this.f11031i = null;
        }
    }

    @Override // com.google.android.gms.internal.ads.qr1, com.google.android.gms.internal.ads.sv1, com.google.android.gms.internal.ads.n72
    @Nullable
    public final Uri zzc() {
        HttpURLConnection httpURLConnection = this.f11031i;
        if (httpURLConnection == null) {
            return null;
        }
        return Uri.parse(httpURLConnection.getURL().toString());
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final void zzd() {
        HashSet hashSet = this.f11040r;
        try {
            InputStream inputStream = this.f11032j;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e10) {
                    throw new zzhq(e10, 2000, 3);
                }
            }
        } finally {
            this.f11032j = null;
            p();
            if (this.f11033k) {
                this.f11033k = false;
                o();
            }
            hashSet.clear();
        }
    }

    @Override // com.google.android.gms.internal.ads.sv1, com.google.android.gms.internal.ads.n72
    @Nullable
    public final Map zzj() {
        HttpURLConnection httpURLConnection = this.f11031i;
        if (httpURLConnection == null) {
            return null;
        }
        return httpURLConnection.getHeaderFields();
    }
}
