package okio; import android.support.v4.media.session.PlaybackStateCompat; import java.io.IOException; import java.io.InterruptedIOException; import java.util.concurrent.TimeUnit; /* loaded from: classes5.dex */ public class AsyncTimeout extends Timeout { public static final long IDLE_TIMEOUT_MILLIS; public static final long IDLE_TIMEOUT_NANOS; public static AsyncTimeout head; public boolean inQueue; public AsyncTimeout next; public long timeoutAt; public final long remainingNanos(long j) { return this.timeoutAt - j; } public void timedOut() { } static { long millis = TimeUnit.SECONDS.toMillis(60L); IDLE_TIMEOUT_MILLIS = millis; IDLE_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(millis); } public final void enter() { if (this.inQueue) { throw new IllegalStateException("Unbalanced enter/exit"); } long timeoutNanos = timeoutNanos(); boolean hasDeadline = hasDeadline(); if (timeoutNanos != 0 || hasDeadline) { this.inQueue = true; scheduleTimeout(this, timeoutNanos, hasDeadline); } } public static synchronized void scheduleTimeout(AsyncTimeout asyncTimeout, long j, boolean z) { synchronized (AsyncTimeout.class) { try { if (head == null) { head = new AsyncTimeout(); new Watchdog().start(); } long nanoTime = System.nanoTime(); if (j != 0 && z) { asyncTimeout.timeoutAt = Math.min(j, asyncTimeout.deadlineNanoTime() - nanoTime) + nanoTime; } else if (j != 0) { asyncTimeout.timeoutAt = j + nanoTime; } else if (z) { asyncTimeout.timeoutAt = asyncTimeout.deadlineNanoTime(); } else { throw new AssertionError(); } long remainingNanos = asyncTimeout.remainingNanos(nanoTime); AsyncTimeout asyncTimeout2 = head; while (true) { AsyncTimeout asyncTimeout3 = asyncTimeout2.next; if (asyncTimeout3 == null || remainingNanos < asyncTimeout3.remainingNanos(nanoTime)) { break; } else { asyncTimeout2 = asyncTimeout2.next; } } asyncTimeout.next = asyncTimeout2.next; asyncTimeout2.next = asyncTimeout; if (asyncTimeout2 == head) { AsyncTimeout.class.notify(); } } catch (Throwable th) { throw th; } } } public final boolean exit() { if (!this.inQueue) { return false; } this.inQueue = false; return cancelScheduledTimeout(this); } public static synchronized boolean cancelScheduledTimeout(AsyncTimeout asyncTimeout) { synchronized (AsyncTimeout.class) { AsyncTimeout asyncTimeout2 = head; while (asyncTimeout2 != null) { AsyncTimeout asyncTimeout3 = asyncTimeout2.next; if (asyncTimeout3 == asyncTimeout) { asyncTimeout2.next = asyncTimeout.next; asyncTimeout.next = null; return false; } asyncTimeout2 = asyncTimeout3; } return true; } } public final Sink sink(final Sink sink) { return new Sink() { // from class: okio.AsyncTimeout.1 @Override // okio.Sink public Timeout timeout() { return AsyncTimeout.this; } @Override // okio.Sink public void write(Buffer buffer, long j) { Util.checkOffsetAndCount(buffer.size, 0L, j); while (true) { long j2 = 0; if (j <= 0) { return; } Segment segment = buffer.head; while (true) { if (j2 >= PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH) { break; } j2 += segment.limit - segment.pos; if (j2 >= j) { j2 = j; break; } segment = segment.next; } AsyncTimeout.this.enter(); try { try { sink.write(buffer, j2); j -= j2; AsyncTimeout.this.exit(true); } catch (IOException e) { throw AsyncTimeout.this.exit(e); } } catch (Throwable th) { AsyncTimeout.this.exit(false); throw th; } } } @Override // okio.Sink, java.io.Flushable public void flush() { AsyncTimeout.this.enter(); try { try { sink.flush(); AsyncTimeout.this.exit(true); } catch (IOException e) { throw AsyncTimeout.this.exit(e); } } catch (Throwable th) { AsyncTimeout.this.exit(false); throw th; } } @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() { AsyncTimeout.this.enter(); try { try { sink.close(); AsyncTimeout.this.exit(true); } catch (IOException e) { throw AsyncTimeout.this.exit(e); } } catch (Throwable th) { AsyncTimeout.this.exit(false); throw th; } } public String toString() { return "AsyncTimeout.sink(" + sink + ")"; } }; } public final Source source(final Source source) { return new Source() { // from class: okio.AsyncTimeout.2 @Override // okio.Source public Timeout timeout() { return AsyncTimeout.this; } @Override // okio.Source public long read(Buffer buffer, long j) { AsyncTimeout.this.enter(); try { try { long read = source.read(buffer, j); AsyncTimeout.this.exit(true); return read; } catch (IOException e) { throw AsyncTimeout.this.exit(e); } } catch (Throwable th) { AsyncTimeout.this.exit(false); throw th; } } @Override // okio.Source, java.io.Closeable, java.lang.AutoCloseable public void close() { AsyncTimeout.this.enter(); try { try { source.close(); AsyncTimeout.this.exit(true); } catch (IOException e) { throw AsyncTimeout.this.exit(e); } } catch (Throwable th) { AsyncTimeout.this.exit(false); throw th; } } public String toString() { return "AsyncTimeout.source(" + source + ")"; } }; } public final void exit(boolean z) { if (exit() && z) { throw newTimeoutException(null); } } public final IOException exit(IOException iOException) { return !exit() ? iOException : newTimeoutException(iOException); } public IOException newTimeoutException(IOException iOException) { InterruptedIOException interruptedIOException = new InterruptedIOException("timeout"); if (iOException != null) { interruptedIOException.initCause(iOException); } return interruptedIOException; } public static final class Watchdog extends Thread { public Watchdog() { super("Okio Watchdog"); setDaemon(true); } /* JADX WARN: Code restructure failed: missing block: B:19:0x0017, code lost: r1.timedOut(); */ @Override // java.lang.Thread, java.lang.Runnable /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public void run() { /* r3 = this; L0: java.lang.Class r0 = okio.AsyncTimeout.class monitor-enter(r0) // Catch: java.lang.InterruptedException -> L0 okio.AsyncTimeout r1 = okio.AsyncTimeout.awaitTimeout() // Catch: java.lang.Throwable -> Lb if (r1 != 0) goto Ld monitor-exit(r0) // Catch: java.lang.Throwable -> Lb goto L0 Lb: r1 = move-exception goto L1b Ld: okio.AsyncTimeout r2 = okio.AsyncTimeout.head // Catch: java.lang.Throwable -> Lb if (r1 != r2) goto L16 r1 = 0 okio.AsyncTimeout.head = r1 // Catch: java.lang.Throwable -> Lb monitor-exit(r0) // Catch: java.lang.Throwable -> Lb return L16: monitor-exit(r0) // Catch: java.lang.Throwable -> Lb r1.timedOut() // Catch: java.lang.InterruptedException -> L0 goto L0 L1b: monitor-exit(r0) // Catch: java.lang.Throwable -> Lb throw r1 // Catch: java.lang.InterruptedException -> L0 */ throw new UnsupportedOperationException("Method not decompiled: okio.AsyncTimeout.Watchdog.run():void"); } } public static AsyncTimeout awaitTimeout() { AsyncTimeout asyncTimeout = head.next; if (asyncTimeout == null) { long nanoTime = System.nanoTime(); AsyncTimeout.class.wait(IDLE_TIMEOUT_MILLIS); if (head.next != null || System.nanoTime() - nanoTime < IDLE_TIMEOUT_NANOS) { return null; } return head; } long remainingNanos = asyncTimeout.remainingNanos(System.nanoTime()); if (remainingNanos > 0) { long j = remainingNanos / 1000000; AsyncTimeout.class.wait(j, (int) (remainingNanos - (1000000 * j))); return null; } head.next = asyncTimeout.next; asyncTimeout.next = null; return asyncTimeout; } }