package x7;

import d5.AbstractC0844a;
import java.util.concurrent.locks.ReentrantLock;

/* JADX INFO: loaded from: classes.dex */
public final class l implements H, AutoCloseable {

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

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

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

    public l(s sVar, long j8) {
        P6.g.e(sVar, "fileHandle");
        this.f18773q = sVar;
        this.f18774r = j8;
    }

    @Override // x7.H
    public final long M(C1731g c1731g, long j8) {
        long j9;
        long j10;
        int i;
        P6.g.e(c1731g, "sink");
        if (this.f18775s) {
            throw new IllegalStateException("closed");
        }
        s sVar = this.f18773q;
        long j11 = this.f18774r;
        sVar.getClass();
        if (j8 < 0) {
            throw new IllegalArgumentException(AbstractC0844a.i(j8, "byteCount < 0: ").toString());
        }
        long j12 = j8 + j11;
        long j13 = j11;
        while (true) {
            if (j13 >= j12) {
                j9 = -1;
                break;
            }
            C cQ0 = c1731g.q0(1);
            byte[] bArr = cQ0.f18727a;
            int i5 = cQ0.f18729c;
            j9 = -1;
            int iMin = (int) Math.min(j12 - j13, 8192 - i5);
            synchronized (sVar) {
                P6.g.e(bArr, "array");
                sVar.f18793u.seek(j13);
                i = 0;
                while (true) {
                    if (i >= iMin) {
                        break;
                    }
                    int i8 = sVar.f18793u.read(bArr, i5, iMin - i);
                    if (i8 != -1) {
                        i += i8;
                    } else if (i == 0) {
                        i = -1;
                    }
                }
            }
            if (i == -1) {
                if (cQ0.f18728b == cQ0.f18729c) {
                    c1731g.f18764q = cQ0.a();
                    D.a(cQ0);
                }
                if (j11 == j13) {
                    j10 = -1;
                }
            } else {
                cQ0.f18729c += i;
                long j14 = i;
                j13 += j14;
                c1731g.f18765r += j14;
            }
        }
        j10 = j13 - j11;
        if (j10 != j9) {
            this.f18774r += j10;
        }
        return j10;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (this.f18775s) {
            return;
        }
        this.f18775s = true;
        s sVar = this.f18773q;
        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.H
    public final J f() {
        return J.f18740d;
    }
}
