package x7;

import java.io.Closeable;
import java.io.RandomAccessFile;
import java.util.concurrent.locks.ReentrantLock;

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

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

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

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final ReentrantLock f18792t = new ReentrantLock();

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final RandomAccessFile f18793u;

    public s(boolean z8, RandomAccessFile randomAccessFile) {
        this.f18789q = z8;
        this.f18793u = randomAccessFile;
    }

    public static C1735k a(s sVar) {
        if (!sVar.f18789q) {
            throw new IllegalStateException("file handle is read-only");
        }
        ReentrantLock reentrantLock = sVar.f18792t;
        reentrantLock.lock();
        try {
            if (sVar.f18790r) {
                throw new IllegalStateException("closed");
            }
            sVar.f18791s++;
            reentrantLock.unlock();
            return new C1735k(sVar);
        } catch (Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    public final long b() {
        long length;
        ReentrantLock reentrantLock = this.f18792t;
        reentrantLock.lock();
        try {
            if (this.f18790r) {
                throw new IllegalStateException("closed");
            }
            synchronized (this) {
                length = this.f18793u.length();
            }
            return length;
        } finally {
            reentrantLock.unlock();
        }
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        ReentrantLock reentrantLock = this.f18792t;
        reentrantLock.lock();
        try {
            if (this.f18790r) {
                return;
            }
            this.f18790r = true;
            if (this.f18791s != 0) {
                return;
            }
            synchronized (this) {
                this.f18793u.close();
            }
        } finally {
            reentrantLock.unlock();
        }
    }

    public final void flush() {
        if (!this.f18789q) {
            throw new IllegalStateException("file handle is read-only");
        }
        ReentrantLock reentrantLock = this.f18792t;
        reentrantLock.lock();
        try {
            if (this.f18790r) {
                throw new IllegalStateException("closed");
            }
            synchronized (this) {
                this.f18793u.getFD().sync();
            }
        } finally {
            reentrantLock.unlock();
        }
    }

    public final l g(long j8) {
        ReentrantLock reentrantLock = this.f18792t;
        reentrantLock.lock();
        try {
            if (this.f18790r) {
                throw new IllegalStateException("closed");
            }
            this.f18791s++;
            reentrantLock.unlock();
            return new l(this, j8);
        } catch (Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }
}
