package o;

import java.util.concurrent.locks.ReentrantLock;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.Vx, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C0568Vx implements T90 {
    public final C2609uJ l;
    public long m;
    public boolean n;

    public C0568Vx(C2609uJ c2609uJ, long j) {
        this.l = c2609uJ;
        this.m = j;
    }

    @Override // o.T90
    public final long C(long j, C0732af c0732af) {
        long j2;
        long j3;
        int i;
        if (this.n) {
            throw new IllegalStateException("closed");
        }
        C2609uJ c2609uJ = this.l;
        long j4 = this.m;
        if (j < 0) {
            throw new IllegalArgumentException(AbstractC0126Ew.l("byteCount < 0: ", j).toString());
        }
        long j5 = j + j4;
        long j6 = j4;
        while (true) {
            if (j6 >= j5) {
                j2 = -1;
                break;
            }
            C2496t50 c2496t50Y = c0732af.y(1);
            byte[] bArr = c2496t50Y.a;
            int i2 = c2496t50Y.c;
            j2 = -1;
            int iMin = (int) Math.min(j5 - j6, 8192 - i2);
            synchronized (c2609uJ) {
                c2609uJ.f743o.seek(j6);
                i = 0;
                while (true) {
                    if (i >= iMin) {
                        break;
                    }
                    int i3 = c2609uJ.f743o.read(bArr, i2, iMin - i);
                    if (i3 != -1) {
                        i += i3;
                    } else if (i == 0) {
                        i = -1;
                    }
                }
            }
            if (i == -1) {
                if (c2496t50Y.b == c2496t50Y.c) {
                    c0732af.l = c2496t50Y.a();
                    AbstractC2781w50.a(c2496t50Y);
                }
                if (j4 == j6) {
                    j3 = -1;
                }
            } else {
                c2496t50Y.c += i;
                long j7 = i;
                j6 += j7;
                c0732af.m += j7;
            }
        }
        j3 = j6 - j4;
        if (j3 != j2) {
            this.m += j3;
        }
        return j3;
    }

    @Override // o.T90
    public final Cf0 a() {
        return Cf0.d;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        C2609uJ c2609uJ = this.l;
        if (this.n) {
            return;
        }
        this.n = true;
        ReentrantLock reentrantLock = c2609uJ.n;
        reentrantLock.lock();
        try {
            int i = c2609uJ.m - 1;
            c2609uJ.m = i;
            if (i == 0) {
                if (c2609uJ.l) {
                    synchronized (c2609uJ) {
                        c2609uJ.f743o.close();
                    }
                }
            }
        } finally {
            reentrantLock.unlock();
        }
    }
}
