package x7;

import java.util.concurrent.locks.ReentrantLock;

/* JADX INFO: renamed from: x7.k, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1735k implements F, AutoCloseable {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final s f18770q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public long f18771r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public boolean f18772s;

    public C1735k(s sVar) {
        P6.g.e(sVar, "fileHandle");
        this.f18770q = sVar;
        this.f18771r = 0L;
    }

    @Override // x7.F
    public final void X(C1731g c1731g, long j8) {
        P6.g.e(c1731g, "source");
        if (this.f18772s) {
            throw new IllegalStateException("closed");
        }
        s sVar = this.f18770q;
        long j9 = this.f18771r;
        sVar.getClass();
        s7.d.g(c1731g.f18765r, 0L, j8);
        long j10 = j9 + j8;
        while (j9 < j10) {
            C c3 = c1731g.f18764q;
            P6.g.b(c3);
            int iMin = (int) Math.min(j10 - j9, c3.f18729c - c3.f18728b);
            byte[] bArr = c3.f18727a;
            int i = c3.f18728b;
            synchronized (sVar) {
                P6.g.e(bArr, "array");
                sVar.f18793u.seek(j9);
                sVar.f18793u.write(bArr, i, iMin);
            }
            int i5 = c3.f18728b + iMin;
            c3.f18728b = i5;
            long j11 = iMin;
            j9 += j11;
            c1731g.f18765r -= j11;
            if (i5 == c3.f18729c) {
                c1731g.f18764q = c3.a();
                D.a(c3);
            }
        }
        this.f18771r += j8;
    }

    @Override // x7.F, java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (this.f18772s) {
            return;
        }
        this.f18772s = true;
        s sVar = this.f18770q;
        ReentrantLock reentrantLock = sVar.f18792t;
        reentrantLock.lock();
        try {
            int i = sVar.f18791s - 1;
            sVar.f18791s = i;
            if (i == 0) {
                if (sVar.f18790r) {
                    synchronized (sVar) {
                        sVar.f18793u.close();
                    }
                }
            }
        } finally {
            reentrantLock.unlock();
        }
    }

    @Override // x7.F
    public final J f() {
        return J.f18740d;
    }

    @Override // x7.F, java.io.Flushable
    public final void flush() {
        if (this.f18772s) {
            throw new IllegalStateException("closed");
        }
        s sVar = this.f18770q;
        synchronized (sVar) {
            sVar.f18793u.getFD().sync();
        }
    }
}
