package z0;

import android.content.Context;
import android.content.res.AssetManager;
import android.net.Uri;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: renamed from: z0.b, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1778b extends AbstractC1779c {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final AssetManager f19111u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public Uri f19112v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public InputStream f19113w;

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

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

    public C1778b(Context context) {
        super(false);
        this.f19111u = context.getAssets();
    }

    @Override // z0.h
    public final void close() {
        this.f19112v = null;
        try {
            try {
                InputStream inputStream = this.f19113w;
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e8) {
                throw new C1777a(e8, 2000);
            }
        } finally {
            this.f19113w = null;
            if (this.f19115y) {
                this.f19115y = false;
                j();
            }
        }
    }

    @Override // z0.h
    public final Uri e() {
        return this.f19112v;
    }

    @Override // z0.h
    public final long g(k kVar) throws C1777a {
        try {
            Uri uri = kVar.f19138a;
            long j8 = kVar.f19142e;
            this.f19112v = uri;
            String path = uri.getPath();
            path.getClass();
            if (path.startsWith("/android_asset/")) {
                path = path.substring(15);
            } else if (path.startsWith("/")) {
                path = path.substring(1);
            }
            l();
            InputStream inputStreamOpen = this.f19111u.open(path, 1);
            this.f19113w = inputStreamOpen;
            if (inputStreamOpen.skip(j8) < j8) {
                throw new C1777a(null, 2008);
            }
            long j9 = kVar.f19143f;
            if (j9 != -1) {
                this.f19114x = j9;
            } else {
                long jAvailable = this.f19113w.available();
                this.f19114x = jAvailable;
                if (jAvailable == 2147483647L) {
                    this.f19114x = -1L;
                }
            }
            this.f19115y = true;
            r(kVar);
            return this.f19114x;
        } catch (C1777a e8) {
            throw e8;
        } catch (IOException e9) {
            throw new C1777a(e9, e9 instanceof FileNotFoundException ? 2005 : 2000);
        }
    }

    @Override // u0.InterfaceC1571g
    public final int p(byte[] bArr, int i, int i5) throws C1777a {
        if (i5 == 0) {
            return 0;
        }
        long j8 = this.f19114x;
        if (j8 != 0) {
            if (j8 != -1) {
                try {
                    i5 = (int) Math.min(j8, i5);
                } catch (IOException e8) {
                    throw new C1777a(e8, 2000);
                }
            }
            InputStream inputStream = this.f19113w;
            int i8 = x0.v.f18553a;
            int i9 = inputStream.read(bArr, i, i5);
            if (i9 != -1) {
                long j9 = this.f19114x;
                if (j9 != -1) {
                    this.f19114x = j9 - ((long) i9);
                }
                h(i9);
                return i9;
            }
        }
        return -1;
    }
}
