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.SequenceInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayDeque;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

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

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public dy1 f14856h;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final ArrayDeque f14858j;

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final long f14867s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final long f14868t;

    public o50(String str, n50 n50Var, int i10, int i11, long j10, long j11) {
        super(true);
        ki0.zza(str);
        this.f14854f = str;
        this.f14855g = new d72();
        this.f14852d = i10;
        this.f14853e = i11;
        this.f14858j = new ArrayDeque();
        this.f14867s = j10;
        this.f14868t = j11;
        if (n50Var != null) {
            g(n50Var);
        }
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws zzhq {
        this.f14856h = dy1Var;
        this.f14863o = 0L;
        long j10 = dy1Var.f10566c;
        long j11 = dy1Var.f10567d;
        long jMin = this.f14867s;
        if (j11 != -1) {
            jMin = Math.min(jMin, j11);
        }
        this.f14864p = j10;
        HttpURLConnection httpURLConnectionP = p(1, j10, (jMin + j10) - 1);
        this.f14857i = httpURLConnectionP;
        String headerField = httpURLConnectionP.getHeaderField("Content-Range");
        if (!TextUtils.isEmpty(headerField)) {
            Matcher matcher = f14851u.matcher(headerField);
            if (matcher.find()) {
                try {
                    Long.parseLong(matcher.group(1));
                    long j12 = Long.parseLong(matcher.group(2));
                    long j13 = Long.parseLong(matcher.group(3));
                    if (j11 != -1) {
                        this.f14862n = j11;
                        this.f14865q = Math.max(j12, (this.f14864p + j11) - 1);
                    } else {
                        this.f14862n = j13 - this.f14864p;
                        this.f14865q = j13 - 1;
                    }
                    this.f14866r = j12;
                    this.f14860l = true;
                    m(dy1Var);
                    return this.f14862n;
                } catch (NumberFormatException unused) {
                    StringBuilder sb = new StringBuilder(String.valueOf(headerField).length() + 27);
                    sb.append("Unexpected Content-Range [");
                    sb.append(headerField);
                    sb.append("]");
                    q7.j.b(sb.toString());
                }
            }
        }
        throw new zzciu("Invalid content range: ".concat(String.valueOf(headerField)), 2000, 1);
    }

    @Override // com.google.android.gms.internal.ads.eh2
    public final int d(byte[] bArr, int i10, int i11) throws zzhq {
        if (i11 == 0) {
            return 0;
        }
        try {
            long j10 = this.f14862n;
            long j11 = this.f14863o;
            if (j10 - j11 == 0) {
                return -1;
            }
            long j12 = this.f14864p + j11;
            long j13 = i11;
            long j14 = j12 + j13 + this.f14868t;
            long j15 = this.f14866r;
            long j16 = j15 + 1;
            if (j14 > j16) {
                long j17 = this.f14865q;
                if (j15 < j17) {
                    long jMin = Math.min(j17, Math.max(((this.f14867s + j16) - r3) - 1, (-1) + j16 + j13));
                    p(2, j16, jMin);
                    this.f14866r = jMin;
                    j15 = jMin;
                }
            }
            int i12 = this.f14859k.read(bArr, i10, (int) Math.min(j13, ((j15 + 1) - this.f14864p) - this.f14863o));
            if (i12 == -1) {
                throw new EOFException();
            }
            this.f14863o += (long) i12;
            n(i12);
            return i12;
        } catch (IOException e10) {
            throw new zzhq(e10, 2000, 2);
        }
    }

    public final HttpURLConnection p(int i10, long j10, long j11) throws zzhq {
        String string = this.f14856h.f10564a.toString();
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(string).openConnection();
            httpURLConnection.setConnectTimeout(this.f14852d);
            httpURLConnection.setReadTimeout(this.f14853e);
            for (Map.Entry entry : this.f14855g.a().entrySet()) {
                httpURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
            }
            StringBuilder sb = new StringBuilder(String.valueOf(j10).length() + 7 + String.valueOf(j11).length());
            sb.append("bytes=");
            sb.append(j10);
            sb.append("-");
            sb.append(j11);
            httpURLConnection.setRequestProperty(RtspHeaders.RANGE, sb.toString());
            httpURLConnection.setRequestProperty(RtspHeaders.USER_AGENT, this.f14854f);
            httpURLConnection.setRequestProperty("Accept-Encoding", "identity");
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.connect();
            this.f14858j.add(httpURLConnection);
            String string2 = this.f14856h.f10564a.toString();
            try {
                int responseCode = httpURLConnection.getResponseCode();
                this.f14861m = responseCode;
                if (responseCode < 200 || responseCode > 299) {
                    httpURLConnection.getHeaderFields();
                    q();
                    int i11 = this.f14861m;
                    throw new zzciv(defpackage.e.n(new StringBuilder(String.valueOf(i11).length() + 15), "Response code: ", i11), 2000, i10);
                }
                try {
                    InputStream inputStream = httpURLConnection.getInputStream();
                    if (this.f14859k != null) {
                        inputStream = new SequenceInputStream(this.f14859k, inputStream);
                    }
                    this.f14859k = inputStream;
                    return httpURLConnection;
                } catch (IOException e10) {
                    q();
                    throw new zzhq(e10, 2000, i10);
                }
            } catch (IOException e11) {
                q();
                String strValueOf = String.valueOf(string2);
                throw new zzhq("Unable to connect to ".concat(strValueOf), e11, this.f14856h, 2000, i10);
            }
        } catch (IOException e12) {
            String strValueOf2 = String.valueOf(string);
            throw new zzhq("Unable to connect to ".concat(strValueOf2), e12, this.f14856h, 2000, i10);
        }
    }

    public final void q() {
        while (true) {
            ArrayDeque arrayDeque = this.f14858j;
            if (arrayDeque.isEmpty()) {
                this.f14857i = null;
                return;
            }
            try {
                ((HttpURLConnection) arrayDeque.remove()).disconnect();
            } catch (Exception unused) {
            }
        }
    }

    @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.f14857i;
        if (httpURLConnection == null) {
            return null;
        }
        return Uri.parse(httpURLConnection.getURL().toString());
    }

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

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