package defpackage;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
import androidx.media3.datasource.ContentDataSource$ContentDataSourceException;
import j$.util.Objects;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;

/* renamed from: ᵨˌᵳ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C3727 extends AbstractC1628 {

    /* renamed from: ˀᵗᵝᵭ, reason: contains not printable characters */
    public AssetFileDescriptor f13919;

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

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

    /* renamed from: ᵡˎ, reason: contains not printable characters */
    public FileInputStream f13922;

    /* renamed from: ᵡⁱᵥ, reason: contains not printable characters */
    public Uri f13923;

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

    public C3727(Context context) {
        super(false);
        this.f13924 = context.getContentResolver();
    }

    @Override // defpackage.InterfaceC2569
    public final void close() {
        this.f13923 = null;
        try {
            try {
                FileInputStream fileInputStream = this.f13922;
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                this.f13922 = null;
                try {
                    try {
                        AssetFileDescriptor assetFileDescriptor = this.f13919;
                        if (assetFileDescriptor != null) {
                            assetFileDescriptor.close();
                        }
                    } catch (IOException e) {
                        throw new ContentDataSource$ContentDataSourceException(2000, e);
                    }
                } finally {
                    this.f13919 = null;
                    if (this.f13920) {
                        this.f13920 = false;
                        m4667();
                    }
                }
            } catch (IOException e2) {
                throw new ContentDataSource$ContentDataSourceException(2000, e2);
            }
        } catch (Throwable th) {
            this.f13922 = null;
            try {
                try {
                    AssetFileDescriptor assetFileDescriptor2 = this.f13919;
                    if (assetFileDescriptor2 != null) {
                        assetFileDescriptor2.close();
                    }
                    this.f13919 = null;
                    if (this.f13920) {
                        this.f13920 = false;
                        m4667();
                    }
                    throw th;
                } catch (IOException e3) {
                    throw new ContentDataSource$ContentDataSourceException(2000, e3);
                }
            } finally {
                this.f13919 = null;
                if (this.f13920) {
                    this.f13920 = false;
                    m4667();
                }
            }
        }
    }

    @Override // defpackage.InterfaceC1463
    public final int read(byte[] bArr, int i, int i2) throws IOException {
        if (i2 == 0) {
            return 0;
        }
        long j = this.f13921;
        if (j != 0) {
            if (j != -1) {
                try {
                    i2 = (int) Math.min(j, i2);
                } catch (IOException e) {
                    throw new ContentDataSource$ContentDataSourceException(2000, e);
                }
            }
            FileInputStream fileInputStream = this.f13922;
            String str = AbstractC3558.f13329;
            int i3 = fileInputStream.read(bArr, i, i2);
            if (i3 != -1) {
                long j2 = this.f13921;
                if (j2 != -1) {
                    this.f13921 = j2 - i3;
                }
                m4668(i3);
                return i3;
            }
        }
        return -1;
    }

    @Override // defpackage.InterfaceC2569
    /* renamed from: ˏˋٴ */
    public final long mo1165(C4179 c4179) throws IOException {
        int i;
        AssetFileDescriptor assetFileDescriptorOpenAssetFileDescriptor;
        try {
            try {
                Uri uri = c4179.f15620;
                long j = c4179.f15618;
                long j2 = c4179.f15616;
                Uri uriNormalizeScheme = uri.normalizeScheme();
                this.f13923 = uriNormalizeScheme;
                m4666();
                boolean zEquals = Objects.equals(uriNormalizeScheme.getScheme(), "content");
                ContentResolver contentResolver = this.f13924;
                if (zEquals) {
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("android.provider.extra.ACCEPT_ORIGINAL_MEDIA_FORMAT", true);
                    assetFileDescriptorOpenAssetFileDescriptor = contentResolver.openTypedAssetFileDescriptor(uriNormalizeScheme, "*/*", bundle);
                } else {
                    assetFileDescriptorOpenAssetFileDescriptor = contentResolver.openAssetFileDescriptor(uriNormalizeScheme, "r");
                }
                this.f13919 = assetFileDescriptorOpenAssetFileDescriptor;
                if (assetFileDescriptorOpenAssetFileDescriptor == null) {
                    i = 2000;
                    try {
                        throw new ContentDataSource$ContentDataSourceException(2000, new IOException("Could not open file descriptor for: " + uriNormalizeScheme));
                    } catch (IOException e) {
                        e = e;
                        throw new ContentDataSource$ContentDataSourceException(e instanceof FileNotFoundException ? 2005 : i, e);
                    }
                }
                long length = assetFileDescriptorOpenAssetFileDescriptor.getLength();
                FileInputStream fileInputStream = new FileInputStream(assetFileDescriptorOpenAssetFileDescriptor.getFileDescriptor());
                this.f13922 = fileInputStream;
                if (length != -1 && j2 > length) {
                    throw new ContentDataSource$ContentDataSourceException(2008, null);
                }
                long startOffset = assetFileDescriptorOpenAssetFileDescriptor.getStartOffset();
                long jSkip = fileInputStream.skip(startOffset + j2) - startOffset;
                if (jSkip != j2) {
                    throw new ContentDataSource$ContentDataSourceException(2008, null);
                }
                if (length == -1) {
                    FileChannel channel = fileInputStream.getChannel();
                    long size = channel.size();
                    if (size == 0) {
                        this.f13921 = -1L;
                    } else {
                        long jPosition = size - channel.position();
                        this.f13921 = jPosition;
                        if (jPosition < 0) {
                            throw new ContentDataSource$ContentDataSourceException(2008, null);
                        }
                    }
                } else {
                    long j3 = length - jSkip;
                    this.f13921 = j3;
                    if (j3 < 0) {
                        throw new ContentDataSource$ContentDataSourceException(2008, null);
                    }
                }
                if (j != -1) {
                    long j4 = this.f13921;
                    this.f13921 = j4 == -1 ? j : Math.min(j4, j);
                }
                this.f13920 = true;
                m4669(c4179);
                return j != -1 ? j : this.f13921;
            } catch (ContentDataSource$ContentDataSourceException e2) {
                throw e2;
            }
        } catch (IOException e3) {
            e = e3;
            i = 2000;
        }
    }

    @Override // defpackage.InterfaceC2569
    /* renamed from: ᵿʗᵝʽ */
    public final Uri mo1166() {
        return this.f13923;
    }
}
