package o6; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; /* loaded from: classes2.dex */ public class v extends FilterInputStream { /* renamed from: a, reason: collision with root package name */ private volatile byte[] f12792a; /* renamed from: b, reason: collision with root package name */ private int f12793b; /* renamed from: c, reason: collision with root package name */ private int f12794c; /* renamed from: d, reason: collision with root package name */ private int f12795d; /* renamed from: e, reason: collision with root package name */ private int f12796e; /* renamed from: f, reason: collision with root package name */ private final i6.a f12797f; static class a extends IOException { a(String str) { super(str); } } public v(InputStream inputStream, i6.a aVar) { this(inputStream, aVar, 65536); } private int c(InputStream inputStream, byte[] bArr) { int i10 = this.f12795d; if (i10 != -1) { int i11 = this.f12796e - i10; int i12 = this.f12794c; if (i11 < i12) { if (i10 == 0 && i12 > bArr.length && this.f12793b == bArr.length) { int length = bArr.length * 2; if (length <= i12) { i12 = length; } byte[] bArr2 = (byte[]) this.f12797f.e(i12, byte[].class); System.arraycopy(bArr, 0, bArr2, 0, bArr.length); this.f12792a = bArr2; this.f12797f.d(bArr); bArr = bArr2; } else if (i10 > 0) { System.arraycopy(bArr, i10, bArr, 0, bArr.length - i10); } int i13 = this.f12796e - this.f12795d; this.f12796e = i13; this.f12795d = 0; this.f12793b = 0; int read = inputStream.read(bArr, i13, bArr.length - i13); int i14 = this.f12796e; if (read > 0) { i14 += read; } this.f12793b = i14; return read; } } int read2 = inputStream.read(bArr); if (read2 > 0) { this.f12795d = -1; this.f12796e = 0; this.f12793b = read2; } return read2; } private static IOException z() { throw new IOException("BufferedInputStream is closed"); } public synchronized void G() { this.f12794c = this.f12792a.length; } @Override // java.io.FilterInputStream, java.io.InputStream public synchronized int available() { InputStream inputStream; inputStream = ((FilterInputStream) this).in; if (this.f12792a == null || inputStream == null) { z(); throw null; } return (this.f12793b - this.f12796e) + inputStream.available(); } @Override // java.io.FilterInputStream, java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.f12792a != null) { this.f12797f.d(this.f12792a); this.f12792a = null; } InputStream inputStream = ((FilterInputStream) this).in; ((FilterInputStream) this).in = null; if (inputStream != null) { inputStream.close(); } } @Override // java.io.FilterInputStream, java.io.InputStream public synchronized void mark(int i10) { this.f12794c = Math.max(this.f12794c, i10); this.f12795d = this.f12796e; } @Override // java.io.FilterInputStream, java.io.InputStream public boolean markSupported() { return true; } @Override // java.io.FilterInputStream, java.io.InputStream public synchronized int read() { byte[] bArr = this.f12792a; InputStream inputStream = ((FilterInputStream) this).in; if (bArr == null || inputStream == null) { z(); throw null; } if (this.f12796e >= this.f12793b && c(inputStream, bArr) == -1) { return -1; } if (bArr != this.f12792a && (bArr = this.f12792a) == null) { z(); throw null; } int i10 = this.f12793b; int i11 = this.f12796e; if (i10 - i11 <= 0) { return -1; } this.f12796e = i11 + 1; return bArr[i11] & 255; } public synchronized void release() { if (this.f12792a != null) { this.f12797f.d(this.f12792a); this.f12792a = null; } } @Override // java.io.FilterInputStream, java.io.InputStream public synchronized void reset() { if (this.f12792a == null) { throw new IOException("Stream is closed"); } int i10 = this.f12795d; if (-1 == i10) { throw new a("Mark has been invalidated, pos: " + this.f12796e + " markLimit: " + this.f12794c); } this.f12796e = i10; } @Override // java.io.FilterInputStream, java.io.InputStream public synchronized long skip(long j10) { if (j10 < 1) { return 0L; } byte[] bArr = this.f12792a; if (bArr == null) { z(); throw null; } InputStream inputStream = ((FilterInputStream) this).in; if (inputStream == null) { z(); throw null; } int i10 = this.f12793b; int i11 = this.f12796e; if (i10 - i11 >= j10) { this.f12796e = (int) (i11 + j10); return j10; } long j11 = i10 - i11; this.f12796e = i10; if (this.f12795d == -1 || j10 > this.f12794c) { long skip = inputStream.skip(j10 - j11); if (skip > 0) { this.f12795d = -1; } return j11 + skip; } if (c(inputStream, bArr) == -1) { return j11; } int i12 = this.f12793b; int i13 = this.f12796e; if (i12 - i13 >= j10 - j11) { this.f12796e = (int) ((i13 + j10) - j11); return j10; } long j12 = (j11 + i12) - i13; this.f12796e = i12; return j12; } v(InputStream inputStream, i6.a aVar, int i10) { super(inputStream); this.f12795d = -1; this.f12797f = aVar; this.f12792a = (byte[]) aVar.e(i10, byte[].class); } @Override // java.io.FilterInputStream, java.io.InputStream public synchronized int read(byte[] bArr, int i10, int i11) { int i12; int i13; byte[] bArr2 = this.f12792a; if (bArr2 == null) { z(); throw null; } if (i11 == 0) { return 0; } InputStream inputStream = ((FilterInputStream) this).in; if (inputStream == null) { z(); throw null; } int i14 = this.f12796e; int i15 = this.f12793b; if (i14 < i15) { int i16 = i15 - i14; if (i16 >= i11) { i16 = i11; } System.arraycopy(bArr2, i14, bArr, i10, i16); this.f12796e += i16; if (i16 == i11 || inputStream.available() == 0) { return i16; } i10 += i16; i12 = i11 - i16; } else { i12 = i11; } while (true) { if (this.f12795d == -1 && i12 >= bArr2.length) { i13 = inputStream.read(bArr, i10, i12); if (i13 == -1) { return i12 != i11 ? i11 - i12 : -1; } } else { if (c(inputStream, bArr2) == -1) { return i12 != i11 ? i11 - i12 : -1; } if (bArr2 != this.f12792a && (bArr2 = this.f12792a) == null) { z(); throw null; } int i17 = this.f12793b; int i18 = this.f12796e; i13 = i17 - i18; if (i13 >= i12) { i13 = i12; } System.arraycopy(bArr2, i18, bArr, i10, i13); this.f12796e += i13; } i12 -= i13; if (i12 == 0) { return i11; } if (inputStream.available() == 0) { return i11 - i12; } i10 += i13; } } }