package o; import com.drew.lang.BufferBoundsException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; /* loaded from: classes.dex */ public final class WrL extends Pmg { private final InputStream a; private boolean c; private final ArrayList d; private final int e; private long i; public WrL(InputStream inputStream) { this(inputStream, -1L); } public WrL(InputStream inputStream, long j) { this.d = new ArrayList<>(); this.e = 2048; this.a = inputStream; this.i = j; } @Override // o.Pmg public final long a() throws IOException { long j = this.i; if (j != -1) { return j; } b(Integer.MAX_VALUE, 1); return this.i; } @Override // o.Pmg protected final void a(int i, int i2) throws IOException { if (i < 0) { throw new BufferBoundsException(String.format("Attempt to read from buffer using a negative index (%d)", Integer.valueOf(i))); } if (i2 < 0) { throw new BufferBoundsException("Number of requested bytes must be zero or greater"); } if ((i + i2) - 1 > 2147483647L) { throw new BufferBoundsException(String.format("Number of requested bytes summed with starting index exceed maximum range of signed 32 bit integers (requested index: %d, requested count: %d)", Integer.valueOf(i), Integer.valueOf(i2))); } if (!b(i, i2)) { throw new BufferBoundsException(i, i2, this.i); } } @Override // o.Pmg protected final boolean b(int i, int i2) throws IOException { int i3; if (i < 0 || i2 < 0) { return false; } long j = (i + i2) - 1; if (j > 2147483647L) { return false; } int i4 = (int) j; if (this.c) { return ((long) i4) < this.i; } int i5 = i4 / this.e; while (i5 >= this.d.size()) { byte[] bArr = new byte[this.e]; int i6 = 0; while (!this.c && i6 != (i3 = this.e)) { int read = this.a.read(bArr, i6, i3 - i6); if (read == -1) { this.c = true; int size = this.d.size(); int i7 = this.e; if (this.i == -1) { this.i = (size * i7) + i6; } if (i4 >= this.i) { this.d.add(bArr); return false; } } else { i6 += read; } } this.d.add(bArr); } return true; } @Override // o.Pmg public final byte d(int i) throws IOException { int i2 = this.e; return this.d.get(i / i2)[i % i2]; } @Override // o.Pmg public final byte[] c(int i, int i2) throws IOException { a(i, i2); byte[] bArr = new byte[i2]; int i3 = 0; while (i2 != 0) { int i4 = this.e; int i5 = i / i4; int i6 = i % i4; int min = Math.min(i2, i4 - i6); System.arraycopy(this.d.get(i5), i6, bArr, i3, min); i2 -= min; i += min; i3 += min; } return bArr; } }