package h5;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.TimeUnit;
/* loaded from: classes2.dex */
public class b extends p0 {
/* renamed from: h, reason: collision with root package name */
private static final long f9973h;
/* renamed from: i, reason: collision with root package name */
private static final long f9974i;
/* renamed from: j, reason: collision with root package name */
static b f9975j;
/* renamed from: e, reason: collision with root package name */
private boolean f9976e;
/* renamed from: f, reason: collision with root package name */
private b f9977f;
/* renamed from: g, reason: collision with root package name */
private long f9978g;
class a implements n0 {
/* renamed from: a, reason: collision with root package name */
final /* synthetic */ n0 f9979a;
a(n0 n0Var) {
this.f9979a = n0Var;
}
@Override // h5.n0
public p0 b() {
return b.this;
}
@Override // h5.n0, java.io.Closeable, java.lang.AutoCloseable
public void close() {
b.this.q();
try {
try {
this.f9979a.close();
b.this.m(true);
} catch (IOException e10) {
throw b.this.k(e10);
}
} catch (Throwable th) {
b.this.m(false);
throw th;
}
}
@Override // h5.n0, java.io.Flushable
public void flush() {
b.this.q();
try {
try {
this.f9979a.flush();
b.this.m(true);
} catch (IOException e10) {
throw b.this.k(e10);
}
} catch (Throwable th) {
b.this.m(false);
throw th;
}
}
@Override // h5.n0
public void k0(d dVar, long j10) {
y0.c(dVar.f10014b, 0L, j10);
while (true) {
long j11 = 0;
if (j10 <= 0) {
return;
}
v0 v0Var = dVar.f10013a;
while (true) {
if (j11 >= 65536) {
break;
}
j11 += v0Var.f10311c - v0Var.f10310b;
if (j11 >= j10) {
j11 = j10;
break;
}
v0Var = v0Var.f10314f;
}
b.this.q();
try {
try {
this.f9979a.k0(dVar, j11);
j10 -= j11;
b.this.m(true);
} catch (IOException e10) {
throw b.this.k(e10);
}
} catch (Throwable th) {
b.this.m(false);
throw th;
}
}
}
public String toString() {
return "AsyncTimeout.sink(" + this.f9979a + ")";
}
}
/* renamed from: h5.b$b, reason: collision with other inner class name */
class C0150b implements o0 {
/* renamed from: a, reason: collision with root package name */
final /* synthetic */ o0 f9981a;
C0150b(o0 o0Var) {
this.f9981a = o0Var;
}
@Override // h5.o0
public long H(d dVar, long j10) {
b.this.q();
try {
try {
long H = this.f9981a.H(dVar, j10);
b.this.m(true);
return H;
} catch (IOException e10) {
throw b.this.k(e10);
}
} catch (Throwable th) {
b.this.m(false);
throw th;
}
}
@Override // h5.o0
public p0 b() {
return b.this;
}
@Override // h5.o0, java.io.Closeable, java.lang.AutoCloseable
public void close() {
try {
try {
this.f9981a.close();
b.this.m(true);
} catch (IOException e10) {
throw b.this.k(e10);
}
} catch (Throwable th) {
b.this.m(false);
throw th;
}
}
public String toString() {
return "AsyncTimeout.source(" + this.f9981a + ")";
}
}
private static final class c extends Thread {
c() {
super("Okio Watchdog");
setDaemon(true);
}
/* JADX WARN: Code restructure failed: missing block: B:19:0x0015, code lost:
r1.p();
*/
@Override // java.lang.Thread, java.lang.Runnable
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct code enable 'Show inconsistent code' option in preferences
*/
public void run() {
/*
r3 = this;
L0:
java.lang.Class
r0 = h5.b.class
monitor-enter(r0) // Catch: java.lang.InterruptedException -> L0
h5.b r1 = h5.b.j() // Catch: java.lang.Throwable -> L19
if (r1 != 0) goto Lb
monitor-exit(r0) // Catch: java.lang.Throwable -> L19
goto L0
Lb:
h5.b r2 = h5.b.f9975j // Catch: java.lang.Throwable -> L19
if (r1 != r2) goto L14
r1 = 0
h5.b.f9975j = r1 // Catch: java.lang.Throwable -> L19
monitor-exit(r0) // Catch: java.lang.Throwable -> L19
return
L14:
monitor-exit(r0) // Catch: java.lang.Throwable -> L19
r1.p() // Catch: java.lang.InterruptedException -> L0
goto L0
L19:
r1 = move-exception
monitor-exit(r0) // Catch: java.lang.Throwable -> L19
throw r1 // Catch: java.lang.InterruptedException -> L0
*/
throw new UnsupportedOperationException("Method not decompiled: h5.b.c.run():void");
}
}
static {
long millis = TimeUnit.SECONDS.toMillis(120L);
f9973h = millis;
f9974i = TimeUnit.MILLISECONDS.toNanos(millis);
}
private long i(long j10) {
return this.f9978g - j10;
}
static b j() {
b bVar = f9975j.f9977f;
long nanoTime = System.nanoTime();
if (bVar == null) {
b.class.wait(f9973h);
if (f9975j.f9977f != null || System.nanoTime() - nanoTime < f9974i) {
return null;
}
return f9975j;
}
long i10 = bVar.i(nanoTime);
if (i10 > 0) {
long j10 = i10 / 1000000;
b.class.wait(j10, (int) (i10 - (1000000 * j10)));
return null;
}
f9975j.f9977f = bVar.f9977f;
bVar.f9977f = null;
return bVar;
}
/* JADX WARN: Removed duplicated region for block: B:22:0x005a A[Catch: all -> 0x0067, TRY_LEAVE, TryCatch #0 {, blocks: (B:4:0x0003, B:6:0x0007, B:7:0x0016, B:10:0x0022, B:11:0x002b, B:12:0x003a, B:13:0x0040, B:15:0x0044, B:17:0x004d, B:20:0x0050, B:22:0x005a, B:30:0x0034, B:31:0x0061, B:32:0x0066), top: B:3:0x0003 }] */
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct code enable 'Show inconsistent code' option in preferences
*/
private static synchronized void l(h5.b r6, long r7, boolean r9) {
/*
java.lang.Class r0 = h5.b.class
monitor-enter(r0)
h5.b r1 = h5.b.f9975j // Catch: java.lang.Throwable -> L67
if (r1 != 0) goto L16
h5.b r1 = new h5.b // Catch: java.lang.Throwable -> L67
r1.() // Catch: java.lang.Throwable -> L67
h5.b.f9975j = r1 // Catch: java.lang.Throwable -> L67
h5.b$c r1 = new h5.b$c // Catch: java.lang.Throwable -> L67
r1.() // Catch: java.lang.Throwable -> L67
r1.start() // Catch: java.lang.Throwable -> L67
L16:
long r1 = java.lang.System.nanoTime() // Catch: java.lang.Throwable -> L67
r3 = 0
int r5 = (r7 > r3 ? 1 : (r7 == r3 ? 0 : -1))
if (r5 == 0) goto L2f
if (r9 == 0) goto L2f
long r3 = r6.c() // Catch: java.lang.Throwable -> L67
long r3 = r3 - r1
long r7 = java.lang.Math.min(r7, r3) // Catch: java.lang.Throwable -> L67
L2b:
long r7 = r7 + r1
r6.f9978g = r7 // Catch: java.lang.Throwable -> L67
goto L3a
L2f:
if (r5 == 0) goto L32
goto L2b
L32:
if (r9 == 0) goto L61
long r7 = r6.c() // Catch: java.lang.Throwable -> L67
r6.f9978g = r7 // Catch: java.lang.Throwable -> L67
L3a:
long r7 = r6.i(r1) // Catch: java.lang.Throwable -> L67
h5.b r9 = h5.b.f9975j // Catch: java.lang.Throwable -> L67
L40:
h5.b r3 = r9.f9977f // Catch: java.lang.Throwable -> L67
if (r3 == 0) goto L50
long r3 = r3.i(r1) // Catch: java.lang.Throwable -> L67
int r5 = (r7 > r3 ? 1 : (r7 == r3 ? 0 : -1))
if (r5 >= 0) goto L4d
goto L50
L4d:
h5.b r9 = r9.f9977f // Catch: java.lang.Throwable -> L67
goto L40
L50:
h5.b r7 = r9.f9977f // Catch: java.lang.Throwable -> L67
r6.f9977f = r7 // Catch: java.lang.Throwable -> L67
r9.f9977f = r6 // Catch: java.lang.Throwable -> L67
h5.b r6 = h5.b.f9975j // Catch: java.lang.Throwable -> L67
if (r9 != r6) goto L5f
java.lang.Class r6 = h5.b.class
r6.notify() // Catch: java.lang.Throwable -> L67
L5f:
monitor-exit(r0)
return
L61:
java.lang.AssertionError r6 = new java.lang.AssertionError // Catch: java.lang.Throwable -> L67
r6.() // Catch: java.lang.Throwable -> L67
throw r6 // Catch: java.lang.Throwable -> L67
L67:
r6 = move-exception
monitor-exit(r0)
throw r6
*/
throw new UnsupportedOperationException("Method not decompiled: h5.b.l(h5.b, long, boolean):void");
}
private static synchronized boolean n(b bVar) {
synchronized (b.class) {
b bVar2 = f9975j;
while (bVar2 != null) {
b bVar3 = bVar2.f9977f;
if (bVar3 == bVar) {
bVar2.f9977f = bVar.f9977f;
bVar.f9977f = null;
return false;
}
bVar2 = bVar3;
}
return true;
}
}
final IOException k(IOException iOException) {
return !r() ? iOException : o(iOException);
}
final void m(boolean z10) {
if (r() && z10) {
throw o(null);
}
}
protected IOException o(IOException iOException) {
InterruptedIOException interruptedIOException = new InterruptedIOException("timeout");
if (iOException != null) {
interruptedIOException.initCause(iOException);
}
return interruptedIOException;
}
protected void p() {
}
public final void q() {
if (this.f9976e) {
throw new IllegalStateException("Unbalanced enter/exit");
}
long h10 = h();
boolean e10 = e();
if (h10 != 0 || e10) {
this.f9976e = true;
l(this, h10, e10);
}
}
public final boolean r() {
if (!this.f9976e) {
return false;
}
this.f9976e = false;
return n(this);
}
public final n0 s(n0 n0Var) {
return new a(n0Var);
}
public final o0 t(o0 o0Var) {
return new C0150b(o0Var);
}
}