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

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.media3.common.PlaybackException;
import androidx.media3.datasource.RawResourceDataSource;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.util.List;

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

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

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

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

    @Nullable
    private dy1 zzb;

    @Nullable
    private AssetFileDescriptor zzc;

    @Nullable
    private InputStream zzd;

    public u82(Context context) {
        super(false);
        this.f17161d = context.getApplicationContext();
    }

    @Override // com.google.android.gms.internal.ads.sv1
    public final long b(dy1 dy1Var) throws zzhw {
        Resources resourcesForApplication;
        int identifier;
        Resources resources;
        long size;
        this.zzb = dy1Var;
        l(dy1Var);
        Uri uriNormalizeScheme = dy1Var.f10564a.normalizeScheme();
        boolean zEquals = TextUtils.equals(RawResourceDataSource.RAW_RESOURCE_SCHEME, uriNormalizeScheme.getScheme());
        Context context = this.f17161d;
        if (zEquals) {
            resources = context.getResources();
            List<String> pathSegments = uriNormalizeScheme.getPathSegments();
            if (pathSegments.size() != 1) {
                int size2 = pathSegments.size();
                throw new zzhw(defpackage.e.n(new StringBuilder(String.valueOf(size2).length() + 61), "rawresource:// URI must have exactly one path element, found ", size2), null, 2000);
            }
            try {
                identifier = Integer.parseInt(pathSegments.get(0));
            } catch (NumberFormatException unused) {
                throw new zzhw("Resource identifier must be an integer.", null, 1004);
            }
        } else {
            if (!TextUtils.equals("android.resource", uriNormalizeScheme.getScheme())) {
                String scheme = uriNormalizeScheme.getScheme();
                throw new zzhw(defpackage.e.p(new StringBuilder(String.valueOf(scheme).length() + 62), "Unsupported URI scheme (", scheme, "). Only android.resource is supported."), null, 1004);
            }
            String path = uriNormalizeScheme.getPath();
            path.getClass();
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            String packageName = TextUtils.isEmpty(uriNormalizeScheme.getHost()) ? context.getPackageName() : uriNormalizeScheme.getHost();
            if (packageName.equals(context.getPackageName())) {
                resourcesForApplication = context.getResources();
            } else {
                try {
                    resourcesForApplication = context.getPackageManager().getResourcesForApplication(packageName);
                } catch (PackageManager.NameNotFoundException e10) {
                    throw new zzhw("Package in android.resource:// URI not found. Check http://g.co/dev/packagevisibility.", e10, PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND);
                }
            }
            if (path.matches("\\d+")) {
                try {
                    identifier = Integer.parseInt(path);
                } catch (NumberFormatException unused2) {
                    throw new zzhw("Resource identifier must be an integer.", null, 1004);
                }
            } else {
                identifier = resourcesForApplication.getIdentifier(defpackage.e.p(new StringBuilder(packageName.length() + 1 + path.length()), packageName, ":", path), "raw", null);
                if (identifier == 0) {
                    throw new zzhw("Resource not found.", null, PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND);
                }
            }
            resources = resourcesForApplication;
        }
        try {
            AssetFileDescriptor assetFileDescriptorOpenRawResourceFd = resources.openRawResourceFd(identifier);
            if (assetFileDescriptorOpenRawResourceFd == null) {
                throw new zzhw("Resource is compressed: ".concat(String.valueOf(uriNormalizeScheme)), null, 2000);
            }
            this.zzc = assetFileDescriptorOpenRawResourceFd;
            long length = assetFileDescriptorOpenRawResourceFd.getLength();
            FileInputStream fileInputStream = new FileInputStream(this.zzc.getFileDescriptor());
            this.zzd = fileInputStream;
            long j10 = dy1Var.f10566c;
            try {
                if (length != -1 && j10 > length) {
                    throw new zzhw(null, null, 2008);
                }
                long startOffset = this.zzc.getStartOffset();
                long jSkip = fileInputStream.skip(startOffset + j10) - startOffset;
                if (jSkip != j10) {
                    throw new zzhw(null, null, 2008);
                }
                if (length == -1) {
                    FileChannel channel = fileInputStream.getChannel();
                    if (channel.size() == 0) {
                        this.f17162e = -1L;
                        size = -1;
                    } else {
                        size = channel.size() - channel.position();
                        this.f17162e = size;
                        if (size < 0) {
                            throw new zzhw(null, null, 2008);
                        }
                    }
                } else {
                    size = length - jSkip;
                    this.f17162e = size;
                    if (size < 0) {
                        throw new zzhc(2008);
                    }
                }
                long j11 = dy1Var.f10567d;
                if (j11 != -1) {
                    this.f17162e = size == -1 ? j11 : Math.min(size, j11);
                }
                this.f17163f = true;
                m(dy1Var);
                return j11 != -1 ? j11 : this.f17162e;
            } catch (zzhw e11) {
                throw e11;
            } catch (IOException e12) {
                throw new zzhw(null, e12, 2000);
            }
        } catch (Resources.NotFoundException e13) {
            throw new zzhw(null, e13, PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND);
        }
    }

    @Override // com.google.android.gms.internal.ads.eh2
    public final int d(byte[] bArr, int i10, int i11) throws zzhw {
        if (i11 == 0) {
            return 0;
        }
        long j10 = this.f17162e;
        if (j10 == 0) {
            return -1;
        }
        if (j10 != -1) {
            try {
                i11 = (int) Math.min(j10, i11);
            } catch (IOException e10) {
                throw new zzhw(null, e10, 2000);
            }
        }
        InputStream inputStream = this.zzd;
        String str = bb1.f9474a;
        int i12 = inputStream.read(bArr, i10, i11);
        if (i12 == -1) {
            if (this.f17162e == -1) {
                return -1;
            }
            throw new zzhw("End of stream reached having not read sufficient data.", new EOFException(), 2000);
        }
        long j11 = this.f17162e;
        if (j11 != -1) {
            this.f17162e = 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() {
        dy1 dy1Var = this.zzb;
        if (dy1Var != null) {
            return dy1Var.f10564a;
        }
        return null;
    }

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