Rabbit-R1/switch port/java/sources/okio/Throttler.java

156 lines
7.1 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package okio;
import io.sentry.protocol.TransactionInfo;
import java.io.IOException;
import java.io.InterruptedIOException;
import kotlin.Metadata;
import kotlin.Unit;
import kotlin.jvm.internal.Intrinsics;
/* compiled from: Throttler.kt */
@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\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0004X\u0082\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;", TransactionInfo.JsonKeys.SOURCE, "Lokio/Source;", "take", "take$okio", "waitNanos", "nanosToWait", "bytesToNanos", "nanosToBytes", "okio"}, k = 1, mv = {1, 4, 0})
/* loaded from: classes3.dex */
public final class Throttler {
private long allocatedUntil;
private long bytesPerSecond;
private long maxByteCount;
private long waitByteCount;
public final void bytesPerSecond(long j) {
bytesPerSecond$default(this, j, 0L, 0L, 6, null);
}
public final void bytesPerSecond(long j, long j2) {
bytesPerSecond$default(this, j, j2, 0L, 4, null);
}
public Throttler(long j) {
this.allocatedUntil = j;
this.waitByteCount = 8192L;
this.maxByteCount = 262144L;
}
public Throttler() {
this(System.nanoTime());
}
public static /* synthetic */ void bytesPerSecond$default(Throttler throttler, long j, long j2, long j3, int i, Object obj) {
if ((i & 2) != 0) {
j2 = throttler.waitByteCount;
}
long j4 = j2;
if ((i & 4) != 0) {
j3 = throttler.maxByteCount;
}
throttler.bytesPerSecond(j, j4, j3);
}
public final long byteCountOrWaitNanos$okio(long now, long byteCount) {
if (this.bytesPerSecond == 0) {
return byteCount;
}
long max = Math.max(this.allocatedUntil - now, 0L);
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 = now + bytesToNanos(this.maxByteCount);
return nanosToBytes;
}
long min = Math.min(j, byteCount);
long bytesToNanos = max + bytesToNanos(min - this.maxByteCount);
if (bytesToNanos != 0) {
return -bytesToNanos;
}
this.allocatedUntil = now + bytesToNanos(this.maxByteCount);
return min;
}
private final long nanosToBytes(long j) {
return (j * this.bytesPerSecond) / 1000000000;
}
private final long bytesToNanos(long j) {
return (j * 1000000000) / this.bytesPerSecond;
}
private final void waitNanos(long nanosToWait) {
long j = nanosToWait / 1000000;
wait(j, (int) (nanosToWait - (1000000 * j)));
}
public final Source source(final Source source) {
Intrinsics.checkNotNullParameter(source, "source");
return new ForwardingSource(source) { // from class: okio.Throttler$source$1
@Override // okio.ForwardingSource, okio.Source
public long read(Buffer sink, long byteCount) {
Intrinsics.checkNotNullParameter(sink, "sink");
try {
return super.read(sink, Throttler.this.take$okio(byteCount));
} catch (InterruptedException unused) {
Thread.currentThread().interrupt();
throw new InterruptedIOException("interrupted");
}
}
};
}
public final Sink sink(final Sink sink) {
Intrinsics.checkNotNullParameter(sink, "sink");
return new ForwardingSink(sink) { // from class: okio.Throttler$sink$1
@Override // okio.ForwardingSink, okio.Sink
public void write(Buffer source, long byteCount) throws IOException {
Intrinsics.checkNotNullParameter(source, "source");
while (byteCount > 0) {
try {
long take$okio = Throttler.this.take$okio(byteCount);
super.write(source, take$okio);
byteCount -= take$okio;
} catch (InterruptedException unused) {
Thread.currentThread().interrupt();
throw new InterruptedIOException("interrupted");
}
}
}
};
}
public final void bytesPerSecond(long bytesPerSecond, long waitByteCount, long maxByteCount) {
synchronized (this) {
if (!(bytesPerSecond >= 0)) {
throw new IllegalArgumentException("Failed requirement.".toString());
}
if (!(waitByteCount > 0)) {
throw new IllegalArgumentException("Failed requirement.".toString());
}
if (!(maxByteCount >= waitByteCount)) {
throw new IllegalArgumentException("Failed requirement.".toString());
}
this.bytesPerSecond = bytesPerSecond;
this.waitByteCount = waitByteCount;
this.maxByteCount = maxByteCount;
notifyAll();
Unit unit = Unit.INSTANCE;
}
}
public final long take$okio(long byteCount) {
long byteCountOrWaitNanos$okio;
if (!(byteCount > 0)) {
throw new IllegalArgumentException("Failed requirement.".toString());
}
synchronized (this) {
while (true) {
byteCountOrWaitNanos$okio = byteCountOrWaitNanos$okio(System.nanoTime(), byteCount);
if (byteCountOrWaitNanos$okio < 0) {
waitNanos(-byteCountOrWaitNanos$okio);
}
}
}
return byteCountOrWaitNanos$okio;
}
}