package p5;

import java.util.concurrent.locks.ReentrantLock;

/* JADX INFO: renamed from: p5.o, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C2745o implements N {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final AbstractC2747q f23105a;

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

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

    public C2745o(AbstractC2747q fileHandle) {
        kotlin.jvm.internal.o.h(fileHandle, "fileHandle");
        this.f23105a = fileHandle;
        this.f23106b = 0L;
    }

    @Override // p5.N, java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (this.f23107c) {
            return;
        }
        this.f23107c = true;
        AbstractC2747q abstractC2747q = this.f23105a;
        ReentrantLock reentrantLock = abstractC2747q.f23114d;
        reentrantLock.lock();
        try {
            int i6 = abstractC2747q.f23113c - 1;
            abstractC2747q.f23113c = i6;
            if (i6 == 0) {
                if (abstractC2747q.f23112b) {
                    reentrantLock.unlock();
                    abstractC2747q.a();
                }
            }
        } finally {
            reentrantLock.unlock();
        }
    }

    @Override // p5.N, java.io.Flushable
    public final void flush() {
        if (this.f23107c) {
            throw new IllegalStateException("closed");
        }
        this.f23105a.b();
    }

    @Override // p5.N
    public final S timeout() {
        return S.f23074d;
    }

    @Override // p5.N
    public final void write(C2740j source, long j4) {
        kotlin.jvm.internal.o.h(source, "source");
        if (this.f23107c) {
            throw new IllegalStateException("closed");
        }
        long j6 = this.f23106b;
        AbstractC2747q abstractC2747q = this.f23105a;
        abstractC2747q.getClass();
        AbstractC2732b.e(source.f23100b, 0L, j4);
        long j7 = j6 + j4;
        long j8 = j6;
        while (j8 < j7) {
            K k6 = source.f23099a;
            kotlin.jvm.internal.o.e(k6);
            int iMin = (int) Math.min(j7 - j8, k6.f23063c - k6.f23062b);
            abstractC2747q.f(j8, k6.f23061a, k6.f23062b, iMin);
            int i6 = k6.f23062b + iMin;
            k6.f23062b = i6;
            long j9 = iMin;
            j8 += j9;
            source.f23100b -= j9;
            if (i6 == k6.f23063c) {
                source.f23099a = k6.a();
                L.a(k6);
            }
        }
        this.f23106b += j4;
    }
}
