package a0;

import android.content.Context;
import android.content.res.AssetManager;
import b0.D;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: loaded from: classes.dex */
public final class c implements x {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final AssetManager f1922a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final w f1923b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private String f1924c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private InputStream f1925d;

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

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

    public static final class a extends IOException {
        public a(IOException iOException) {
            super(iOException);
        }
    }

    public c(Context context, w wVar) {
        this.f1922a = context.getAssets();
        this.f1923b = wVar;
    }

    @Override // a0.f
    public long a(h hVar) throws a {
        try {
            this.f1924c = hVar.f1940a.toString();
            String path = hVar.f1940a.getPath();
            if (path.startsWith("/android_asset/")) {
                path = path.substring(15);
            } else if (path.startsWith("/")) {
                path = path.substring(1);
            }
            this.f1924c = hVar.f1940a.toString();
            InputStream inputStreamOpen = this.f1922a.open(path, 1);
            this.f1925d = inputStreamOpen;
            if (inputStreamOpen.skip(hVar.f1943d) < hVar.f1943d) {
                throw new EOFException();
            }
            long j2 = hVar.f1944e;
            if (j2 != -1) {
                this.f1926e = j2;
            } else {
                long jAvailable = this.f1925d.available();
                this.f1926e = jAvailable;
                if (jAvailable == 2147483647L) {
                    this.f1926e = -1L;
                }
            }
            this.f1927f = true;
            w wVar = this.f1923b;
            if (wVar != null) {
                wVar.b();
            }
            return this.f1926e;
        } catch (IOException e2) {
            D.h(this.f1925d);
            throw new a(e2);
        }
    }

    @Override // a0.f
    public void close() {
        this.f1924c = null;
        InputStream inputStream = this.f1925d;
        if (inputStream != null) {
            try {
                try {
                    inputStream.close();
                } catch (IOException e2) {
                    throw new a(e2);
                }
            } finally {
                this.f1925d = null;
                if (this.f1927f) {
                    this.f1927f = false;
                    w wVar = this.f1923b;
                    if (wVar != null) {
                        wVar.a();
                    }
                }
            }
        }
    }

    @Override // a0.x
    public String getUri() {
        return this.f1924c;
    }

    @Override // a0.f
    public int read(byte[] bArr, int i2, int i3) throws a {
        long j2 = this.f1926e;
        if (j2 == 0) {
            return -1;
        }
        if (j2 != -1) {
            try {
                i3 = (int) Math.min(j2, i3);
            } catch (IOException e2) {
                throw new a(e2);
            }
        }
        int i4 = this.f1925d.read(bArr, i2, i3);
        if (i4 > 0) {
            long j3 = this.f1926e;
            if (j3 != -1) {
                this.f1926e = j3 - ((long) i4);
            }
            w wVar = this.f1923b;
            if (wVar != null) {
                wVar.c(i4);
            }
        }
        return i4;
    }
}
