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

108 lines
2.8 KiB
Java

package okio;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.Deflater;
/* loaded from: classes2.dex */
public final class GzipSink implements Sink {
private boolean closed;
private final CRC32 crc = new CRC32();
private final Deflater deflater;
private final DeflaterSink deflaterSink;
private final BufferedSink sink;
public GzipSink(Sink sink) {
if (sink == null) {
throw new IllegalArgumentException("sink == null");
}
this.deflater = new Deflater(-1, true);
this.sink = Okio.buffer(sink);
this.deflaterSink = new DeflaterSink(this.sink, this.deflater);
writeHeader();
}
private void updateCrc(Buffer buffer, long j) {
Segment segment = buffer.head;
while (j > 0) {
int min = (int) Math.min(j, segment.limit - segment.pos);
this.crc.update(segment.data, segment.pos, min);
j -= min;
segment = segment.next;
}
}
private void writeFooter() throws IOException {
this.sink.writeIntLe((int) this.crc.getValue());
this.sink.writeIntLe((int) this.deflater.getBytesRead());
}
private void writeHeader() {
Buffer buffer = this.sink.buffer();
buffer.writeShort(8075);
buffer.writeByte(8);
buffer.writeByte(0);
buffer.writeInt(0);
buffer.writeByte(0);
buffer.writeByte(0);
}
@Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
if (this.closed) {
return;
}
Throwable th = null;
try {
this.deflaterSink.finishDeflate();
writeFooter();
} catch (Throwable th2) {
th = th2;
}
try {
this.deflater.end();
} catch (Throwable th3) {
if (th == null) {
th = th3;
}
}
try {
this.sink.close();
} catch (Throwable th4) {
if (th == null) {
th = th4;
}
}
this.closed = true;
if (th != null) {
Util.sneakyRethrow(th);
}
}
public Deflater deflater() {
return this.deflater;
}
@Override // okio.Sink, java.io.Flushable
public void flush() throws IOException {
this.deflaterSink.flush();
}
@Override // okio.Sink
public Timeout timeout() {
return this.sink.timeout();
}
@Override // okio.Sink
public void write(Buffer buffer, long j) throws IOException {
if (j < 0) {
throw new IllegalArgumentException("byteCount < 0: " + j);
}
if (j == 0) {
return;
}
updateCrc(buffer, j);
this.deflaterSink.write(buffer, j);
}
}