package okhttp3.internal.ws; import java.io.Closeable; import java.io.IOException; import java.util.zip.Deflater; import kotlin.Metadata; import kotlin.io.CloseableKt; import kotlin.jvm.internal.Intrinsics; import okio.Buffer; import okio.ByteString; import okio.DeflaterSink; import okio.Sink; /* compiled from: MessageDeflater.kt */ @Metadata(bv = {1, 0, 3}, d1 = {"\u00002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0000\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\b\u0010\u000b\u001a\u00020\fH\u0016J\u000e\u0010\r\u001a\u00020\f2\u0006\u0010\u000e\u001a\u00020\u0006J\u0014\u0010\u000f\u001a\u00020\u0003*\u00020\u00062\u0006\u0010\u0010\u001a\u00020\u0011H\u0002R\u000e\u0010\u0005\u001a\u00020\u0006X\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\bX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\nX\u0082\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u0012"}, d2 = {"Lokhttp3/internal/ws/MessageDeflater;", "Ljava/io/Closeable;", "noContextTakeover", "", "(Z)V", "deflatedBytes", "Lokio/Buffer;", "deflater", "Ljava/util/zip/Deflater;", "deflaterSink", "Lokio/DeflaterSink;", "close", "", "deflate", "buffer", "endsWith", "suffix", "Lokio/ByteString;", "okhttp"}, k = 1, mv = {1, 4, 0}) /* loaded from: classes3.dex */ public final class MessageDeflater implements Closeable { private final Buffer deflatedBytes; private final Deflater deflater; private final DeflaterSink deflaterSink; private final boolean noContextTakeover; public MessageDeflater(boolean z) { this.noContextTakeover = z; Buffer buffer = new Buffer(); this.deflatedBytes = buffer; Deflater deflater = new Deflater(-1, true); this.deflater = deflater; this.deflaterSink = new DeflaterSink((Sink) buffer, deflater); } public final void deflate(Buffer buffer) throws IOException { ByteString byteString; Intrinsics.checkNotNullParameter(buffer, "buffer"); if (!(this.deflatedBytes.size() == 0)) { throw new IllegalArgumentException("Failed requirement.".toString()); } if (this.noContextTakeover) { this.deflater.reset(); } this.deflaterSink.write(buffer, buffer.size()); this.deflaterSink.flush(); Buffer buffer2 = this.deflatedBytes; byteString = MessageDeflaterKt.EMPTY_DEFLATE_BLOCK; if (endsWith(buffer2, byteString)) { long size = this.deflatedBytes.size() - 4; Buffer.UnsafeCursor readAndWriteUnsafe$default = Buffer.readAndWriteUnsafe$default(this.deflatedBytes, null, 1, null); try { readAndWriteUnsafe$default.resizeBuffer(size); CloseableKt.closeFinally(readAndWriteUnsafe$default, null); } finally { } } else { this.deflatedBytes.writeByte(0); } Buffer buffer3 = this.deflatedBytes; buffer.write(buffer3, buffer3.size()); } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { this.deflaterSink.close(); } private final boolean endsWith(Buffer buffer, ByteString byteString) { return buffer.rangeEquals(buffer.size() - byteString.size(), byteString); } }