package h;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

/* JADX INFO: compiled from: AsyncTimeout.java */
/* JADX INFO: loaded from: classes.dex */
public class a extends t {
    private static final long IDLE_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(60);
    private static final long IDLE_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(IDLE_TIMEOUT_MILLIS);
    private static final int TIMEOUT_WRITE_SIZE = 65536;

    @Nullable
    static a head;
    private boolean inQueue;

    @Nullable
    private a next;
    private long timeoutAt;

    /* JADX INFO: renamed from: h.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: AsyncTimeout.java */
    class C0130a implements r {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ r f4171b;

        C0130a(r rVar) {
            this.f4171b = rVar;
        }

        @Override // h.r, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            a.this.enter();
            try {
                try {
                    this.f4171b.close();
                    a.this.exit(true);
                } catch (IOException e2) {
                    throw a.this.exit(e2);
                }
            } catch (Throwable th) {
                a.this.exit(false);
                throw th;
            }
        }

        @Override // h.r, java.io.Flushable
        public void flush() throws IOException {
            a.this.enter();
            try {
                try {
                    this.f4171b.flush();
                    a.this.exit(true);
                } catch (IOException e2) {
                    throw a.this.exit(e2);
                }
            } catch (Throwable th) {
                a.this.exit(false);
                throw th;
            }
        }

        @Override // h.r
        public t timeout() {
            return a.this;
        }

        public String toString() {
            return "AsyncTimeout.sink(" + this.f4171b + ")";
        }

