content: package okio;
  
  import kotlin.Metadata;
  import kotlin.Unit;
  import kotlin.jvm.internal.Intrinsics;
  
  @Metadata(bv = {1, 0, 3}, d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\b\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0007\u0018\u00002\u00020\u0001B\u0007\b\u0016¢\u0006\u0002\u0010\u0002B\u000f\b\u0000\u0012\u0006\u0010\u0003\u001a\u00020\u0004¢\u0006\u0002\u0010\u0005J\u001d\u0010\t\u001a\u00020\u00042\u0006\u0010\n\u001a\u00020\u00042\u0006\u0010\u000b\u001a\u00020\u0004H\u0000¢\u0006\u0002\b\fJ$\u0010\u0006\u001a\u00020\r2\u0006\u0010\u0006\u001a\u00020\u00042\b\b\u0002\u0010\b\u001a\u00020\u00042\b\b\u0002\u0010\u0007\u001a\u00020\u0004H\u0007J\u000e\u0010\u000e\u001a\u00020\u000f2\u0006\u0010\u000e\u001a\u00020\u000fJ\u000e\u0010\u0010\u001a\u00020\u00112\u0006\u0010\u0010\u001a\u00020\u0011J\u0015\u0010\u0012\u001a\u00020\u00042\u0006\u0010\u000b\u001a\u00020\u0004H\u0000¢\u0006\u0002\b\u0013J\u0010\u0010\u0014\u001a\u00020\r2\u0006\u0010\u0015\u001a\u00020\u0004H\u0002J\f\u0010\u0016\u001a\u00020\u0004*\u00020\u0004H\u0002J\f\u0010\u0017\u001a\u00020\u0004*\u00020\u0004H\u0002R\u000e\u0010\u0003\u001a\u00020\u0004X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0004X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\u0004X\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0004X\u000e¢\u0006\u0002\n\u0000¨\u0006\u0018"}, d2 = {"Lokio/Throttler;", "", "()V", "allocatedUntil", "", "(J)V", "bytesPerSecond", "maxByteCount", "waitByteCount", "byteCountOrWaitNanos", "now", "byteCount", "byteCountOrWaitNanos$okio", "", "sink", "Lokio/Sink;", "source", "Lokio/Source;", "take", "take$okio", "waitNanos", "nanosToWait", "bytesToNanos", "nanosToBytes", "okio"}, k = 1, mv = {1, 4, 0})
  /* compiled from: Throttler.kt */
  public final class Throttler {
      private long allocatedUntil;
      private long bytesPerSecond;
      private long maxByteCount;
      private long waitByteCount;
  
      public Throttler() {
          this(System.nanoTime());
      }
  
      public Throttler(long allocatedUntil2) {
          this.allocatedUntil = allocatedUntil2;
          this.waitByteCount = 8192;
          this.maxByteCount = 262144;
      }
  
      public static /* synthetic */ void bytesPerSecond$default(Throttler throttler, long j, long j2, long j3, int i, Object obj) {
          throttler.bytesPerSecond(j, (i & 2) != 0 ? throttler.waitByteCount : j2, (i & 4) != 0 ? throttler.maxByteCount : j3);
      }
  
      private final long bytesToNanos(long $this$bytesToNanos) {
          return (1000000000 * $this$bytesToNanos) / this.bytesPerSecond;
      }
  
      private final long nanosToBytes(long $this$nanosToBytes) {
          return (this.bytesPerSecond * $this$nanosToBytes) / 1000000000;
      }
  
      private final void waitNanos(long nanosToWait) {
          long j = nanosToWait / 1000000;
          wait(j, (int) (nanosToWait - (1000000 * j)));
      }
  
      public final long byteCountOrWaitNanos$okio(long now, long byteCount) {
          if (this.bytesPerSecond == 0) {
              return byteCount;
          }
          long max = Math.max(this.allocatedUntil - now, 0);
          long nanosToBytes = this.maxByteCount - nanosToBytes(max);
          if (nanosToBytes >= byteCount) {
              this.allocatedUntil = now + max + bytesToNanos(byteCount);
              return byteCount;
          }
          long j = this.waitByteCount;
          if (nanosToBytes >= j) {
              this.allocatedUntil = bytesToNanos(this.maxByteCount) + now;
              return nanosToBytes;
          }
          long min = Math.min(j, byteCount);
          long bytesToNanos = bytesToNanos(min - this.maxByteCount) + max;
          if (bytesToNanos != 0) {
              return -bytesToNanos;
          }
          this.allocatedUntil = bytesToNanos(this.maxByteCount) + now;
          return min;
      }
  
      public final void bytesPerSecond(long j) {
          bytesPerSecond$default(this, j, 0, 0, 6, (Object) null);
      }
  
      public final void bytesPerSecond(long j, long j2) {
          bytesPerSecond$default(this, j, j2, 0, 4, (Object) null);
      }
  
      public final void bytesPerSecond(long bytesPerSecond2, long waitByteCount2, long maxByteCount2) {
          synchronized (this) {
              boolean z = true;
              if (bytesPerSecond2 >= 0) {
                  if (waitByteCount2 > 0) {
                      if (maxByteCount2 < waitByteCount2) {
                          z = false;
                      }
                      if (z) {
                          this.bytesPerSecond = bytesPerSecond2;
                          this.waitByteCount = waitByteCount2;
                          this.maxByteCount = maxByteCount2;
                          notifyAll();
                          Unit unit = Unit.INSTANCE;
                      } else {
                          throw new IllegalArgumentException("Failed requirement.".toString());
                      }
                  } else {
                      throw new IllegalArgumentException("Failed requirement.".toString());
                  }
              } else {
                  throw new IllegalArgumentException("Failed requirement.".toString());
              }
          }
      }
  
      public final Sink sink(Sink sink) {
          Intrinsics.checkNotNullParameter(sink, "sink");
          return new Throttler$sink$1(this, sink, sink);
      }
  
      public final Source source(Source source) {
          Intrinsics.checkNotNullParameter(source, "source");
          return new Throttler$source$1(this, source, source);
      }
  
      public final long take$okio(long byteCount) {
          long byteCountOrWaitNanos$okio;
          if (byteCount > 0) {
              synchronized (this) {
                  while (true) {
                      byteCountOrWaitNanos$okio = byteCountOrWaitNanos$okio(System.nanoTime(), byteCount);
                      if (byteCountOrWaitNanos$okio < 0) {
                          waitNanos(-byteCountOrWaitNanos$okio);
                      }
                  }
              }
              return byteCountOrWaitNanos$okio;
          }
          throw new IllegalArgumentException("Failed requirement.".toString());
      }
  }
  