package com.applovin.impl; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.content.res.Resources; import android.net.Uri; import android.text.TextUtils; import com.facebook.internal.security.CertificateUtil; import com.mbridge.msdk.playercommon.exoplayer2.upstream.RawResourceDataSource; import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.channels.FileChannel; /* loaded from: classes.dex */ public final class li extends a2 { private final Resources e; private final String f; private Uri g; private AssetFileDescriptor h; private InputStream i; private long j; private boolean k; @Override // com.applovin.impl.i5 public Uri c() { return this.g; } public static class a extends j5 { public a(String str, Throwable th, int i) { super(str, th, i); } } public li(Context context) { super(false); this.e = context.getResources(); this.f = context.getPackageName(); } @Override // com.applovin.impl.i5 public long a(l5 l5Var) { int parseInt; String str; Uri uri = l5Var.a; this.g = uri; if (!TextUtils.equals(RawResourceDataSource.RAW_RESOURCE_SCHEME, uri.getScheme()) && (!TextUtils.equals("android.resource", uri.getScheme()) || uri.getPathSegments().size() != 1 || !((String) b1.a((Object) uri.getLastPathSegment())).matches("\\d+"))) { if (TextUtils.equals("android.resource", uri.getScheme())) { String str2 = (String) b1.a((Object) uri.getPath()); if (str2.startsWith("/")) { str2 = str2.substring(1); } String host = uri.getHost(); StringBuilder sb = new StringBuilder(); if (TextUtils.isEmpty(host)) { str = ""; } else { str = host + CertificateUtil.DELIMITER; } sb.append(str); sb.append(str2); parseInt = this.e.getIdentifier(sb.toString(), "raw", this.f); if (parseInt == 0) { throw new a("Resource not found.", null, 2005); } } else { throw new a("URI must either use scheme rawresource or android.resource", null, 1004); } } else { try { parseInt = Integer.parseInt((String) b1.a((Object) uri.getLastPathSegment())); } catch (NumberFormatException unused) { throw new a("Resource identifier must be an integer.", null, 1004); } } b(l5Var); try { AssetFileDescriptor openRawResourceFd = this.e.openRawResourceFd(parseInt); this.h = openRawResourceFd; if (openRawResourceFd != null) { long length = openRawResourceFd.getLength(); FileInputStream fileInputStream = new FileInputStream(openRawResourceFd.getFileDescriptor()); this.i = fileInputStream; if (length != -1) { try { if (l5Var.g > length) { throw new a(null, null, 2008); } } catch (a e) { throw e; } catch (IOException e2) { throw new a(null, e2, 2000); } } long startOffset = openRawResourceFd.getStartOffset(); long skip = fileInputStream.skip(l5Var.g + startOffset) - startOffset; if (skip == l5Var.g) { if (length == -1) { FileChannel channel = fileInputStream.getChannel(); if (channel.size() == 0) { this.j = -1L; } else { long size = channel.size() - channel.position(); this.j = size; if (size < 0) { throw new a(null, null, 2008); } } } else { long j = length - skip; this.j = j; if (j < 0) { throw new j5(2008); } } long j2 = l5Var.h; if (j2 != -1) { long j3 = this.j; if (j3 != -1) { j2 = Math.min(j3, j2); } this.j = j2; } this.k = true; c(l5Var); long j4 = l5Var.h; return j4 != -1 ? j4 : this.j; } throw new a(null, null, 2008); } throw new a("Resource is compressed: " + uri, null, 2000); } catch (Resources.NotFoundException e3) { throw new a(null, e3, 2005); } } @Override // com.applovin.impl.i5 public void close() { this.g = null; try { try { InputStream inputStream = this.i; if (inputStream != null) { inputStream.close(); } this.i = null; try { try { AssetFileDescriptor assetFileDescriptor = this.h; if (assetFileDescriptor != null) { assetFileDescriptor.close(); } } catch (IOException e) { throw new a(null, e, 2000); } } finally { this.h = null; if (this.k) { this.k = false; g(); } } } catch (IOException e2) { throw new a(null, e2, 2000); } } catch (Throwable th) { this.i = null; try { try { AssetFileDescriptor assetFileDescriptor2 = this.h; if (assetFileDescriptor2 != null) { assetFileDescriptor2.close(); } this.h = null; if (this.k) { this.k = false; g(); } throw th; } catch (IOException e3) { throw new a(null, e3, 2000); } } finally { this.h = null; if (this.k) { this.k = false; g(); } } } } @Override // com.applovin.impl.g5 public int a(byte[] bArr, int i, int i2) { if (i2 == 0) { return 0; } long j = this.j; if (j == 0) { return -1; } if (j != -1) { try { i2 = (int) Math.min(j, i2); } catch (IOException e) { throw new a(null, e, 2000); } } int read = ((InputStream) xp.a((Object) this.i)).read(bArr, i, i2); if (read == -1) { if (this.j == -1) { return -1; } throw new a("End of stream reached having not read sufficient data.", new EOFException(), 2000); } long j2 = this.j; if (j2 != -1) { this.j = j2 - read; } d(read); return read; } }