        @Override // h.r
        public void write(h.c cVar, long j2) throws IOException {
            u.b(cVar.f4177f, 0L, j2);
            while (true) {
                long j3 = 0;
                if (j2 <= 0) {
                    return;
                }
                o oVar = cVar.f4176b;
                while (true) {
                    if (j3 >= 65536) {
                        break;
                    }
                    o oVar2 = cVar.f4176b;
                    j3 += (long) (oVar2.f4206c - oVar2.f4205b);
                    if (j3 >= j2) {
                        j3 = j2;
                        break;
                    }
                    oVar = oVar.f4209f;
                }
                a.this.enter();
                try {
                    try {
                        this.f4171b.write(cVar, j3);
                        j2 -= j3;
                        a.this.exit(true);
                    } catch (IOException e2) {
                        throw a.this.exit(e2);
                    }
                } catch (Throwable th) {
                    a.this.exit(false);
                    throw th;
                }
            }
        }
    }

    /* JADX INFO: compiled from: AsyncTimeout.java */
    class b implements s {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ s f4173b;

        b(s sVar) {
            this.f4173b = sVar;
        }

        @Override // h.s, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            try {
                try {
                    this.f4173b.close();
                    a.this.exit(true);
                } catch (IOException e2) {
                    throw a.this.exit(e2);
                }
            } catch (Throwable th) {
                a.this.exit(false);
                throw th;
            }
        }

        @Override // h.s
        public long read(h.c cVar, long j2) throws IOException {
            a.this.enter();
            try {
                try {
                    long j3 = this.f4173b.read(cVar, j2);
                    a.this.exit(true);
                    return j3;
                } catch (IOException e2) {
                    throw a.this.exit(e2);
                }
            } catch (Throwable th) {
                a.this.exit(false);
                throw th;
            }
        }

        @Override // h.s
        public t timeout() {
            return a.this;
        }

        public String toString() {
            return "AsyncTimeout.source(" + this.f4173b + ")";
        }
    }

    /* JADX INFO: compiled from: AsyncTimeout.java */
    private static final class c extends Thread {
        c() {
            super("Okio Watchdog");
            setDaemon(true);
        }

        /* JADX WARN: Code restructure failed: missing block: B:14:0x0015, 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<h.a> r0 = h.a.class
                monitor-enter(r0)     // Catch: java.lang.InterruptedException -> L0
                h.a r1 = h.a.awaitTimeout()     // Catch: java.lang.Throwable -> L19
                if (r1 != 0) goto Lb
                monitor-exit(r0)     // Catch: java.lang.Throwable -> L19
                goto L0
            Lb:
                h.a r2 = h.a.head     // Catch: java.lang.Throwable -> L19
                if (r1 != r2) goto L14
                r1 = 0
                h.a.head = 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.timedOut()     // 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: h.a.c.run():void");
        }
    }

    @Nullable
    static a awaitTimeout() throws InterruptedException {
        a aVar = head.next;
        if (aVar == null) {
            long jNanoTime = System.nanoTime();
            a.class.wait(IDLE_TIMEOUT_MILLIS);
            if (head.next != null || System.nanoTime() - jNanoTime < IDLE_TIMEOUT_NANOS) {
                return null;
            }
            return head;
        }
        long jRemainingNanos = aVar.remainingNanos(System.nanoTime());
        if (jRemainingNanos > 0) {
            long j2 = jRemainingNanos / 1000000;
            a.class.wait(j2, (int) (jRemainingNanos - (1000000 * j2)));
            return null;
        }
        head.next = aVar.next;
        aVar.next = null;
        return aVar;
    }

    private static synchronized boolean cancelScheduledTimeout(a aVar) {
        for (a aVar2 = head; aVar2 != null; aVar2 = aVar2.next) {
            if (aVar2.next == aVar) {
                aVar2.next = aVar.next;
                aVar.next = null;
                return false;
            }
        }
        return true;
    }

    private long remainingNanos(long j2) {
        return this.timeoutAt - j2;
    }

    private static synchronized void scheduleTimeout(a aVar, long j2, boolean z) {
        if (head == null) {
            head = new a();
            new c().start();
        }
        long jNanoTime = System.nanoTime();
        if (j2 != 0 && z) {
            aVar.timeoutAt = Math.min(j2, aVar.deadlineNanoTime() - jNanoTime) + jNanoTime;
        } else if (j2 != 0) {
            aVar.timeoutAt = j2 + jNanoTime;
        } else {
            if (!z) {
                throw new AssertionError();
            }
            aVar.timeoutAt = aVar.deadlineNanoTime();
        }
        long jRemainingNanos = aVar.remainingNanos(jNanoTime);
        a aVar2 = head;
        while (aVar2.next != null && jRemainingNanos >= aVar2.next.remainingNanos(jNanoTime)) {
            aVar2 = aVar2.next;
        }
        aVar.next = aVar2.next;
        aVar2.next = aVar;
        if (aVar2 == head) {
            a.class.notify();
        }
    }

    public final void enter() {
        if (this.inQueue) {
            throw new IllegalStateException("Unbalanced enter/exit");
        }
        long jTimeoutNanos = timeoutNanos();
        boolean zHasDeadline = hasDeadline();
        if (jTimeoutNanos != 0 || zHasDeadline) {
            this.inQueue = true;
            scheduleTimeout(this, jTimeoutNanos, zHasDeadline);
        }
    }

    public final boolean exit() {
        if (!this.inQueue) {
            return false;
        }
        this.inQueue = false;
        return cancelScheduledTimeout(this);
    }

    protected IOException newTimeoutException(@Nullable IOException iOException) {
        InterruptedIOException interruptedIOException = new InterruptedIOException("timeout");
        if (iOException != null) {
            interruptedIOException.initCause(iOException);
        }
        return interruptedIOException;
    }

    public final r sink(r rVar) {
        return new C0130a(rVar);
    }

    public final s source(s sVar) {
        return new b(sVar);
    }

    protected void timedOut() {
    }

    final void exit(boolean z) throws IOException {
        if (exit() && z) {
            throw newTimeoutException(null);
        }
    }

    final IOException exit(IOException iOException) throws IOException {
        return !exit() ? iOException : newTimeoutException(iOException);
    }
}
