mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-13 17:37:16 +01:00
188 lines
8.2 KiB
Java
188 lines
8.2 KiB
Java
package okio;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InterruptedIOException;
|
|
import java.util.concurrent.locks.Condition;
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
import kotlin.Metadata;
|
|
import kotlin.Unit;
|
|
import kotlin.jvm.JvmOverloads;
|
|
import org.aspectj.lang.JoinPoint;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import rx.Single$$ExternalSyntheticBUOutline0;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
@Metadata(d1 = {"\u00008\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\t\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\b\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\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\u0011\u001a\u00020\u00042\u0006\u0010\u0012\u001a\u00020\u00042\u0006\u0010\u0013\u001a\u00020\u0004H\u0000¢\u0006\u0002\b\u0014J$\u0010\u0006\u001a\u00020\u00152\u0006\u0010\u0006\u001a\u00020\u00042\b\b\u0002\u0010\u0010\u001a\u00020\u00042\b\b\u0002\u0010\u000f\u001a\u00020\u0004H\u0007J\u000e\u0010\u0016\u001a\u00020\u00172\u0006\u0010\u0016\u001a\u00020\u0017J\u000e\u0010\u0018\u001a\u00020\u00192\u0006\u0010\u0018\u001a\u00020\u0019J\u0015\u0010\u001a\u001a\u00020\u00042\u0006\u0010\u0013\u001a\u00020\u0004H\u0000¢\u0006\u0002\b\u001bJ\f\u0010\u001c\u001a\u00020\u0004*\u00020\u0004H\u0002J\f\u0010\u001d\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\u0011\u0010\u0007\u001a\u00020\b¢\u0006\b\n\u0000\u001a\u0004\b\t\u0010\nR\u0011\u0010\u000b\u001a\u00020\f¢\u0006\b\n\u0000\u001a\u0004\b\r\u0010\u000eR\u000e\u0010\u000f\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000R\u000e\u0010\u0010\u001a\u00020\u0004X\u0082\u000e¢\u0006\u0002\n\u0000¨\u0006\u001e"}, d2 = {"Lokio/Throttler;", "", "()V", "allocatedUntil", "", "(J)V", "bytesPerSecond", "condition", "Ljava/util/concurrent/locks/Condition;", "getCondition", "()Ljava/util/concurrent/locks/Condition;", JoinPoint.SYNCHRONIZATION_LOCK, "Ljava/util/concurrent/locks/ReentrantLock;", "getLock", "()Ljava/util/concurrent/locks/ReentrantLock;", "maxByteCount", "waitByteCount", "byteCountOrWaitNanos", "now", "byteCount", "byteCountOrWaitNanos$okio", "", "sink", "Lokio/Sink;", "source", "Lokio/Source;", "take", "take$okio", "bytesToNanos", "nanosToBytes", "okio"}, k = 1, mv = {1, 9, 0}, xi = 48)
|
|
/* loaded from: classes10.dex */
|
|
public final class Throttler {
|
|
private long allocatedUntil;
|
|
private long bytesPerSecond;
|
|
|
|
@NotNull
|
|
private final Condition condition;
|
|
|
|
@NotNull
|
|
private final ReentrantLock lock;
|
|
private long maxByteCount;
|
|
private long waitByteCount;
|
|
|
|
public Throttler(long j) {
|
|
this.allocatedUntil = j;
|
|
this.waitByteCount = 8192L;
|
|
this.maxByteCount = 262144L;
|
|
ReentrantLock reentrantLock = new ReentrantLock();
|
|
this.lock = reentrantLock;
|
|
Condition newCondition = reentrantLock.newCondition();
|
|
newCondition.getClass();
|
|
this.condition = newCondition;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
private final long bytesToNanos(long j) {
|
|
return (j * 1000000000) / this.bytesPerSecond;
|
|
}
|
|
|
|
private final long nanosToBytes(long j) {
|
|
return (j * this.bytesPerSecond) / 1000000000;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
@JvmOverloads
|
|
public final void bytesPerSecond(long bytesPerSecond, long waitByteCount, long maxByteCount) {
|
|
ReentrantLock reentrantLock = this.lock;
|
|
reentrantLock.lock();
|
|
try {
|
|
if (bytesPerSecond < 0) {
|
|
throw new IllegalArgumentException("Failed requirement.");
|
|
}
|
|
if (waitByteCount <= 0) {
|
|
throw new IllegalArgumentException("Failed requirement.");
|
|
}
|
|
if (maxByteCount < waitByteCount) {
|
|
throw new IllegalArgumentException("Failed requirement.");
|
|
}
|
|
this.bytesPerSecond = bytesPerSecond;
|
|
this.waitByteCount = waitByteCount;
|
|
this.maxByteCount = maxByteCount;
|
|
this.condition.signalAll();
|
|
Unit unit = Unit.INSTANCE;
|
|
} finally {
|
|
reentrantLock.unlock();
|
|
}
|
|
}
|
|
|
|
@NotNull
|
|
public final Condition getCondition() {
|
|
return this.condition;
|
|
}
|
|
|
|
@NotNull
|
|
public final ReentrantLock getLock() {
|
|
return this.lock;
|
|
}
|
|
|
|
@NotNull
|
|
public final Sink sink(@NotNull final Sink sink) {
|
|
sink.getClass();
|
|
return new ForwardingSink(sink) { // from class: okio.Throttler$sink$1
|
|
@Override // okio.ForwardingSink, okio.Sink
|
|
public void write(@NotNull Buffer source, long byteCount) throws IOException {
|
|
source.getClass();
|
|
while (byteCount > 0) {
|
|
try {
|
|
long take$okio = this.take$okio(byteCount);
|
|
super.write(source, take$okio);
|
|
byteCount -= take$okio;
|
|
} catch (InterruptedException unused) {
|
|
Thread.currentThread().interrupt();
|
|
throw new InterruptedIOException("interrupted");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
@NotNull
|
|
public final Source source(@NotNull final Source source) {
|
|
source.getClass();
|
|
return new ForwardingSource(source) { // from class: okio.Throttler$source$1
|
|
@Override // okio.ForwardingSource, okio.Source
|
|
public long read(@NotNull Buffer sink, long byteCount) {
|
|
sink.getClass();
|
|
try {
|
|
return super.read(sink, this.take$okio(byteCount));
|
|
} catch (InterruptedException unused) {
|
|
Thread.currentThread().interrupt();
|
|
throw new InterruptedIOException("interrupted");
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
public final long take$okio(long byteCount) {
|
|
if (byteCount <= 0) {
|
|
Single$$ExternalSyntheticBUOutline0.m$3("Failed requirement.");
|
|
return 0L;
|
|
}
|
|
ReentrantLock reentrantLock = this.lock;
|
|
reentrantLock.lock();
|
|
while (true) {
|
|
try {
|
|
long byteCountOrWaitNanos$okio = byteCountOrWaitNanos$okio(System.nanoTime(), byteCount);
|
|
if (byteCountOrWaitNanos$okio >= 0) {
|
|
return byteCountOrWaitNanos$okio;
|
|
}
|
|
this.condition.awaitNanos(-byteCountOrWaitNanos$okio);
|
|
} finally {
|
|
reentrantLock.unlock();
|
|
}
|
|
}
|
|
}
|
|
|
|
public Throttler() {
|
|
this(System.nanoTime());
|
|
}
|
|
|
|
@JvmOverloads
|
|
public final void bytesPerSecond(long j, long j2) {
|
|
bytesPerSecond$default(this, j, j2, 0L, 4, null);
|
|
}
|
|
|
|
@JvmOverloads
|
|
public final void bytesPerSecond(long j) {
|
|
bytesPerSecond$default(this, j, 0L, 0L, 6, null);
|
|
}
|
|
}
|