package com.squareup.picasso; import com.ubt.jimu.base.util.FileUtil; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; /* loaded from: classes.dex */ final class MarkableInputStream extends InputStream { private final InputStream a; private long b; private long c; private long d; private long e; public MarkableInputStream(InputStream inputStream) { this(inputStream, FileUtil.ZIP_BUFFER_SIZE); } private void b(long j) { try { if (this.c >= this.b || this.b > this.d) { this.c = this.b; this.a.mark((int) (j - this.b)); } else { this.a.reset(); this.a.mark((int) (j - this.c)); a(this.c, this.b); } this.d = j; } catch (IOException e) { throw new IllegalStateException("Unable to mark: " + e); } } public long a(int i) { long j = this.b + i; if (this.d < j) { b(j); } return this.b; } @Override // java.io.InputStream public int available() throws IOException { return this.a.available(); } @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { this.a.close(); } @Override // java.io.InputStream public void mark(int i) { this.e = a(i); } @Override // java.io.InputStream public boolean markSupported() { return this.a.markSupported(); } @Override // java.io.InputStream public int read() throws IOException { int read = this.a.read(); if (read != -1) { this.b++; } return read; } @Override // java.io.InputStream public void reset() throws IOException { a(this.e); } @Override // java.io.InputStream public long skip(long j) throws IOException { long skip = this.a.skip(j); this.b += skip; return skip; } public MarkableInputStream(InputStream inputStream, int i) { this.e = -1L; this.a = inputStream.markSupported() ? inputStream : new BufferedInputStream(inputStream, i); } @Override // java.io.InputStream public int read(byte[] bArr) throws IOException { int read = this.a.read(bArr); if (read != -1) { this.b += read; } return read; } public void a(long j) throws IOException { if (this.b <= this.d && j >= this.c) { this.a.reset(); a(this.c, j); this.b = j; return; } throw new IOException("Cannot reset"); } @Override // java.io.InputStream public int read(byte[] bArr, int i, int i2) throws IOException { int read = this.a.read(bArr, i, i2); if (read != -1) { this.b += read; } return read; } private void a(long j, long j2) throws IOException { while (j < j2) { long skip = this.a.skip(j2 - j); if (skip == 0) { if (read() == -1) { return; } else { skip = 1; } } j += skip; } } }