jimu-decompiled/sources/okio/InflaterSource.java
2025-05-13 19:24:51 +02:00

113 lines
3.5 KiB
Java

package okio;
import java.io.EOFException;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
/* loaded from: classes2.dex */
public final class InflaterSource implements Source {
private int bufferBytesHeldByInflater;
private boolean closed;
private final Inflater inflater;
private final BufferedSource source;
public InflaterSource(Source source, Inflater inflater) {
this(Okio.buffer(source), inflater);
}
private void releaseInflatedBytes() throws IOException {
int i = this.bufferBytesHeldByInflater;
if (i == 0) {
return;
}
int remaining = i - this.inflater.getRemaining();
this.bufferBytesHeldByInflater -= remaining;
this.source.skip(remaining);
}
@Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
if (this.closed) {
return;
}
this.inflater.end();
this.closed = true;
this.source.close();
}
@Override // okio.Source
public long read(Buffer buffer, long j) throws IOException {
boolean refill;
if (j < 0) {
throw new IllegalArgumentException("byteCount < 0: " + j);
}
if (this.closed) {
throw new IllegalStateException("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 boolean refill() throws IOException {
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;
this.bufferBytesHeldByInflater = i - i2;
this.inflater.setInput(segment.data, i2, this.bufferBytesHeldByInflater);
return false;
}
@Override // okio.Source
public Timeout timeout() {
return this.source.timeout();
}
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;
}
}