package z0;

import android.net.Uri;
import android.util.Base64;
import d5.AbstractC0844a;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import u0.F;
import x0.AbstractC1709a;

/* JADX INFO: loaded from: classes.dex */
public final class f extends AbstractC1779c {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public k f19126u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public byte[] f19127v;

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

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

    @Override // z0.h
    public final void close() {
        if (this.f19127v != null) {
            this.f19127v = null;
            j();
        }
        this.f19126u = null;
    }

    @Override // z0.h
    public final Uri e() {
        k kVar = this.f19126u;
        if (kVar != null) {
            return kVar.f19138a;
        }
        return null;
    }

    @Override // z0.h
    public final long g(k kVar) throws F, i {
        l();
        this.f19126u = kVar;
        Uri uriNormalizeScheme = kVar.f19138a.normalizeScheme();
        String scheme = uriNormalizeScheme.getScheme();
        AbstractC1709a.c("Unsupported scheme: " + scheme, "data".equals(scheme));
        String schemeSpecificPart = uriNormalizeScheme.getSchemeSpecificPart();
        int i = x0.v.f18553a;
        String[] strArrSplit = schemeSpecificPart.split(",", -1);
        if (strArrSplit.length != 2) {
            throw new F("Unexpected URI format: " + uriNormalizeScheme, null, true, 0);
        }
        String str = strArrSplit[1];
        if (strArrSplit[0].contains(";base64")) {
            try {
                this.f19127v = Base64.decode(str, 0);
            } catch (IllegalArgumentException e8) {
                throw new F(AbstractC0844a.x("Error while parsing Base64 encoded string: ", str), e8, true, 0);
            }
        } else {
            this.f19127v = URLDecoder.decode(str, StandardCharsets.US_ASCII.name()).getBytes(StandardCharsets.UTF_8);
        }
        byte[] bArr = this.f19127v;
        long length = bArr.length;
        long j8 = kVar.f19142e;
        if (j8 > length) {
            this.f19127v = null;
            throw new i(2008);
        }
        int i5 = (int) j8;
        this.f19128w = i5;
        int length2 = bArr.length - i5;
        this.f19129x = length2;
        long j9 = kVar.f19143f;
        if (j9 != -1) {
            this.f19129x = (int) Math.min(length2, j9);
        }
        r(kVar);
        return j9 != -1 ? j9 : this.f19129x;
    }

    @Override // u0.InterfaceC1571g
    public final int p(byte[] bArr, int i, int i5) {
        if (i5 == 0) {
            return 0;
        }
        int i8 = this.f19129x;
        if (i8 == 0) {
            return -1;
        }
        int iMin = Math.min(i5, i8);
        byte[] bArr2 = this.f19127v;
        int i9 = x0.v.f18553a;
        System.arraycopy(bArr2, this.f19128w, bArr, i, iMin);
        this.f19128w += iMin;
        this.f19129x -= iMin;
        h(iMin);
        return iMin;
    }
}
