package okio; import java.util.zip.CRC32; import java.util.zip.Deflater; /* loaded from: classes5.dex */ public final class GzipSink implements Sink { public boolean closed; public final CRC32 crc = new CRC32(); public final Deflater deflater; public final DeflaterSink deflaterSink; public final BufferedSink sink; public GzipSink(Sink sink) { if (sink == null) { throw new IllegalArgumentException("sink == null"); } Deflater deflater = new Deflater(-1, true); this.deflater = deflater; BufferedSink buffer = Okio.buffer(sink); this.sink = buffer; this.deflaterSink = new DeflaterSink(buffer, deflater); writeHeader(); } @Override // okio.Sink public void write(Buffer buffer, long j) { if (j < 0) { throw new IllegalArgumentException("byteCount < 0: " + j); } if (j == 0) { return; } updateCrc(buffer, j); this.deflaterSink.write(buffer, j); } @Override // okio.Sink, java.io.Flushable public void flush() { this.deflaterSink.flush(); } @Override // okio.Sink public Timeout timeout() { return this.sink.timeout(); } @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.closed) { return; } try { this.deflaterSink.finishDeflate(); writeFooter(); th = null; } catch (Throwable th) { th = th; } try { this.deflater.end(); } catch (Throwable th2) { if (th == null) { th = th2; } } try { this.sink.close(); } catch (Throwable th3) { if (th == null) { th = th3; } } this.closed = true; if (th != null) { Util.sneakyRethrow(th); } } public final 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); } public final void writeFooter() { this.sink.writeIntLe((int) this.crc.getValue()); this.sink.writeIntLe((int) this.deflater.getBytesRead()); } public final 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; } } }