package defpackage;

import android.net.Uri;
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.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ExecutionException;

/* renamed from: ᵚʧᵝ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C2642 extends AbstractC1628 {

    /* renamed from: ˀᵗᵝᵭ, reason: contains not printable characters */
    public final String f10230;

    /* renamed from: ˎˑː, reason: contains not printable characters */
    public C2977 f10231;

    /* renamed from: ˑᵥˁ, reason: contains not printable characters */
    public C4179 f10232;

    /* renamed from: ᐧﾞ, reason: contains not printable characters */
    public long f10233;

    /* renamed from: ᵡˎ, reason: contains not printable characters */
    public final C5553 f10234;

    /* renamed from: ᵡⁱᵥ, reason: contains not printable characters */
    public final C5553 f10235;

    /* renamed from: ᵤᵙᵷ, reason: contains not printable characters */
    public final C3914 f10236;

    /* renamed from: ᵧـˌᵎ, reason: contains not printable characters */
    public boolean f10237;

    /* renamed from: ᵮᵠـˌ, reason: contains not printable characters */
    public InputStream f10238;

    /* renamed from: ᵺʙ, reason: contains not printable characters */
    public long f10239;

    static {
        AbstractC2483.m6325("media3.datasource.okhttp");
    }

    public C2642(C3914 c3914, String str, C5553 c5553) {
        super(true);
        c3914.getClass();
        this.f10236 = c3914;
        this.f10230 = str;
        this.f10234 = c5553;
        this.f10235 = new C5553(0);
    }

    @Override // defpackage.InterfaceC2569
    public final void close() {
        if (this.f10237) {
            this.f10237 = false;
            m4667();
            m6628();
        }
        this.f10231 = null;
        this.f10232 = null;
    }

    /* 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.InterfaceC1463
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final int read(byte[] r7, int r8, int r9) throws java.io.IOException {
        /*
            r6 = this;
            if (r9 != 0) goto L4
            r6 = 0
            return r6
        L4:
            long r0 = r6.f10239     // Catch: java.io.IOException -> L32
            r2 = -1
            int r2 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1))
            r3 = -1
            if (r2 == 0) goto L1d
            long r4 = r6.f10233     // Catch: java.io.IOException -> L32
            long r0 = r0 - r4
            r4 = 0
            int r2 = (r0 > r4 ? 1 : (r0 == r4 ? 0 : -1))
            if (r2 != 0) goto L17
            goto L27
        L17:
            long r4 = (long) r9     // Catch: java.io.IOException -> L32
            long r0 = java.lang.Math.min(r4, r0)     // Catch: java.io.IOException -> L32
            int r9 = (int) r0     // Catch: java.io.IOException -> L32
        L1d:
            java.io.InputStream r0 = r6.f10238     // Catch: java.io.IOException -> L32
            java.lang.String r1 = defpackage.AbstractC3558.f13329     // Catch: java.io.IOException -> L32
            int r7 = r0.read(r7, r8, r9)     // Catch: java.io.IOException -> L32
            if (r7 != r3) goto L28
        L27:
            return r3
        L28:
            long r8 = r6.f10233     // Catch: java.io.IOException -> L32
            long r0 = (long) r7     // Catch: java.io.IOException -> L32
            long r8 = r8 + r0
            r6.f10233 = r8     // Catch: java.io.IOException -> L32
            r6.m4668(r7)     // Catch: java.io.IOException -> L32
            return r7
        L32:
            r6 = move-exception
            java.lang.String r7 = defpackage.AbstractC3558.f13329
            r7 = 2
            androidx.media3.datasource.HttpDataSource$HttpDataSourceException r6 = androidx.media3.datasource.HttpDataSource$HttpDataSourceException.m358(r7, r6)
            throw r6
        */
        throw new UnsupportedOperationException("Method not decompiled: defpackage.C2642.read(byte[], int, int):int");
    }

    @Override // defpackage.InterfaceC2569
    /* renamed from: ˏˋٴ */
    public final long mo1165(C4179 c4179) throws IOException {
        C1489 c1489M10502;
        long j;
        C4443 c4443;
        this.f10232 = c4179;
        this.f10233 = 0L;
        this.f10239 = 0L;
        m4666();
        long j2 = c4179.f15616;
        int i = c4179.f15621;
        long j3 = c4179.f15618;
        String string = c4179.f15620.toString();
        try {
            C5036 c5036 = new C5036(2);
            c5036.m10499(null, string);
            c1489M10502 = c5036.m10502();
        } catch (IllegalArgumentException unused) {
            c1489M10502 = null;
        }
        if (c1489M10502 == null) {
            throw new HttpDataSource$HttpDataSourceException(1004, "Malformed URL");
        }
        ʽˋːᵜ r12 = new ʽˋːᵜ(7);
        r12.ᵢˇᵭ = c1489M10502;
        HashMap map = new HashMap();
        C5553 c5553 = this.f10234;
        if (c5553 != null) {
            map.putAll(c5553.m11183());
        }
        map.putAll(this.f10235.m11183());
        map.putAll(c4179.f15623);
        for (Map.Entry entry : map.entrySet()) {
            r12.ᵡʠ((String) entry.getKey(), (String) entry.getValue());
        }
        String strM1243 = AbstractC0137.m1243(j2, j3);
        if (strM1243 != null) {
            ((C3842) r12.ᵾᵬ).m8494("Range", strM1243);
        }
        String str = this.f10230;
        if (str != null) {
            ((C3842) r12.ᵾᵬ).m8494("User-Agent", str);
        }
        if ((c4179.f15622 & 1) != 1) {
            ((C3842) r12.ᵾᵬ).m8494("Accept-Encoding", "identity");
        }
        byte[] bArr = c4179.f15619;
        if (bArr != null) {
            int length = bArr.length;
            j = 0;
            AbstractC1348.m4178(bArr.length, 0L, length);
            c4443 = new C4443(length, bArr);
        } else {
            j = 0;
            if (i == 2) {
                byte[] bArr2 = AbstractC3558.f13330;
                int length2 = bArr2.length;
                AbstractC1348.m4178(bArr2.length, 0L, length2);
                c4443 = new C4443(length2, bArr2);
            } else {
                c4443 = null;
            }
        }
        r12.ᵞʡ(C4179.m9098(i), c4443);
        C1947 c1947 = new C1947(r12);
        C3914 c3914 = this.f10236;
        c3914.getClass();
        C1588 c1588 = new C1588(c3914, c1947);
        try {
            C5202 c5202 = new C5202();
            c1588.m4562(new ᵿʤˑᵦ(11, c5202));
            try {
                try {
                    C2977 c2977 = (C2977) c5202.get();
                    this.f10231 = c2977;
                    AbstractC3866 abstractC3866 = c2977.f11499;
                    abstractC3866.getClass();
                    this.f10238 = abstractC3866.mo3786().mo3271();
                    int i2 = c2977.f11513;
                    if (!c2977.f11500) {
                        if (i2 == 416 && j2 == AbstractC0137.m1244(c2977.f11505.m6560("Content-Range"))) {
                            this.f10237 = true;
                            m4669(c4179);
                            return j3 != -1 ? j3 : j;
                        }
                        try {
                            InputStream inputStream = this.f10238;
                            inputStream.getClass();
                            AbstractC4058.m8917(inputStream);
                        } catch (IOException unused2) {
                            String str2 = AbstractC3558.f13329;
                        }
                        TreeMap treeMapM6557 = c2977.f11505.m6557();
                        m6628();
                        throw new HttpDataSource$InvalidResponseCodeException(i2, i2 == 416 ? new DataSourceException(2008) : null, treeMapM6557);
                    }
                    abstractC3866.mo3787();
                    if (i2 != 200 || j2 == j) {
                        j2 = j;
                    }
                    if (j3 != -1) {
                        this.f10239 = j3;
                    } else {
                        long jMo3788 = abstractC3866.mo3788();
                        this.f10239 = jMo3788 != -1 ? jMo3788 - j2 : -1L;
                    }
                    this.f10237 = true;
                    m4669(c4179);
                    try {
                        m6629(j2);
                        return this.f10239;
                    } catch (HttpDataSource$HttpDataSourceException e) {
                        m6628();
                        throw e;
                    }
                } catch (InterruptedException unused3) {
                    c1588.cancel();
                    throw new InterruptedIOException();
                }
            } catch (ExecutionException e2) {
                throw new IOException(e2);
            }
        } catch (IOException e3) {
            throw HttpDataSource$HttpDataSourceException.m358(1, e3);
        }
    }

    @Override // defpackage.AbstractC1628, defpackage.InterfaceC2569
    /* renamed from: ᵨᵳʝ */
    public final Map mo1416() {
        C2977 c2977 = this.f10231;
        return c2977 == null ? Collections.EMPTY_MAP : c2977.f11505.m6557();
    }

    /* renamed from: ᵯٴᵦ, reason: contains not printable characters */
    public final void m6628() {
        C2977 c2977 = this.f10231;
        if (c2977 != null) {
            AbstractC3866 abstractC3866 = c2977.f11499;
            abstractC3866.getClass();
            abstractC3866.close();
        }
        this.f10238 = null;
    }

    /* renamed from: ᵸʓᵠ, reason: contains not printable characters */
    public final void m6629(long j) throws IOException {
        if (j == 0) {
            return;
        }
        byte[] bArr = new byte[4096];
        while (j > 0) {
            try {
                int iMin = (int) Math.min(j, 4096L);
                InputStream inputStream = this.f10238;
                String str = AbstractC3558.f13329;
                int i = inputStream.read(bArr, 0, iMin);
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedIOException();
                }
                if (i == -1) {
                    throw new HttpDataSource$HttpDataSourceException(2008);
                }
                j -= i;
                m4668(i);
            } catch (IOException e) {
                if (!(e instanceof HttpDataSource$HttpDataSourceException)) {
                    throw new HttpDataSource$HttpDataSourceException(2000);
                }
                throw ((HttpDataSource$HttpDataSourceException) e);
            }
        }
    }

    @Override // defpackage.InterfaceC2569
    /* renamed from: ᵿʗᵝʽ */
    public final Uri mo1166() {
        C2977 c2977 = this.f10231;
        if (c2977 != null) {
            return Uri.parse(c2977.f11509.f7850.f5998);
        }
        C4179 c4179 = this.f10232;
        if (c4179 != null) {
            return c4179.f15620;
        }
        return null;
    }
}
