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

import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.media3.common.PlaybackException;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.NoRouteToHostException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import tv.danmaku.ijk.media.player.IjkMediaMeta;

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

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

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

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

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

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

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

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

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

    @Nullable
    private final String zzd;

    @Nullable
    private final d72 zze;

    @Nullable
    private dy1 zzg;

    @Nullable
    private HttpURLConnection zzh;

    @Nullable
    private InputStream zzi;

    public /* synthetic */ z12(String str, int i10, int i11, boolean z10, d72 d72Var) {
        super(true);
        this.zzd = str;
        this.f19046e = i10;
        this.f19047f = i11;
        this.f19045d = z10;
        this.zze = d72Var;
        this.f19048g = new d72();
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0069  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final HttpURLConnection zzk(URL url, int i10, @Nullable byte[] bArr, long j10, long j11, boolean z10, boolean z11, Map map) throws IOException {
        String string;
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setConnectTimeout(this.f19046e);
        httpURLConnection.setReadTimeout(this.f19047f);
        HashMap map2 = new HashMap();
        map2.putAll(this.zze.a());
        map2.putAll(this.f19048g.a());
        map2.putAll(map);
        for (Map.Entry entry : map2.entrySet()) {
            httpURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
        if (j10 != 0) {
            StringBuilder sbS = defpackage.e.s("bytes=", j10, "-");
            if (j11 != -1) {
                sbS.append((j10 + j11) - 1);
            }
            string = sbS.toString();
        } else if (j11 == -1) {
            string = null;
        } else {
            j10 = 0;
            StringBuilder sbS2 = defpackage.e.s("bytes=", j10, "-");
            if (j11 != -1) {
            }
            string = sbS2.toString();
        }
        if (string != null) {
            httpURLConnection.setRequestProperty(RtspHeaders.RANGE, string);
        }
        String str = this.zzd;
        if (str != null) {
            httpURLConnection.setRequestProperty(RtspHeaders.USER_AGENT, str);
        }
        httpURLConnection.setRequestProperty("Accept-Encoding", true != z10 ? "identity" : "gzip");
        httpURLConnection.setInstanceFollowRedirects(z11);
        httpURLConnection.setDoOutput(false);
        int i11 = dy1.f10563f;
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.connect();
        return httpURLConnection;
    }

    private final URL zzl(URL url, @Nullable String str, dy1 dy1Var) throws zzhq {
        if (str == null) {
            throw new zzhq("Null location redirect", PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, 1);
        }
        try {
            URL url2 = new URL(url, str);
            String protocol = url2.getProtocol();
            if (!"https".equals(protocol) && !"http".equals(protocol)) {
                throw new zzhq("Unsupported protocol redirect: ".concat(String.valueOf(protocol)), PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, 1);
            }
            if (this.f19045d || protocol.equals(url.getProtocol())) {
                return url2;
            }
            String protocol2 = url.getProtocol();
            StringBuilder sb = new StringBuilder(com.google.android.gms.measurement.internal.u1.c(protocol, String.valueOf(protocol2).length() + 40, 1));
            sb.append("Disallowed cross-protocol redirect (");
            sb.append(protocol2);
            sb.append(" to ");
            sb.append(protocol);
            sb.append(")");
            throw new zzhq(sb.toString(), PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, 1);
        } catch (MalformedURLException e10) {
            throw new zzhq(e10, PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, 1);
        }
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws zzhq {
        int i10;
        URL url;
        long j10;
        long j11;
        boolean z10;
        HttpURLConnection httpURLConnectionZzk;
        byte[] bArrA;
        this.zzg = dy1Var;
        long j12 = 0;
        this.f19052k = 0L;
        this.f19051j = 0L;
        l(dy1Var);
        try {
            url = new URL(dy1Var.f10564a.toString());
            j10 = dy1Var.f10566c;
            j11 = dy1Var.f10567d;
            z10 = (dy1Var.f10568e & 1) == 1;
            try {
            } catch (IOException e10) {
                e = e10;
                i10 = 1;
            }
        } catch (IOException e11) {
            e = e11;
            i10 = 1;
        }
        if (this.f19045d) {
            URL urlZzl = url;
            int i11 = 0;
            while (true) {
                int i12 = i11 + 1;
                if (i11 > 20) {
                    i10 = 1;
                    StringBuilder sb = new StringBuilder(String.valueOf(i12).length() + 20);
                    sb.append("Too many redirects: ");
                    sb.append(i12);
                    throw new zzhq(new NoRouteToHostException(sb.toString()), PlaybackException.ERROR_CODE_IO_NETWORK_CONNECTION_FAILED, 1);
                }
                long j13 = j11;
                long j14 = j10;
                URL url2 = urlZzl;
                HttpURLConnection httpURLConnectionZzk2 = zzk(urlZzl, 1, null, j10, j11, z10, false, dy1Var.f10565b);
                int responseCode = httpURLConnectionZzk2.getResponseCode();
                String headerField = httpURLConnectionZzk2.getHeaderField(RtspHeaders.LOCATION);
                if (responseCode != 300 && responseCode != 301 && responseCode != 302 && responseCode != 303 && responseCode != 307 && responseCode != 308) {
                    httpURLConnectionZzk = httpURLConnectionZzk2;
                    break;
                }
                long j15 = j12;
                i10 = 1;
                try {
                    httpURLConnectionZzk2.disconnect();
                    urlZzl = zzl(url2, headerField, dy1Var);
                    j12 = j15;
                    i11 = i12;
                    j11 = j13;
                    j10 = j14;
                } catch (IOException e12) {
                    e = e12;
                }
                e = e12;
                p();
                throw zzhq.a(e, dy1Var, i10);
            }
        }
        httpURLConnectionZzk = zzk(url, 1, null, j10, j11, z10, true, dy1Var.f10565b);
        this.zzh = httpURLConnectionZzk;
        this.f19050i = httpURLConnectionZzk.getResponseCode();
        String responseMessage = httpURLConnectionZzk.getResponseMessage();
        int i13 = this.f19050i;
        long j16 = dy1Var.f10566c;
        long j17 = dy1Var.f10567d;
        if (i13 < 200 || i13 > 299) {
            Map<String, List<String>> headerFields = httpURLConnectionZzk.getHeaderFields();
            if (this.f19050i == 416 && j16 == c82.zza(httpURLConnectionZzk.getHeaderField("Content-Range"))) {
                this.f19049h = true;
                m(dy1Var);
                if (j17 != -1) {
                    return j17;
                }
                return 0L;
            }
            InputStream errorStream = httpURLConnectionZzk.getErrorStream();
            try {
                bArrA = errorStream != null ? jt1.a(errorStream) : bb1.f9475b;
            } catch (IOException unused) {
                bArrA = bb1.f9475b;
            }
            byte[] bArr = bArrA;
            p();
            throw new zzhs(this.f19050i, responseMessage, this.f19050i == 416 ? new zzhc(2008) : null, headerFields, dy1Var, bArr);
        }
        httpURLConnectionZzk.getContentType();
        if (this.f19050i != 200 || j16 == j12) {
            j16 = j12;
        }
        boolean zEqualsIgnoreCase = "gzip".equalsIgnoreCase(httpURLConnectionZzk.getHeaderField(RtspHeaders.CONTENT_ENCODING));
        if (zEqualsIgnoreCase || j17 != -1) {
            this.f19051j = j17;
        } else {
            long jZzb = c82.zzb(httpURLConnectionZzk.getHeaderField(RtspHeaders.CONTENT_LENGTH), httpURLConnectionZzk.getHeaderField("Content-Range"));
            this.f19051j = jZzb != -1 ? jZzb - j16 : -1L;
        }
        try {
            this.zzi = httpURLConnectionZzk.getInputStream();
            if (zEqualsIgnoreCase) {
                this.zzi = new GZIPInputStream(this.zzi);
            }
            this.f19049h = true;
            m(dy1Var);
            if (j16 != j12) {
                try {
                    byte[] bArr2 = new byte[4096];
                    while (j16 > j12) {
                        int iMin = (int) Math.min(j16, IjkMediaMeta.AV_CH_TOP_FRONT_LEFT);
                        InputStream inputStream = this.zzi;
                        String str = bb1.f9474a;
                        int i14 = inputStream.read(bArr2, 0, iMin);
                        if (Thread.currentThread().isInterrupted()) {
                            throw new zzhq(new InterruptedIOException(), 2000, 1);
                        }
                        if (i14 == -1) {
                            throw new zzhq();
                        }
                        j16 -= (long) i14;
                        n(i14);
                    }
                } catch (IOException e13) {
                    p();
                    if (e13 instanceof zzhq) {
                        throw ((zzhq) e13);
                    }
                    throw new zzhq(e13, 2000, 1);
                }
            }
            return this.f19051j;
        } catch (IOException e14) {
            p();
            throw new zzhq(e14, 2000, 1);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x002b  */
    /* JADX WARN: Removed duplicated region for block: B:15:0x002c A[Catch: IOException -> 0x001f, TRY_LEAVE, TryCatch #0 {IOException -> 0x001f, blocks: (B:4:0x0004, B:6:0x000d, B:9:0x0018, B:12:0x0021, B:15:0x002c), top: B:19:0x0004 }] */
    @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;
        if (i11 == 0) {
            return 0;
        }
        try {
            long j10 = this.f19051j;
            if (j10 != -1) {
                long j11 = j10 - this.f19052k;
                if (j11 != 0) {
                    i11 = (int) Math.min(i11, j11);
                    InputStream inputStream = this.zzi;
                    String str = bb1.f9474a;
                    i12 = inputStream.read(bArr, i10, i11);
                    if (i12 == -1) {
                        this.f19052k += (long) i12;
                        n(i12);
                        return i12;
                    }
                }
            } else {
                InputStream inputStream2 = this.zzi;
                String str2 = bb1.f9474a;
                i12 = inputStream2.read(bArr, i10, i11);
                if (i12 == -1) {
                }
            }
            return -1;
        } catch (IOException e10) {
            dy1 dy1Var = this.zzg;
            String str3 = bb1.f9474a;
            throw zzhq.a(e10, dy1Var, 2);
        }
    }

    public final void p() {
        HttpURLConnection httpURLConnection = this.zzh;
        if (httpURLConnection != null) {
            try {
                httpURLConnection.disconnect();
            } catch (Exception e10) {
                vt0.zzf("DefaultHttpDataSource", "Unexpected error while disconnecting", e10);
            }
        }
    }

    @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.zzh;
        if (httpURLConnection != null) {
            return Uri.parse(httpURLConnection.getURL().toString());
        }
        dy1 dy1Var = this.zzg;
        if (dy1Var != null) {
            return dy1Var.f10564a;
        }
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // com.google.android.gms.internal.ads.sv1
    public final void zzd() {
        try {
            InputStream inputStream = this.zzi;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e10) {
                    String str = bb1.f9474a;
                    throw new zzhq(e10, 2000, 3);
                }
            }
        } finally {
            this.zzi = null;
            p();
            if (this.f19049h) {
                this.f19049h = false;
                o();
            }
            this.zzh = null;
            this.zzg = null;
        }
    }

    @Override // com.google.android.gms.internal.ads.sv1, com.google.android.gms.internal.ads.n72
    public final Map zzj() {
        HttpURLConnection httpURLConnection = this.zzh;
        return httpURLConnection == null ? zzgwf.f19797g : new f12(httpURLConnection.getHeaderFields());
    }
}
