package okio; import csdk.gluads.Consts; import java.io.EOFException; import java.io.IOException; import java.util.zip.DataFormatException; import java.util.zip.Inflater; /* loaded from: classes5.dex */ public final class InflaterSource implements Source { public int bufferBytesHeldByInflater; public boolean closed; public final Inflater inflater; public final BufferedSource source; public InflaterSource(BufferedSource bufferedSource, Inflater inflater) { if (bufferedSource == null) { throw new IllegalArgumentException("source == null"); } if (inflater == null) { throw new IllegalArgumentException("inflater == null"); } this.source = bufferedSource; this.inflater = inflater; } @Override // okio.Source public long read(Buffer buffer, long j) { boolean refill; if (j < 0) { throw new IllegalArgumentException("byteCount < 0: " + j); } if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } if (j == 0) { return 0L; } do { refill = refill(); try { Segment writableSegment = buffer.writableSegment(1); int inflate = this.inflater.inflate(writableSegment.data, writableSegment.limit, (int) Math.min(j, 8192 - writableSegment.limit)); if (inflate > 0) { writableSegment.limit += inflate; long j2 = inflate; buffer.size += j2; return j2; } if (!this.inflater.finished() && !this.inflater.needsDictionary()) { } releaseInflatedBytes(); if (writableSegment.pos != writableSegment.limit) { return -1L; } buffer.head = writableSegment.pop(); SegmentPool.recycle(writableSegment); return -1L; } catch (DataFormatException e) { throw new IOException(e); } } while (!refill); throw new EOFException("source exhausted prematurely"); } public final boolean refill() { if (!this.inflater.needsInput()) { return false; } releaseInflatedBytes(); if (this.inflater.getRemaining() != 0) { throw new IllegalStateException("?"); } if (this.source.exhausted()) { return true; } Segment segment = this.source.buffer().head; int i = segment.limit; int i2 = segment.pos; int i3 = i - i2; this.bufferBytesHeldByInflater = i3; this.inflater.setInput(segment.data, i2, i3); return false; } public final void releaseInflatedBytes() { int i = this.bufferBytesHeldByInflater; if (i == 0) { return; } int remaining = i - this.inflater.getRemaining(); this.bufferBytesHeldByInflater -= remaining; this.source.skip(remaining); } @Override // okio.Source public Timeout timeout() { return this.source.timeout(); } @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.closed) { return; } this.inflater.end(); this.closed = true; this.source.close(); } }