content: package okio;
  
  import java.io.IOException;
  import java.util.zip.Deflater;
  import kotlin.Metadata;
  import kotlin.jvm.internal.Intrinsics;
  
  @Metadata(bv = {1, 0, 3}, d1 = {"\u0000B\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000b\n\u0000\n\u0002\u0010\u0002\n\u0002\b\u0006\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\t\n\u0000\u0018\u00002\u00020\u0001B\u0017\b\u0016\u0012\u0006\u0010\u0002\u001a\u00020\u0001\u0012\u0006\u0010\u0003\u001a\u00020\u0004¢\u0006\u0002\u0010\u0005B\u0017\b\u0000\u0012\u0006\u0010\u0002\u001a\u00020\u0006\u0012\u0006\u0010\u0003\u001a\u00020\u0004¢\u0006\u0002\u0010\u0007J\b\u0010\n\u001a\u00020\u000bH\u0016J\u0010\u0010\f\u001a\u00020\u000b2\u0006\u0010\r\u001a\u00020\tH\u0003J\r\u0010\u000e\u001a\u00020\u000bH\u0000¢\u0006\u0002\b\u000fJ\b\u0010\u0010\u001a\u00020\u000bH\u0016J\b\u0010\u0011\u001a\u00020\u0012H\u0016J\b\u0010\u0013\u001a\u00020\u0014H\u0016J\u0018\u0010\u0015\u001a\u00020\u000b2\u0006\u0010\u0016\u001a\u00020\u00172\u0006\u0010\u0018\u001a\u00020\u0019H\u0016R\u000e\u0010\b\u001a\u00020\tX\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0003\u001a\u00020\u0004X\u0004¢\u0006\u0002\n\u0000R\u000e\u0010\u0002\u001a\u00020\u0006X\u0004¢\u0006\u0002\n\u0000¨\u0006\u001a"}, d2 = {"Lokio/DeflaterSink;", "Lokio/Sink;", "sink", "deflater", "Ljava/util/zip/Deflater;", "(Lokio/Sink;Ljava/util/zip/Deflater;)V", "Lokio/BufferedSink;", "(Lokio/BufferedSink;Ljava/util/zip/Deflater;)V", "closed", "", "close", "", "deflate", "syncFlush", "finishDeflate", "finishDeflate$okio", "flush", "timeout", "Lokio/Timeout;", "toString", "", "write", "source", "Lokio/Buffer;", "byteCount", "", "okio"}, k = 1, mv = {1, 4, 0})
  /* compiled from: DeflaterSink.kt */
  public final class DeflaterSink implements Sink {
      private boolean closed;
      private final Deflater deflater;
      private final BufferedSink sink;
  
      public DeflaterSink(BufferedSink sink2, Deflater deflater2) {
          Intrinsics.checkNotNullParameter(sink2, "sink");
          Intrinsics.checkNotNullParameter(deflater2, "deflater");
          this.sink = sink2;
          this.deflater = deflater2;
      }
  
      /* JADX INFO: this call moved to the top of the method (can break code semantics) */
      public DeflaterSink(Sink sink2, Deflater deflater2) {
          this(Okio.buffer(sink2), deflater2);
          Intrinsics.checkNotNullParameter(sink2, "sink");
          Intrinsics.checkNotNullParameter(deflater2, "deflater");
      }
  
      private final void deflate(boolean syncFlush) {
          Segment writableSegment$okio;
          Buffer buffer = this.sink.getBuffer();
          while (true) {
              writableSegment$okio = buffer.writableSegment$okio(1);
              int deflate = syncFlush ? this.deflater.deflate(writableSegment$okio.data, writableSegment$okio.limit, 8192 - writableSegment$okio.limit, 2) : this.deflater.deflate(writableSegment$okio.data, writableSegment$okio.limit, 8192 - writableSegment$okio.limit);
              if (deflate > 0) {
                  writableSegment$okio.limit += deflate;
                  buffer.setSize$okio(buffer.size() + ((long) deflate));
                  this.sink.emitCompleteSegments();
              } else if (this.deflater.needsInput()) {
                  break;
              }
          }
          if (writableSegment$okio.pos == writableSegment$okio.limit) {
              buffer.head = writableSegment$okio.pop();
              SegmentPool.recycle(writableSegment$okio);
          }
      }
  
      public void close() throws IOException {
          if (!this.closed) {
              Throwable th = null;
              try {
                  finishDeflate$okio();
              } 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) {
                  throw th;
              }
          }
      }
  
      public final void finishDeflate$okio() {
          this.deflater.finish();
          deflate(false);
      }
  
      public void flush() throws IOException {
          deflate(true);
          this.sink.flush();
      }
  
      public Timeout timeout() {
          return this.sink.timeout();
      }
  
      public String toString() {
          return "DeflaterSink(" + this.sink + ')';
      }
  
      public void write(Buffer source, long byteCount) throws IOException {
          Intrinsics.checkNotNullParameter(source, "source");
          Util.checkOffsetAndCount(source.size(), 0, byteCount);
          long j = byteCount;
          while (j > 0) {
              Segment segment = source.head;
              Intrinsics.checkNotNull(segment);
              int min = (int) Math.min(j, (long) (segment.limit - segment.pos));
              this.deflater.setInput(segment.data, segment.pos, min);
              deflate(false);
              source.setSize$okio(source.size() - ((long) min));
              segment.pos += min;
              if (segment.pos == segment.limit) {
                  source.head = segment.pop();
                  SegmentPool.recycle(segment);
              }
              j -= (long) min;
          }
      }
  }
  