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;

/* JADX INFO: renamed from: ˈᵤʿᵱ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C1098 extends AbstractC0824 {

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

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

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

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

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

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

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

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

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

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

    static {
        AbstractC0456.m1895("media3.datasource.okhttp");
    }

    public C1098(C3423 c3423, String str, C1709 c1709) {
        super(true);
        c3423.getClass();
        this.f4679 = c3423;
        this.f4674 = str;
        this.f4676 = c1709;
        this.f4680 = new C1709(0);
    }

    @Override // defpackage.InterfaceC1070
    public final void close() {
        if (this.f4678) {
            this.f4678 = false;
            m2945();
            m3547();
        }
        this.f4677 = null;
        this.f4681 = 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.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.f4675;
            if (j != -1) {
                long j2 = j - this.f4673;
                if (j2 != 0) {
                    i2 = (int) Math.min(i2, j2);
                    InputStream inputStream = this.f4672;
                    String str = AbstractC0956.f4222;
                    i3 = inputStream.read(bArr, i, i2);
                    if (i3 != -1) {
                        this.f4673 += (long) i3;
                        m2946(i3);
                        return i3;
                    }
                }
            } else {
                InputStream inputStream2 = this.f4672;
                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() {
        C1120 c1120 = this.f4677;
        return c1120 == null ? Collections.EMPTY_MAP : c1120.f4792.m2427();
    }

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

    /* JADX INFO: renamed from: ᵝᵶ, reason: contains not printable characters */
    public final void m3547() {
        C1120 c1120 = this.f4677;
        if (c1120 != null) {
            AbstractC4407 abstractC4407 = c1120.f4780;
            abstractC4407.getClass();
            abstractC4407.close();
        }
        this.f4672 = null;
    }

    @Override // defpackage.InterfaceC1070
    /* JADX INFO: renamed from: ᵯᵷʽ */
    public final Uri mo1307() {
        C1120 c1120 = this.f4677;
        if (c1120 != null) {
            return Uri.parse(c1120.f4784.f16708.f6701);
        }
        C0084 c0084 = this.f4681;
        if (c0084 != null) {
            return c0084.f1291;
        }
        return null;
    }

    @Override // defpackage.InterfaceC1070
    /* JADX INFO: renamed from: ᵵᵽ */
    public final long mo1308(C0084 c0084) throws HttpDataSource$HttpDataSourceException {
        C1695 c1695M10583;
        long j;
        C2320 c2320;
        this.f4681 = c0084;
        this.f4673 = 0L;
        this.f4675 = 0L;
        m2948();
        long j2 = c0084.f1290;
        int i = c0084.f1287;
        long j3 = c0084.f1294;
        String string = c0084.f1291.toString();
        try {
            C5096 c5096 = new C5096(2);
            c5096.m10582(null, string);
            c1695M10583 = c5096.m10583();
        } catch (IllegalArgumentException unused) {
            c1695M10583 = null;
        }
        if (c1695M10583 == null) {
            throw new HttpDataSource$HttpDataSourceException(1004, "Malformed URL");
        }
        ᵕˁـ r12 = new ᵕˁـ(7);
        r12.ᵜᵪʾ = c1695M10583;
        HashMap map = new HashMap();
        C1709 c1709 = this.f4676;
        if (c1709 != null) {
            map.putAll(c1709.m4733());
        }
        map.putAll(this.f4680.m4733());
        map.putAll(c0084.f1293);
        for (Map.Entry entry : map.entrySet()) {
            r12.ᵱʡˏ((String) entry.getKey(), (String) entry.getValue());
        }
        String strM7089 = AbstractC3006.m7089(j2, j3);
        if (strM7089 != null) {
            ((C4759) r12.ᵨᵼⁱ).m9939("Range", strM7089);
        }
        String str = this.f4674;
        if (str != null) {
            ((C4759) r12.ᵨᵼⁱ).m9939("User-Agent", str);
        }
        if ((c0084.f1288 & 1) != 1) {
            ((C4759) r12.ᵨᵼⁱ).m9939("Accept-Encoding", "identity");
        }
        byte[] bArr = c0084.f1289;
        if (bArr != null) {
            int length = bArr.length;
            j = 0;
            AbstractC1701.m4661(bArr.length, 0L, length);
            c2320 = new C2320(length, bArr);
        } else {
            j = 0;
            if (i == 2) {
                byte[] bArr2 = AbstractC0956.f4216;
                int length2 = bArr2.length;
                AbstractC1701.m4661(bArr2.length, 0L, length2);
                c2320 = new C2320(length2, bArr2);
            } else {
                c2320 = null;
            }
        }
        r12.ˉᵲ(C0084.m1130(i), c2320);
        C4402 c4402 = new C4402(r12);
        C3423 c3423 = this.f4679;
        c3423.getClass();
        C4778 c4778 = new C4778(c3423, c4402);
        try {
            C5308 c5308 = new C5308();
            c4778.m9971(new ʽˎˇˌ(10, c5308));
            try {
                try {
                    C1120 c1120 = (C1120) c5308.get();
                    this.f4677 = c1120;
                    AbstractC4407 abstractC4407 = c1120.f4780;
                    abstractC4407.getClass();
                    this.f4672 = abstractC4407.mo2194().mo5347();
                    int i2 = c1120.f4790;
                    if (!c1120.f4783) {
                        if (i2 == 416 && j2 == AbstractC3006.m7088(c1120.f4792.m2425("Content-Range"))) {
                            this.f4678 = true;
                            m2947(c0084);
                            return j3 != -1 ? j3 : j;
                        }
                        try {
                            InputStream inputStream = this.f4672;
                            inputStream.getClass();
                            AbstractC3780.m8454(inputStream);
                        } catch (IOException unused2) {
                            String str2 = AbstractC0956.f4222;
                        }
                        TreeMap treeMapM2427 = c1120.f4792.m2427();
                        m3547();
                        throw new HttpDataSource$InvalidResponseCodeException(i2, i2 == 416 ? new DataSourceException(2008) : null, treeMapM2427);
                    }
                    abstractC4407.mo2193();
                    if (i2 != 200 || j2 == j) {
                        j2 = j;
                    }
                    if (j3 != -1) {
                        this.f4675 = j3;
                    } else {
                        long jMo2195 = abstractC4407.mo2195();
                        this.f4675 = jMo2195 != -1 ? jMo2195 - j2 : -1L;
                    }
                    this.f4678 = true;
                    m2947(c0084);
                    try {
                        m3546(j2);
                        return this.f4675;
                    } catch (HttpDataSource$HttpDataSourceException e) {
                        m3547();
                        throw e;
                    }
                } catch (InterruptedException unused3) {
                    c4778.cancel();
                    throw new InterruptedIOException();
                }
            } catch (ExecutionException e2) {
                throw new IOException(e2);
            }
        } catch (IOException e3) {
            throw HttpDataSource$HttpDataSourceException.m358(1, e3);
        }
    }
}
