jimu-decompiled/sources/okio/Timeout.java
2025-05-13 19:24:51 +02:00

118 lines
3.5 KiB
Java

package okio;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.TimeUnit;
/* loaded from: classes2.dex */
public class Timeout {
public static final Timeout NONE = new Timeout() { // from class: okio.Timeout.1
@Override // okio.Timeout
public Timeout deadlineNanoTime(long j) {
return this;
}
@Override // okio.Timeout
public void throwIfReached() throws IOException {
}
@Override // okio.Timeout
public Timeout timeout(long j, TimeUnit timeUnit) {
return this;
}
};
private long deadlineNanoTime;
private boolean hasDeadline;
private long timeoutNanos;
public Timeout clearDeadline() {
this.hasDeadline = false;
return this;
}
public Timeout clearTimeout() {
this.timeoutNanos = 0L;
return this;
}
public final Timeout deadline(long j, TimeUnit timeUnit) {
if (j > 0) {
if (timeUnit != null) {
return deadlineNanoTime(System.nanoTime() + timeUnit.toNanos(j));
}
throw new IllegalArgumentException("unit == null");
}
throw new IllegalArgumentException("duration <= 0: " + j);
}
public long deadlineNanoTime() {
if (this.hasDeadline) {
return this.deadlineNanoTime;
}
throw new IllegalStateException("No deadline");
}
public boolean hasDeadline() {
return this.hasDeadline;
}
public void throwIfReached() throws IOException {
if (Thread.interrupted()) {
throw new InterruptedIOException("thread interrupted");
}
if (this.hasDeadline && this.deadlineNanoTime - System.nanoTime() <= 0) {
throw new InterruptedIOException("deadline reached");
}
}
public Timeout timeout(long j, TimeUnit timeUnit) {
if (j >= 0) {
if (timeUnit == null) {
throw new IllegalArgumentException("unit == null");
}
this.timeoutNanos = timeUnit.toNanos(j);
return this;
}
throw new IllegalArgumentException("timeout < 0: " + j);
}
public long timeoutNanos() {
return this.timeoutNanos;
}
public final void waitUntilNotified(Object obj) throws InterruptedIOException {
try {
boolean hasDeadline = hasDeadline();
long timeoutNanos = timeoutNanos();
long j = 0;
if (!hasDeadline && timeoutNanos == 0) {
obj.wait();
return;
}
long nanoTime = System.nanoTime();
if (hasDeadline && timeoutNanos != 0) {
timeoutNanos = Math.min(timeoutNanos, deadlineNanoTime() - nanoTime);
} else if (hasDeadline) {
timeoutNanos = deadlineNanoTime() - nanoTime;
}
if (timeoutNanos > 0) {
long j2 = timeoutNanos / 1000000;
Long.signum(j2);
obj.wait(j2, (int) (timeoutNanos - (1000000 * j2)));
j = System.nanoTime() - nanoTime;
}
if (j >= timeoutNanos) {
throw new InterruptedIOException("timeout");
}
} catch (InterruptedException unused) {
throw new InterruptedIOException("interrupted");
}
}
public Timeout deadlineNanoTime(long j) {
this.hasDeadline = true;
this.deadlineNanoTime = j;
return this;
}
}