package defpackage;

import android.net.TrafficStats;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import androidx.media3.datasource.DataSourceException;
import androidx.media3.datasource.HttpDataSource$HttpDataSourceException;
import androidx.media3.datasource.HttpDataSource$InvalidResponseCodeException;
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 javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

/* JADX INFO: renamed from: ʾᵸ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C0335 extends AbstractC0824 {

    /* JADX INFO: renamed from: ʿٴˇ, reason: contains not printable characters */
    public InputStream f2057;

    /* JADX INFO: renamed from: ˀᵭ, reason: contains not printable characters */
    public long f2058;

    /* JADX INFO: renamed from: ˁˌᵩ, reason: contains not printable characters */
    public long f2059;

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public final C1709 f2060;

    /* JADX INFO: renamed from: ˉᵵᵜ, reason: contains not printable characters */
    public int f2061;

    /* JADX INFO: renamed from: ˌᵨʠᵫ, reason: contains not printable characters */
    public final C1709 f2062;

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public HttpURLConnection f2063;

    /* JADX INFO: renamed from: ᵠˏˏ, reason: contains not printable characters */
    public boolean f2064;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public final int f2065;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public final int f2066;

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public C0084 f2067;

    public C0335(int i, int i2, C1709 c1709) {
        super(true);
        this.f2065 = i;
        this.f2066 = i2;
        this.f2060 = c1709;
        this.f2062 = new C1709(0);
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // defpackage.InterfaceC1070
    public final void close() {
        try {
            InputStream inputStream = this.f2057;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    String str = AbstractC0956.f4222;
                    throw new HttpDataSource$HttpDataSourceException(2000, 3, e);
                }
            }
        } finally {
            this.f2057 = null;
            m1592();
            if (this.f2064) {
                this.f2064 = false;
                m2945();
            }
            this.f2063 = null;
            this.f2067 = null;
            TrafficStats.clearThreadStatsTag();
        }
    }

    /* 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 // defpackage.InterfaceC0372
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int read(byte[] bArr, int i, int i2) throws HttpDataSource$HttpDataSourceException {
        int i3;
        if (i2 == 0) {
            return 0;
        }
        try {
            long j = this.f2059;
            if (j != -1) {
                long j2 = j - this.f2058;
                if (j2 != 0) {
                    i2 = (int) Math.min(i2, j2);
                    InputStream inputStream = this.f2057;
                    String str = AbstractC0956.f4222;
                    i3 = inputStream.read(bArr, i, i2);
                    if (i3 != -1) {
                        this.f2058 += (long) i3;
                        m2946(i3);
                        return i3;
                    }
                }
            } else {
                InputStream inputStream2 = this.f2057;
                String str2 = AbstractC0956.f4222;
                i3 = inputStream2.read(bArr, i, i2);
                if (i3 != -1) {
                }
            }
            return -1;
        } catch (IOException e) {
            String str3 = AbstractC0956.f4222;
            throw HttpDataSource$HttpDataSourceException.m358(2, e);
        }
    }

    @Override // defpackage.AbstractC0824, defpackage.InterfaceC1070
    /* JADX INFO: renamed from: ˉᵲ */
    public final Map mo1306() {
        HttpURLConnection httpURLConnection = this.f2063;
        return httpURLConnection == null ? C1323.f5510 : new C3919(httpURLConnection.getHeaderFields());
    }

    /* JADX INFO: renamed from: ˊᵙᵾᵲ, reason: contains not printable characters */
    public final HttpURLConnection m1590(URL url, int i, byte[] bArr, long j, long j2, boolean z, boolean z2, Map map) throws IOException {
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        if (httpURLConnection instanceof HttpsURLConnection) {
            try {
                TrustManager[] trustManagerArr = {new ˆᵴ(1)};
                SSLContext sSLContext = SSLContext.getInstance("TLS");
                sSLContext.init(null, trustManagerArr, null);
                ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(sSLContext.getSocketFactory());
            } catch (Exception unused) {
            }
        }
        httpURLConnection.setConnectTimeout(this.f2065);
        httpURLConnection.setReadTimeout(this.f2066);
        HashMap map2 = new HashMap();
        C1709 c1709 = this.f2060;
        if (c1709 != null) {
            map2.putAll(c1709.m4733());
        }
        map2.putAll(this.f2062.m4733());
        map2.putAll(map);
        for (Map.Entry entry : map2.entrySet()) {
            httpURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
        String strM7089 = AbstractC3006.m7089(j, j2);
        if (strM7089 != null) {
            httpURLConnection.setRequestProperty("Range", strM7089);
        }
        httpURLConnection.setRequestProperty("Accept-Encoding", z ? "gzip" : "identity");
        httpURLConnection.setInstanceFollowRedirects(z2);
        httpURLConnection.setDoOutput(bArr != null);
        httpURLConnection.setRequestMethod(C0084.m1130(i));
        if (bArr != null) {
            httpURLConnection.setFixedLengthStreamingMode(bArr.length);
            httpURLConnection.connect();
            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(bArr);
            outputStream.close();
        } else {
            httpURLConnection.connect();
        }
        return httpURLConnection;
    }

    /* JADX INFO: renamed from: ˏːˆ, reason: contains not printable characters */
    public final void m1591(long j) throws IOException {
        if (j == 0) {
            return;
        }
        byte[] bArr = new byte[4096];
        while (j > 0) {
            int iMin = (int) Math.min(j, 4096L);
            InputStream inputStream = this.f2057;
            String str = AbstractC0956.f4222;
            int i = inputStream.read(bArr, 0, iMin);
            if (Thread.currentThread().isInterrupted()) {
                throw new HttpDataSource$HttpDataSourceException(2000, 1, new InterruptedIOException());
            }
            if (i == -1) {
                throw new HttpDataSource$HttpDataSourceException(2008);
            }
            j -= (long) i;
            m2946(i);
        }
    }

    /* JADX INFO: renamed from: ᵝᵶ, reason: contains not printable characters */
    public final void m1592() {
        HttpURLConnection httpURLConnection = this.f2063;
        if (httpURLConnection != null) {
            try {
                httpURLConnection.disconnect();
            } catch (Exception e) {
                ˋˀᵧ.ˋˈ("Unexpected error while disconnecting", e);
            }
        }
    }

    @Override // defpackage.InterfaceC1070
    /* JADX INFO: renamed from: ᵯᵷʽ */
    public final Uri mo1307() {
        HttpURLConnection httpURLConnection = this.f2063;
        if (httpURLConnection != null) {
            return Uri.parse(httpURLConnection.getURL().toString());
        }
        C0084 c0084 = this.f2067;
        if (c0084 != null) {
            return c0084.f1291;
        }
        return null;
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x0075  */
    /* JADX WARN: Removed duplicated region for block: B:45:0x00d2  */
    /* JADX WARN: Removed duplicated region for block: B:58:0x013d  */
    @Override // defpackage.InterfaceC1070
    /* JADX INFO: renamed from: ᵵᵽ */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final long mo1308(C0084 c0084) throws HttpDataSource$HttpDataSourceException {
        String headerField;
        long j;
        String headerField2;
        String headerField3;
        long j2;
        long jMax;
        long j3;
        this.f2067 = c0084;
        this.f2058 = 0L;
        this.f2059 = 0L;
        m2948();
        try {
            Thread threadCurrentThread = Thread.currentThread();
            TrafficStats.setThreadStatsTag((int) (Build.VERSION.SDK_INT < 36 ? threadCurrentThread.getId() : threadCurrentThread.threadId()));
            HttpURLConnection httpURLConnectionM1590 = m1590(new URL(c0084.f1291.toString()), c0084.f1287, c0084.f1289, c0084.f1290, c0084.f1294, (c0084.f1288 & 1) == 1, true, c0084.f1293);
            this.f2063 = httpURLConnectionM1590;
            this.f2061 = httpURLConnectionM1590.getResponseCode();
            httpURLConnectionM1590.getResponseMessage();
            int i = this.f2061;
            String headerField4 = null;
            if (i < 200 || i > 299) {
                Map<String, List<String>> headerFields = httpURLConnectionM1590.getHeaderFields();
                if (this.f2061 == 416) {
                    try {
                        headerField = httpURLConnectionM1590.getHeaderField("Content-Range");
                    } catch (Exception unused) {
                        headerField = null;
                    }
                    if (c0084.f1290 == AbstractC3006.m7088(headerField)) {
                        this.f2064 = true;
                        m2947(c0084);
                        long j4 = c0084.f1294;
                        if (j4 != -1) {
                            return j4;
                        }
                        return 0L;
                    }
                }
                InputStream errorStream = httpURLConnectionM1590.getErrorStream();
                try {
                    if (errorStream != null) {
                        AbstractC3780.m8454(errorStream);
                    } else {
                        String str = AbstractC0956.f4222;
                    }
                } catch (IOException unused2) {
                    String str2 = AbstractC0956.f4222;
                }
                m1592();
                throw new HttpDataSource$InvalidResponseCodeException(this.f2061, this.f2061 == 416 ? new DataSourceException(2008) : null, headerFields);
            }
            httpURLConnectionM1590.getContentType();
            if (this.f2061 == 200) {
                j = c0084.f1290;
                if (j == 0) {
                    j = 0;
                }
            }
            try {
                headerField2 = httpURLConnectionM1590.getHeaderField("Content-Encoding");
            } catch (Exception unused3) {
                headerField2 = null;
            }
            boolean zEqualsIgnoreCase = "gzip".equalsIgnoreCase(headerField2);
            long j5 = c0084.f1294;
            if (zEqualsIgnoreCase || j5 != -1) {
                this.f2059 = j5;
            } else {
                try {
                    headerField3 = httpURLConnectionM1590.getHeaderField("Content-Length");
                } catch (Exception unused4) {
                    headerField3 = null;
                }
                try {
                    headerField4 = httpURLConnectionM1590.getHeaderField("Content-Range");
                } catch (Exception unused5) {
                }
                Pattern pattern = AbstractC3006.f11576;
                if (TextUtils.isEmpty(headerField3)) {
                    j2 = 0;
                    jMax = -1;
                    if (TextUtils.isEmpty(headerField4)) {
                    }
                } else {
                    try {
                        j2 = 0;
                        jMax = Long.parseLong(headerField3);
                    } catch (NumberFormatException unused6) {
                        ˋˀᵧ.ᵝᵶ("Unexpected Content-Length [" + headerField3 + "]");
                        j2 = 0;
                        jMax = -1;
                    }
                    if (TextUtils.isEmpty(headerField4)) {
                        Matcher matcher = AbstractC3006.f11576.matcher(headerField4);
                        if (matcher.matches()) {
                            try {
                                String strGroup = matcher.group(2);
                                strGroup.getClass();
                                long j6 = Long.parseLong(strGroup);
                                String strGroup2 = matcher.group(1);
                                strGroup2.getClass();
                                j3 = -1;
                                long j7 = (j6 - Long.parseLong(strGroup2)) + 1;
                                if (jMax < j2) {
                                    jMax = j7;
                                } else if (jMax != j7) {
                                    try {
                                        ˋˀᵧ.ʿٴˇ("Inconsistent headers [" + headerField3 + "] [" + headerField4 + "]");
                                        jMax = Math.max(jMax, j7);
                                    } catch (NumberFormatException unused7) {
                                        ˋˀᵧ.ᵝᵶ("Unexpected Content-Range [" + headerField4 + "]");
                                    }
                                }
                            } catch (NumberFormatException unused8) {
                                j3 = -1;
                            }
                        } else {
                            j3 = -1;
                        }
                        this.f2059 = jMax != j3 ? jMax - j : j3;
                    }
                }
            }
            try {
                this.f2057 = httpURLConnectionM1590.getInputStream();
                if (zEqualsIgnoreCase) {
                    this.f2057 = new GZIPInputStream(this.f2057);
                }
                this.f2064 = true;
                m2947(c0084);
                try {
                    m1591(j);
                    return this.f2059;
                } catch (IOException e) {
                    m1592();
                    if (e instanceof HttpDataSource$HttpDataSourceException) {
                        throw ((HttpDataSource$HttpDataSourceException) e);
                    }
                    throw new HttpDataSource$HttpDataSourceException(2000, 1, e);
                }
            } catch (IOException e2) {
                m1592();
                throw new HttpDataSource$HttpDataSourceException(2000, 1, e2);
            }
        } catch (IOException e3) {
            m1592();
            throw HttpDataSource$HttpDataSourceException.m358(1, e3);
        }
    }
}
