package p138.p176.p177.p190.p238;

import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.exoplayer2.upstream.DataSourceException;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.HashMap;
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 java.util.zip.GZIPInputStream;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p190.p239.C4131;
import p138.p176.p177.p190.p239.C4150;

/* JADX INFO: renamed from: ދ.ހ.֏.ؠ.ྌ.ލ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C4104 extends AbstractC4092 implements HttpDataSource {

    /* JADX INFO: renamed from: ޑ, reason: contains not printable characters */
    public static final Pattern f13129 = Pattern.compile("^bytes (\\d+)-(\\d+)/(\\d+)$");

    /* JADX INFO: renamed from: ޒ, reason: contains not printable characters */
    public static final AtomicReference<byte[]> f13130 = new AtomicReference<>();

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final boolean f13131;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final int f13132;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final int f13133;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final String f13134;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public final HttpDataSource.C0317 f13135;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public final HttpDataSource.C0317 f13136;

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public C4098 f13137;

    /* JADX INFO: renamed from: މ, reason: contains not printable characters */
    public HttpURLConnection f13138;

    /* JADX INFO: renamed from: ފ, reason: contains not printable characters */
    public InputStream f13139;

    /* JADX INFO: renamed from: ދ, reason: contains not printable characters */
    public boolean f13140;

    /* JADX INFO: renamed from: ތ, reason: contains not printable characters */
    public int f13141;

    /* JADX INFO: renamed from: ލ, reason: contains not printable characters */
    public long f13142;

    /* JADX INFO: renamed from: ގ, reason: contains not printable characters */
    public long f13143;

    /* JADX INFO: renamed from: ޏ, reason: contains not printable characters */
    public long f13144;

    /* JADX INFO: renamed from: ސ, reason: contains not printable characters */
    public long f13145;

    /* JADX INFO: renamed from: ދ.ހ.֏.ؠ.ྌ.ލ$ؠ, reason: contains not printable characters */
    public static class C4106 implements X509TrustManager {
        public /* synthetic */ C4106(C4105 c4105) {
        }

        @Override // javax.net.ssl.X509TrustManager
        public void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) {
        }

        @Override // javax.net.ssl.X509TrustManager
        public void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) {
        }

        @Override // javax.net.ssl.X509TrustManager
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }

    public C4104(String str, int i, int i2, boolean z, HttpDataSource.C0317 c0317) {
        super(true);
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException();
        }
        this.f13134 = str;
        this.f13136 = new HttpDataSource.C0317();
        this.f13132 = i;
        this.f13133 = i2;
        this.f13131 = z;
        this.f13135 = c0317;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static URL m6608(URL url, String str) throws ProtocolException {
        if (str == null) {
            throw new ProtocolException("Null location redirect");
        }
        URL url2 = new URL(url, str);
        String protocol = url2.getProtocol();
        if ("https".equals(protocol) || "http".equals(protocol)) {
            return url2;
        }
        throw new ProtocolException(C2803.m4879("Unsupported protocol redirect: ", protocol));
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static void m6609(HttpURLConnection httpURLConnection, long j) {
        int i = C4150.f13259;
        if (i == 19 || i == 20) {
            try {
                InputStream inputStream = httpURLConnection.getInputStream();
                if (j == -1) {
                    if (inputStream.read() == -1) {
                        return;
                    }
                } else if (j <= 2048) {
                    return;
                }
                String name = inputStream.getClass().getName();
                if ("com.android.okhttp.internal.http.HttpTransport$ChunkedInputStream".equals(name) || "com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream".equals(name)) {
                    Method declaredMethod = inputStream.getClass().getSuperclass().getDeclaredMethod("unexpectedEndOfInput", new Class[0]);
                    declaredMethod.setAccessible(true);
                    declaredMethod.invoke(inputStream, new Object[0]);
                }
            } catch (Exception unused) {
            }
        }
    }

    @Override // p138.p176.p177.p190.p238.InterfaceC4095
    public void close() {
        try {
            if (this.f13139 != null) {
                HttpURLConnection httpURLConnection = this.f13138;
                long j = this.f13143;
                if (j != -1) {
                    j -= this.f13145;
                }
                m6609(httpURLConnection, j);
                try {
                    this.f13139.close();
                } catch (IOException e) {
                    throw new HttpDataSource.HttpDataSourceException(e, this.f13137, 3);
                }
            }
        } finally {
            this.f13139 = null;
            m6612();
            if (this.f13140) {
                this.f13140 = false;
                m6581();
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0029 A[Catch: IOException -> 0x0040, TryCatch #0 {IOException -> 0x0040, blocks: (B:2:0x0000, B:5:0x0007, B:7:0x0010, B:10:0x001b, B:11:0x0021, B:13:0x0029, B:16:0x0030, B:17:0x0035, B:18:0x0036), top: B:23:0x0000 }] */
    /* JADX WARN: Removed duplicated region for block: B:18:0x0036 A[Catch: IOException -> 0x0040, TRY_LEAVE, TryCatch #0 {IOException -> 0x0040, blocks: (B:2:0x0000, B:5:0x0007, B:7:0x0010, B:10:0x001b, B:11:0x0021, B:13:0x0029, B:16:0x0030, B:17:0x0035, B:18:0x0036), top: B:23:0x0000 }] */
    @Override // p138.p176.p177.p190.p238.InterfaceC4095
    /* JADX INFO: renamed from: ֏ */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public int mo1282(byte[] bArr, int i, int i2) throws HttpDataSource.HttpDataSourceException {
        int i3;
        try {
            m6613();
            if (i2 == 0) {
                return 0;
            }
            long j = this.f13143;
            if (j != -1) {
                long j2 = j - this.f13145;
                if (j2 != 0) {
                    i2 = (int) Math.min(i2, j2);
                    i3 = this.f13139.read(bArr, i, i2);
                    if (i3 == -1) {
                        this.f13145 += (long) i3;
                        m6579(i3);
                        return i3;
                    }
                    if (this.f13143 != -1) {
                        throw new EOFException();
                    }
                }
            } else {
                i3 = this.f13139.read(bArr, i, i2);
                if (i3 == -1) {
                }
            }
            return -1;
        } catch (IOException e) {
            throw new HttpDataSource.HttpDataSourceException(e, this.f13137, 2);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:15:0x0040  */
    /* JADX WARN: Removed duplicated region for block: B:29:0x009a  */
    /* JADX WARN: Removed duplicated region for block: B:42:0x0108  */
    /* JADX WARN: Removed duplicated region for block: B:43:0x010d  */
    @Override // p138.p176.p177.p190.p238.InterfaceC4095
    /* JADX INFO: renamed from: ֏ */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public long mo1283(C4098 c4098) throws HttpDataSource.HttpDataSourceException {
        long j;
        long jMax;
        String headerField;
        this.f13137 = c4098;
        this.f13145 = 0L;
        this.f13144 = 0L;
        m6580(c4098);
        try {
            HttpURLConnection httpURLConnectionM6611 = m6611(c4098);
            this.f13138 = httpURLConnectionM6611;
            try {
                this.f13141 = httpURLConnectionM6611.getResponseCode();
                String responseMessage = this.f13138.getResponseMessage();
                int i = this.f13141;
                if (i < 200 || i > 299) {
                    Map<String, List<String>> headerFields = this.f13138.getHeaderFields();
                    m6612();
                    HttpDataSource.InvalidResponseCodeException invalidResponseCodeException = new HttpDataSource.InvalidResponseCodeException(this.f13141, responseMessage, headerFields, c4098);
                    if (this.f13141 != 416) {
                        throw invalidResponseCodeException;
                    }
                    invalidResponseCodeException.initCause(new DataSourceException(0));
                    throw invalidResponseCodeException;
                }
                this.f13138.getContentType();
                if (this.f13141 == 200) {
                    j = c4098.f13081;
                    if (j == 0) {
                        j = 0;
                    }
                }
                this.f13142 = j;
                boolean zEqualsIgnoreCase = "gzip".equalsIgnoreCase(this.f13138.getHeaderField("Content-Encoding"));
                if (zEqualsIgnoreCase) {
                    this.f13143 = c4098.f13082;
                } else {
                    long j2 = c4098.f13082;
                    if (j2 != -1) {
                        this.f13143 = j2;
                    } else {
                        HttpURLConnection httpURLConnection = this.f13138;
                        String headerField2 = httpURLConnection.getHeaderField("Content-Length");
                        if (TextUtils.isEmpty(headerField2)) {
                            jMax = -1;
                            headerField = httpURLConnection.getHeaderField("Content-Range");
                            if (!TextUtils.isEmpty(headerField)) {
                            }
                            this.f13143 = jMax == -1 ? jMax - this.f13142 : -1L;
                        } else {
                            try {
                                jMax = Long.parseLong(headerField2);
                            } catch (NumberFormatException unused) {
                                Log.e("DefaultHttpDataSource", "Unexpected Content-Length [" + headerField2 + "]");
                                jMax = -1;
                            }
                            headerField = httpURLConnection.getHeaderField("Content-Range");
                            if (!TextUtils.isEmpty(headerField)) {
                                Matcher matcher = f13129.matcher(headerField);
                                if (matcher.find()) {
                                    try {
                                        long j3 = (Long.parseLong(matcher.group(2)) - Long.parseLong(matcher.group(1))) + 1;
                                        if (jMax < 0) {
                                            jMax = j3;
                                        } else if (jMax != j3) {
                                            Log.w("DefaultHttpDataSource", "Inconsistent headers [" + headerField2 + "] [" + headerField + "]");
                                            jMax = Math.max(jMax, j3);
                                        }
                                    } catch (NumberFormatException unused2) {
                                        Log.e("DefaultHttpDataSource", "Unexpected Content-Range [" + headerField + "]");
                                    }
                                }
                            }
                            this.f13143 = jMax == -1 ? jMax - this.f13142 : -1L;
                        }
                    }
                }
                try {
                    this.f13139 = this.f13138.getInputStream();
                    if (zEqualsIgnoreCase) {
                        this.f13139 = new GZIPInputStream(this.f13139);
                    }
                    this.f13140 = true;
                    m6582(c4098);
                    return this.f13143;
                } catch (IOException e) {
                    m6612();
                    throw new HttpDataSource.HttpDataSourceException(e, c4098, 1);
                }
            } catch (IOException e2) {
                m6612();
                throw new HttpDataSource.HttpDataSourceException("Unable to connect", e2, c4098, 1);
            }
        } catch (IOException e3) {
            throw new HttpDataSource.HttpDataSourceException("Unable to connect", e3, c4098, 1);
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final HttpURLConnection m6610(URL url, int i, byte[] bArr, long j, long j2, boolean z, boolean z2, Map<String, String> map) throws IOException {
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        if (httpURLConnection instanceof HttpsURLConnection) {
            try {
                TrustManager[] trustManagerArr = {new C4106(null)};
                SSLContext sSLContext = SSLContext.getInstance("TLS");
                sSLContext.init(null, trustManagerArr, null);
                ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(sSLContext.getSocketFactory());
            } catch (Exception unused) {
            }
        }
        httpURLConnection.setConnectTimeout(this.f13132);
        httpURLConnection.setReadTimeout(this.f13133);
        HashMap map2 = new HashMap();
        HttpDataSource.C0317 c0317 = this.f13135;
        if (c0317 != null) {
            map2.putAll(c0317.m1287());
        }
        map2.putAll(this.f13136.m1287());
        map2.putAll(map);
        for (Map.Entry entry : map2.entrySet()) {
            httpURLConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
        }
        if (j != 0 || j2 != -1) {
            String strM4875 = C2803.m4875("bytes=", j, "-");
            if (j2 != -1) {
                StringBuilder sbM4889 = C2803.m4889(strM4875);
                sbM4889.append((j + j2) - 1);
                strM4875 = sbM4889.toString();
            }
            httpURLConnection.setRequestProperty("Range", strM4875);
        }
        httpURLConnection.setRequestProperty("User-Agent", this.f13134);
        httpURLConnection.setRequestProperty("Accept-Encoding", z ? "gzip" : "identity");
        httpURLConnection.setInstanceFollowRedirects(z2);
        httpURLConnection.setDoOutput(bArr != null);
        httpURLConnection.setRequestMethod(C4098.m6584(i));
        if (bArr != null) {
            httpURLConnection.setFixedLengthStreamingMode(bArr.length);
            httpURLConnection.connect();
            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(bArr);
            outputStream.close();
        } else {
            httpURLConnection.connect();
        }
        return httpURLConnection;
    }

    @Override // p138.p176.p177.p190.p238.AbstractC4092, p138.p176.p177.p190.p238.InterfaceC4095
    /* JADX INFO: renamed from: ֏ */
    public Map<String, List<String>> mo6120() {
        HttpURLConnection httpURLConnection = this.f13138;
        return httpURLConnection == null ? Collections.emptyMap() : httpURLConnection.getHeaderFields();
    }

    @Override // p138.p176.p177.p190.p238.InterfaceC4095
    /* JADX INFO: renamed from: ؠ */
    public Uri mo1284() {
        HttpURLConnection httpURLConnection = this.f13138;
        if (httpURLConnection == null) {
            return null;
        }
        return Uri.parse(httpURLConnection.getURL().toString());
    }

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final HttpURLConnection m6611(C4098 c4098) throws IOException {
        HttpURLConnection httpURLConnectionM6610;
        C4098 c40982 = c4098;
        URL url = new URL(c40982.f13076.toString());
        int i = c40982.f13077;
        byte[] bArr = c40982.f13078;
        long j = c40982.f13081;
        long j2 = c40982.f13082;
        int i2 = 0;
        boolean z = (c40982.f13084 & 1) == 1;
        if (!this.f13131) {
            return m6610(url, i, bArr, j, j2, z, true, c40982.f13079);
        }
        while (true) {
            int i3 = i2 + 1;
            if (i2 > 20) {
                throw new NoRouteToHostException(C2803.m4871("Too many redirects: ", i3));
            }
            long j3 = j2;
            long j4 = j;
            httpURLConnectionM6610 = m6610(url, i, bArr, j, j2, z, false, c40982.f13079);
            int responseCode = httpURLConnectionM6610.getResponseCode();
            String headerField = httpURLConnectionM6610.getHeaderField("Location");
            if ((i == 1 || i == 3) && (responseCode == 300 || responseCode == 301 || responseCode == 302 || responseCode == 303 || responseCode == 307 || responseCode == 308)) {
                httpURLConnectionM6610.disconnect();
                url = m6608(url, headerField);
            } else {
                if (i != 2 || (responseCode != 300 && responseCode != 301 && responseCode != 302 && responseCode != 303)) {
                    break;
                }
                httpURLConnectionM6610.disconnect();
                url = m6608(url, headerField);
                i = 1;
                bArr = null;
            }
            c40982 = c4098;
            i2 = i3;
            j2 = j3;
            j = j4;
        }
        return httpURLConnectionM6610;
    }

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final void m6612() {
        HttpURLConnection httpURLConnection = this.f13138;
        if (httpURLConnection != null) {
            try {
                httpURLConnection.disconnect();
            } catch (Exception e) {
                C4131.m6645("DefaultHttpDataSource", "Unexpected error while disconnecting", e);
            }
            this.f13138 = null;
        }
    }

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final void m6613() throws IOException {
        if (this.f13144 == this.f13142) {
            return;
        }
        byte[] andSet = f13130.getAndSet(null);
        if (andSet == null) {
            andSet = new byte[4096];
        }
        while (true) {
            long j = this.f13144;
            long j2 = this.f13142;
            if (j == j2) {
                f13130.set(andSet);
                return;
            }
            int i = this.f13139.read(andSet, 0, (int) Math.min(j2 - j, andSet.length));
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedIOException();
            }
            if (i == -1) {
                throw new EOFException();
            }
            this.f13144 += (long) i;
            m6579(i);
        }
    }
}
