package com.google.android.gms.internal.ads;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.media3.common.PlaybackException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Objects;

/* JADX INFO: loaded from: classes2.dex */
public final class rt1 extends qr1 {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final ContentResolver f16221d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public long f16222e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f16223f;

    @Nullable
    private Uri zzb;

    @Nullable
    private AssetFileDescriptor zzc;

    @Nullable
    private FileInputStream zzd;

    public rt1(Context context) {
        super(false);
        this.f16221d = context.getContentResolver();
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws zzgx {
        int i10;
        AssetFileDescriptor assetFileDescriptorOpenAssetFileDescriptor;
        long jPosition;
        try {
            try {
                Uri uriNormalizeScheme = dy1Var.f10564a.normalizeScheme();
                this.zzb = uriNormalizeScheme;
                l(dy1Var);
                boolean zEquals = Objects.equals(uriNormalizeScheme.getScheme(), "content");
                ContentResolver contentResolver = this.f16221d;
                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.zzc = assetFileDescriptorOpenAssetFileDescriptor;
                if (assetFileDescriptorOpenAssetFileDescriptor == null) {
                    String strValueOf = String.valueOf(uriNormalizeScheme);
                    StringBuilder sb = new StringBuilder(strValueOf.length() + 36);
                    sb.append("Could not open file descriptor for: ");
                    sb.append(strValueOf);
                    i10 = 2000;
                    try {
                        throw new zzgx(new IOException(sb.toString()), 2000);
                    } catch (IOException e10) {
                        e = e10;
                        if (true == (e instanceof FileNotFoundException)) {
                            i10 = PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND;
                        }
                        throw new zzgx(e, i10);
                    }
                }
                long length = assetFileDescriptorOpenAssetFileDescriptor.getLength();
                FileInputStream fileInputStream = new FileInputStream(assetFileDescriptorOpenAssetFileDescriptor.getFileDescriptor());
                this.zzd = fileInputStream;
                long j10 = dy1Var.f10566c;
                if (length != -1 && j10 > length) {
                    throw new zzgx(null, 2008);
                }
                long startOffset = assetFileDescriptorOpenAssetFileDescriptor.getStartOffset();
                long jSkip = fileInputStream.skip(startOffset + j10) - startOffset;
                if (jSkip != j10) {
                    throw new zzgx(null, 2008);
                }
                if (length == -1) {
                    FileChannel channel = fileInputStream.getChannel();
                    long size = channel.size();
                    if (size == 0) {
                        this.f16222e = -1L;
                        jPosition = -1;
                    } else {
                        jPosition = size - channel.position();
                        this.f16222e = jPosition;
                        if (jPosition < 0) {
                            throw new zzgx(null, 2008);
                        }
                    }
                } else {
                    jPosition = length - jSkip;
                    this.f16222e = jPosition;
                    if (jPosition < 0) {
                        throw new zzgx(null, 2008);
                    }
                }
                long j11 = dy1Var.f10567d;
                if (j11 != -1) {
                    this.f16222e = jPosition == -1 ? j11 : Math.min(jPosition, j11);
                }
                this.f16223f = true;
                m(dy1Var);
                return j11 != -1 ? j11 : this.f16222e;
            } catch (IOException e11) {
                e = e11;
                i10 = 2000;
            }
        } catch (zzgx e12) {
            throw e12;
        }
    }

    @Override // com.google.android.gms.internal.ads.eh2
    public final int d(byte[] bArr, int i10, int i11) throws zzgx {
        if (i11 == 0) {
            return 0;
        }
        long j10 = this.f16222e;
        if (j10 == 0) {
            return -1;
        }
        if (j10 != -1) {
            try {
                i11 = (int) Math.min(j10, i11);
            } catch (IOException e10) {
                throw new zzgx(e10, 2000);
            }
        }
        FileInputStream fileInputStream = this.zzd;
        String str = bb1.f9474a;
        int i12 = fileInputStream.read(bArr, i10, i11);
        if (i12 == -1) {
            return -1;
        }
        long j11 = this.f16222e;
        if (j11 != -1) {
            this.f16222e = j11 - ((long) i12);
        }
        n(i12);
        return i12;
    }

    @Override // com.google.android.gms.internal.ads.qr1, com.google.android.gms.internal.ads.sv1, com.google.android.gms.internal.ads.n72
    @Nullable
    public final Uri zzc() {
        return this.zzb;
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final void zzd() {
        this.zzb = null;
        try {
            try {
                try {
                    FileInputStream fileInputStream = this.zzd;
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                    this.zzd = null;
                    try {
                        AssetFileDescriptor assetFileDescriptor = this.zzc;
                        if (assetFileDescriptor != null) {
                            assetFileDescriptor.close();
                        }
                        this.zzc = null;
                        if (this.f16223f) {
                            this.f16223f = false;
                            o();
                        }
                    } catch (IOException e10) {
                        throw new zzgx(e10, 2000);
                    }
                } catch (IOException e11) {
                    throw new zzgx(e11, 2000);
                }
            } catch (Throwable th) {
                this.zzd = null;
                try {
                    AssetFileDescriptor assetFileDescriptor2 = this.zzc;
                    if (assetFileDescriptor2 != null) {
                        assetFileDescriptor2.close();
                    }
                    this.zzc = null;
                    if (this.f16223f) {
                        this.f16223f = false;
                        o();
                    }
                    throw th;
                } catch (IOException e12) {
                    throw new zzgx(e12, 2000);
                }
            }
        } catch (Throwable th2) {
            this.zzc = null;
            if (this.f16223f) {
                this.f16223f = false;
                o();
            }
            throw th2;
        }
    }
}
