package p5;

import java.util.concurrent.locks.ReentrantLock;

/* JADX INFO: renamed from: p5.p, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C2746p implements P {

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

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

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

    public C2746p(AbstractC2747q fileHandle, long j4) {
        kotlin.jvm.internal.o.h(fileHandle, "fileHandle");
        this.f23108a = fileHandle;
        this.f23109b = j4;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (this.f23110c) {
            return;
        }
        this.f23110c = true;
        AbstractC2747q abstractC2747q = this.f23108a;
        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.P
    public final long read(C2740j sink, long j4) {
        long j6;
        long j7;
        kotlin.jvm.internal.o.h(sink, "sink");
        if (this.f23110c) {
            throw new IllegalStateException("closed");
        }
        long j8 = this.f23109b;
        AbstractC2747q abstractC2747q = this.f23108a;
        abstractC2747q.getClass();
        if (j4 < 0) {
            throw new IllegalArgumentException(A0.a.m("byteCount < 0: ", j4).toString());
        }
        long j9 = j4 + j8;
        long j10 = j8;
        while (true) {
            if (j10 >= j9) {
                j6 = -1;
                break;
            }
            K kM = sink.M(1);
            j6 = -1;
            long j11 = j9;
            int iC = abstractC2747q.c(j10, kM.f23061a, kM.f23063c, (int) Math.min(j9 - j10, 8192 - r10));
            if (iC == -1) {
                if (kM.f23062b == kM.f23063c) {
                    sink.f23099a = kM.a();
                    L.a(kM);
                }
                if (j8 == j10) {
                    j7 = -1;
                }
            } else {
                kM.f23063c += iC;
                long j12 = iC;
                j10 += j12;
                sink.f23100b += j12;
                j9 = j11;
            }
        }
        j7 = j10 - j8;
        if (j7 != j6) {
            this.f23109b += j7;
        }
        return j7;
    }

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