package okio;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.concurrent.TimeUnit;

/* JADX INFO: renamed from: okio.d, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes2.dex */
public class C0686d extends F {
    public static final a Companion = new a(null);
    private static final long IDLE_TIMEOUT_MILLIS;
    private static final long IDLE_TIMEOUT_NANOS;
    private static final int TIMEOUT_WRITE_SIZE = 65536;
    private static C0686d head;
    private boolean inQueue;
    private C0686d next;
    private long timeoutAt;

    /* JADX INFO: renamed from: okio.d$a */
    public static final class a {
        private a() {
        }

        /* JADX INFO: Access modifiers changed from: private */
        public final boolean d(C0686d c0686d) {
            synchronized (C0686d.class) {
                for (C0686d c0686d2 = C0686d.head; c0686d2 != null; c0686d2 = c0686d2.next) {
                    if (c0686d2.next == c0686d) {
                        c0686d2.next = c0686d.next;
                        c0686d.next = null;
                        return false;
                    }
                }
                return true;
            }
        }

        /* JADX INFO: Access modifiers changed from: private */
        public final void e(C0686d c0686d, long j2, boolean z2) {
            synchronized (C0686d.class) {
                try {
                    if (C0686d.head == null) {
                        C0686d.head = new C0686d();
                        new b().start();
                    }
                    long jNanoTime = System.nanoTime();
                    if (j2 != 0 && z2) {
                        c0686d.timeoutAt = Math.min(j2, c0686d.deadlineNanoTime() - jNanoTime) + jNanoTime;
                    } else if (j2 != 0) {
                        c0686d.timeoutAt = j2 + jNanoTime;
                    } else {
                        if (!z2) {
                            throw new AssertionError();
                        }
                        c0686d.timeoutAt = c0686d.deadlineNanoTime();
                    }
                    long jA = c0686d.a(jNanoTime);
                    C0686d c0686d2 = C0686d.head;
                    kotlin.jvm.internal.l.b(c0686d2);
                    while (c0686d2.next != null) {
                        C0686d c0686d3 = c0686d2.next;
                        kotlin.jvm.internal.l.b(c0686d3);
                        if (jA < c0686d3.a(jNanoTime)) {
                            break;
                        }
                        c0686d2 = c0686d2.next;
                        kotlin.jvm.internal.l.b(c0686d2);
                    }
                    c0686d.next = c0686d2.next;
                    c0686d2.next = c0686d;
                    if (c0686d2 == C0686d.head) {
                        C0686d.class.notify();
                    }
                    n1.s sVar = n1.s.f5871a;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }

        public final C0686d c() throws InterruptedException {
            C0686d c0686d = C0686d.head;
            kotlin.jvm.internal.l.b(c0686d);
            C0686d c0686d2 = c0686d.next;
            if (c0686d2 == null) {
                long jNanoTime = System.nanoTime();
                C0686d.class.wait(C0686d.IDLE_TIMEOUT_MILLIS);
                C0686d c0686d3 = C0686d.head;
                kotlin.jvm.internal.l.b(c0686d3);
                if (c0686d3.next != null || System.nanoTime() - jNanoTime < C0686d.IDLE_TIMEOUT_NANOS) {
                    return null;
                }
                return C0686d.head;
            }
            long jA = c0686d2.a(System.nanoTime());
            if (jA > 0) {
                long j2 = jA / 1000000;
                C0686d.class.wait(j2, (int) (jA - (1000000 * j2)));
                return null;
            }
            C0686d c0686d4 = C0686d.head;
            kotlin.jvm.internal.l.b(c0686d4);
            c0686d4.next = c0686d2.next;
            c0686d2.next = null;
            return c0686d2;
        }

        public /* synthetic */ a(kotlin.jvm.internal.g gVar) {
            this();
        }
    }

    /* JADX INFO: renamed from: okio.d$b */
    private static final class b extends Thread {
        public b() {
            super("Okio Watchdog");
            setDaemon(true);
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            C0686d c0686dC;
            while (true) {
                try {
                    synchronized (C0686d.class) {
                        c0686dC = C0686d.Companion.c();
                        if (c0686dC == C0686d.head) {
                            C0686d.head = null;
                            return;
                        }
                        n1.s sVar = n1.s.f5871a;
                    }
                    if (c0686dC != null) {
                        c0686dC.timedOut();
                    }
                } catch (InterruptedException unused) {
                    continue;
                }
            }
        }
    }

    /* JADX INFO: renamed from: okio.d$c */
    public static final class c implements C {

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final /* synthetic */ C f6018g;

        c(C c2) {
            this.f6018g = c2;
        }

        @Override // okio.C
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public C0686d timeout() {
            return C0686d.this;
        }

        @Override // okio.C, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.Channel
        public void close() throws IOException {
            C0686d c0686d = C0686d.this;
            c0686d.enter();
            try {
                this.f6018g.close();
                n1.s sVar = n1.s.f5871a;
                if (c0686d.exit()) {
                    throw c0686d.access$newTimeoutException(null);
                }
            } catch (IOException e2) {
                if (!c0686d.exit()) {
                    throw e2;
                }
                throw c0686d.access$newTimeoutException(e2);
            } finally {
                c0686d.exit();
            }
        }

        @Override // okio.C, java.io.Flushable
        public void flush() throws IOException {
            C0686d c0686d = C0686d.this;
            c0686d.enter();
            try {
                this.f6018g.flush();
                n1.s sVar = n1.s.f5871a;
                if (c0686d.exit()) {
                    throw c0686d.access$newTimeoutException(null);
                }
            } catch (IOException e2) {
                if (!c0686d.exit()) {
                    throw e2;
                }
                throw c0686d.access$newTimeoutException(e2);
            } finally {
                c0686d.exit();
            }
        }

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

        @Override // okio.C
        public void write(C0688f source, long j2) throws IOException {
            kotlin.jvm.internal.l.e(source, "source");
            AbstractC0685c.b(source.G(), 0L, j2);
            while (true) {
                long j3 = 0;
                if (j2 <= 0) {
                    return;
                }
                y yVar = source.f6021b;
                kotlin.jvm.internal.l.b(yVar);
                while (true) {
                    if (j3 >= 65536) {
                        break;
                    }
                    j3 += (long) (yVar.f6073c - yVar.f6072b);
                    if (j3 >= j2) {
                        j3 = j2;
                        break;
                    } else {
                        yVar = yVar.f6076f;
                        kotlin.jvm.internal.l.b(yVar);
                    }
                }
                C0686d c0686d = C0686d.this;
                c0686d.enter();
                try {
                    this.f6018g.write(source, j3);
                    n1.s sVar = n1.s.f5871a;
                    if (c0686d.exit()) {
                        throw c0686d.access$newTimeoutException(null);
                    }
                    j2 -= j3;
                } catch (IOException e2) {
                    if (!c0686d.exit()) {
                        throw e2;
                    }
                    throw c0686d.access$newTimeoutException(e2);
                } finally {
                    c0686d.exit();
                }
            }
        }
    }

    /* JADX INFO: renamed from: okio.d$d, reason: collision with other inner class name */
    public static final class C0087d implements E {

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        final /* synthetic */ E f6020g;

        C0087d(E e2) {
            this.f6020g = e2;
        }

        @Override // okio.E
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public C0686d timeout() {
            return C0686d.this;
        }

        @Override // okio.E, java.io.Closeable, java.lang.AutoCloseable
        public void close() throws IOException {
            C0686d c0686d = C0686d.this;
            c0686d.enter();
            try {
                this.f6020g.close();
                n1.s sVar = n1.s.f5871a;
                if (c0686d.exit()) {
                    throw c0686d.access$newTimeoutException(null);
                }
            } catch (IOException e2) {
                if (!c0686d.exit()) {
                    throw e2;
                }
                throw c0686d.access$newTimeoutException(e2);
            } finally {
                c0686d.exit();
            }
        }

        @Override // okio.E
        public long read(C0688f sink, long j2) throws IOException {
            kotlin.jvm.internal.l.e(sink, "sink");
            C0686d c0686d = C0686d.this;
            c0686d.enter();
            try {
                long j3 = this.f6020g.read(sink, j2);
                if (c0686d.exit()) {
                    throw c0686d.access$newTimeoutException(null);
                }
                return j3;
            } catch (IOException e2) {
                if (c0686d.exit()) {
                    throw c0686d.access$newTimeoutException(e2);
                }
                throw e2;
            } finally {
                c0686d.exit();
            }
        }

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

    static {
        long millis = TimeUnit.SECONDS.toMillis(60L);
        IDLE_TIMEOUT_MILLIS = millis;
        IDLE_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(millis);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final long a(long j2) {
        return this.timeoutAt - j2;
    }

    public final IOException access$newTimeoutException(IOException iOException) {
        return newTimeoutException(iOException);
    }

    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;
            Companion.e(this, jTimeoutNanos, zHasDeadline);
        }
    }

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

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

    public final C sink(C sink) {
        kotlin.jvm.internal.l.e(sink, "sink");
        return new c(sink);
    }

    public final E source(E source) {
        kotlin.jvm.internal.l.e(source, "source");
        return new C0087d(source);
    }

    protected void timedOut() {
    }

    public final <T> T withTimeout(y1.a block) throws IOException {
        kotlin.jvm.internal.l.e(block, "block");
        enter();
        try {
            try {
                T t2 = (T) block.invoke();
                kotlin.jvm.internal.k.b(1);
                if (exit()) {
                    throw access$newTimeoutException(null);
                }
                kotlin.jvm.internal.k.a(1);
                return t2;
            } catch (IOException e2) {
                if (exit()) {
                    throw access$newTimeoutException(e2);
                }
                throw e2;
            }
        } catch (Throwable th) {
            kotlin.jvm.internal.k.b(1);
            exit();
            kotlin.jvm.internal.k.a(1);
            throw th;
        }
    }
}
