package z0;

import C3.b0;
import android.net.Uri;
import android.text.TextUtils;
import e0.C0853a;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import x0.AbstractC1709a;
import y6.C1767b;

/* JADX INFO: loaded from: classes.dex */
public final class n extends AbstractC1779c {

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

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

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    public int f19159C;
    public long D;

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

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

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final C0853a f19163w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public final C0853a f19164x;

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

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

    public n(int i, int i5, C0853a c0853a) {
        super(true);
        this.f19161u = i;
        this.f19162v = i5;
        this.f19163w = c0853a;
        this.f19164x = new C0853a(27);
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // z0.h
    public final void close() {
        try {
            InputStream inputStream = this.f19157A;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e8) {
                    int i = x0.v.f18553a;
                    throw new r(2000, 3, e8);
                }
            }
        } finally {
            this.f19157A = null;
            s();
            if (this.f19158B) {
                this.f19158B = false;
                j();
            }
            this.f19166z = null;
            this.f19165y = null;
        }
    }

    @Override // z0.h
    public final Uri e() {
        HttpURLConnection httpURLConnection = this.f19166z;
        if (httpURLConnection != null) {
            return Uri.parse(httpURLConnection.getURL().toString());
        }
        k kVar = this.f19165y;
        if (kVar != null) {
            return kVar.f19138a;
        }
        return null;
    }

    /* JADX WARN: Removed duplicated region for block: B:32:0x00bc  */
    /* JADX WARN: Removed duplicated region for block: B:47:0x0128  */
    /* JADX WARN: Removed duplicated region for block: B:48:0x012b  */
    @Override // z0.h
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long g(k kVar) throws r {
        boolean z8;
        long j8;
        long j9;
        long jMax;
        String str;
        this.f19165y = kVar;
        this.f19160E = 0L;
        this.D = 0L;
        l();
        try {
            HttpURLConnection httpURLConnectionT = t(new URL(kVar.f19138a.toString()), kVar.f19139b, kVar.f19140c, kVar.f19142e, kVar.f19143f, (kVar.f19144g & 1) == 1, true, kVar.f19141d);
            this.f19166z = httpURLConnectionT;
            this.f19159C = httpURLConnectionT.getResponseCode();
            httpURLConnectionT.getResponseMessage();
            int i = this.f19159C;
            long j10 = kVar.f19142e;
            long j11 = kVar.f19143f;
            if (i < 200 || i > 299) {
                Map<String, List<String>> headerFields = httpURLConnectionT.getHeaderFields();
                if (this.f19159C == 416) {
                    String headerField = httpURLConnectionT.getHeaderField("Content-Range");
                    Pattern pattern = u.f19173a;
                    if (TextUtils.isEmpty(headerField)) {
                        j8 = -1;
                        z8 = true;
                    } else {
                        Matcher matcher = u.f19174b.matcher(headerField);
                        z8 = true;
                        if (matcher.matches()) {
                            String strGroup = matcher.group(1);
                            strGroup.getClass();
                            j8 = Long.parseLong(strGroup);
                        } else {
                            j8 = -1;
                        }
                    }
                    if (j10 == j8) {
                        this.f19158B = z8;
                        r(kVar);
                        if (j11 != -1) {
                            return j11;
                        }
                        return 0L;
                    }
                }
                InputStream errorStream = httpURLConnectionT.getErrorStream();
                try {
                    if (errorStream != null) {
                        D3.b.b(errorStream);
                    } else {
                        int i5 = x0.v.f18553a;
                    }
                } catch (IOException unused) {
                    int i8 = x0.v.f18553a;
                }
                s();
                throw new t(this.f19159C, this.f19159C == 416 ? new i(2008) : null, headerFields);
            }
            httpURLConnectionT.getContentType();
            if (this.f19159C != 200 || j10 == 0) {
                j10 = 0;
            }
            boolean zEqualsIgnoreCase = "gzip".equalsIgnoreCase(httpURLConnectionT.getHeaderField("Content-Encoding"));
            if (zEqualsIgnoreCase || j11 != -1) {
                this.D = j11;
            } else {
                String headerField2 = httpURLConnectionT.getHeaderField("Content-Length");
                String headerField3 = httpURLConnectionT.getHeaderField("Content-Range");
                Pattern pattern2 = u.f19173a;
                if (TextUtils.isEmpty(headerField2)) {
                    j9 = 0;
                    jMax = -1;
                    if (!TextUtils.isEmpty(headerField3)) {
                    }
                    this.D = jMax == -1 ? jMax - j10 : -1L;
                } else {
                    try {
                        j9 = 0;
                        jMax = Long.parseLong(headerField2);
                    } catch (NumberFormatException unused2) {
                        AbstractC1709a.o("HttpUtil", "Unexpected Content-Length [" + headerField2 + "]");
                        j9 = 0;
                        jMax = -1;
                    }
                    if (!TextUtils.isEmpty(headerField3)) {
                        Matcher matcher2 = u.f19173a.matcher(headerField3);
                        if (matcher2.matches()) {
                            try {
                                String strGroup2 = matcher2.group(2);
                                strGroup2.getClass();
                                long j12 = Long.parseLong(strGroup2);
                                String strGroup3 = matcher2.group(1);
                                strGroup3.getClass();
                                str = "]";
                                long j13 = (j12 - Long.parseLong(strGroup3)) + 1;
                                if (jMax < j9) {
                                    jMax = j13;
                                } else if (jMax != j13) {
                                    try {
                                        AbstractC1709a.B("HttpUtil", "Inconsistent headers [" + headerField2 + "] [" + headerField3 + str);
                                        jMax = Math.max(jMax, j13);
                                    } catch (NumberFormatException unused3) {
                                        AbstractC1709a.o("HttpUtil", "Unexpected Content-Range [" + headerField3 + str);
                                    }
                                }
                            } catch (NumberFormatException unused4) {
                                str = "]";
                            }
                        }
                    }
                    this.D = jMax == -1 ? jMax - j10 : -1L;
                }
            }
            try {
                this.f19157A = httpURLConnectionT.getInputStream();
                if (zEqualsIgnoreCase) {
                    this.f19157A = new GZIPInputStream(this.f19157A);
                }
                this.f19158B = true;
                r(kVar);
                try {
                    u(j10);
                    return this.D;
                } catch (IOException e8) {
                    s();
                    if (e8 instanceof r) {
                        throw ((r) e8);
                    }
                    throw new r(2000, 1, e8);
                }
            } catch (IOException e9) {
                s();
                throw new r(2000, 1, e9);
            }
        } catch (IOException e10) {
            s();
            throw r.a(1, e10);
        }
    }

    @Override // z0.AbstractC1779c, z0.h
    public final Map m() {
        HttpURLConnection httpURLConnection = this.f19166z;
        return httpURLConnection == null ? b0.f1243w : new C1767b(httpURLConnection.getHeaderFields(), 1);
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0028 A[Catch: IOException -> 0x0032, TRY_LEAVE, TryCatch #0 {IOException -> 0x0032, blocks: (B:5:0x0004, B:7:0x000d, B:10:0x0017, B:11:0x001d, B:14:0x0028), top: B:19:0x0004 }] */
    @Override // u0.InterfaceC1571g
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int p(byte[] bArr, int i, int i5) throws r {
        int i8;
        if (i5 == 0) {
            return 0;
        }
        try {
            long j8 = this.D;
            if (j8 != -1) {
                long j9 = j8 - this.f19160E;
                if (j9 != 0) {
                    i5 = (int) Math.min(i5, j9);
                    InputStream inputStream = this.f19157A;
                    int i9 = x0.v.f18553a;
                    i8 = inputStream.read(bArr, i, i5);
                    if (i8 != -1) {
                        this.f19160E += (long) i8;
                        h(i8);
                        return i8;
                    }
                }
            } else {
                InputStream inputStream2 = this.f19157A;
                int i92 = x0.v.f18553a;
                i8 = inputStream2.read(bArr, i, i5);
                if (i8 != -1) {
                }
            }
            return -1;
        } catch (IOException e8) {
            int i10 = x0.v.f18553a;
            throw r.a(2, e8);
        }
    }

    public final void s() {
        HttpURLConnection httpURLConnection = this.f19166z;
        if (httpURLConnection != null) {
            try {
                httpURLConnection.disconnect();
            } catch (Exception e8) {
                AbstractC1709a.p("DefaultHttpDataSource", "Unexpected error while disconnecting", e8);
            }
        }
    }

    public final HttpURLConnection t(URL url, int i, byte[] bArr, long j8, long j9, boolean z8, boolean z9, Map map) throws IOException {
        String string;
        String str;
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setConnectTimeout(this.f19161u);
        httpURLConnection.setReadTimeout(this.f19162v);
        HashMap map2 = new HashMap();
        C0853a c0853a = this.f19163w;
        if (c0853a != null) {
            map2.putAll(c0853a.z());
        }
        map2.putAll(this.f19164x.z());
        map2.putAll(map);
        for (Map.Entry entry : map2.entrySet()) {
            httpURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
        Pattern pattern = u.f19173a;
        if (j8 == 0 && j9 == -1) {
            string = null;
        } else {
            StringBuilder sb = new StringBuilder("bytes=");
            sb.append(j8);
            sb.append("-");
            if (j9 != -1) {
                sb.append((j8 + j9) - 1);
            }
            string = sb.toString();
        }
        if (string != null) {
            httpURLConnection.setRequestProperty("Range", string);
        }
        httpURLConnection.setRequestProperty("Accept-Encoding", z8 ? "gzip" : "identity");
        httpURLConnection.setInstanceFollowRedirects(z9);
        httpURLConnection.setDoOutput(bArr != null);
        int i5 = k.f19137h;
        if (i == 1) {
            str = "GET";
        } else if (i == 2) {
            str = "POST";
        } else {
            if (i != 3) {
                throw new IllegalStateException();
            }
            str = "HEAD";
        }
        httpURLConnection.setRequestMethod(str);
        if (bArr == null) {
            httpURLConnection.connect();
            return httpURLConnection;
        }
        httpURLConnection.setFixedLengthStreamingMode(bArr.length);
        httpURLConnection.connect();
        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write(bArr);
        outputStream.close();
        return httpURLConnection;
    }

    public final void u(long j8) throws IOException {
        if (j8 == 0) {
            return;
        }
        byte[] bArr = new byte[4096];
        while (j8 > 0) {
            int iMin = (int) Math.min(j8, 4096);
            InputStream inputStream = this.f19157A;
            int i = x0.v.f18553a;
            int i5 = inputStream.read(bArr, 0, iMin);
            if (Thread.currentThread().isInterrupted()) {
                throw new r(2000, 1, new InterruptedIOException());
            }
            if (i5 == -1) {
                throw new r();
            }
            j8 -= (long) i5;
            h(i5);
        }
    }
}
