package n3;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/* JADX INFO: loaded from: classes.dex */
public final class a {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final HashMap f30798e = new HashMap();

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final File f30800b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Lock f30801c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public FileChannel f30802d;

    public a(File file, String str, boolean z10) {
        Lock lock;
        this.f30799a = z10;
        this.f30800b = file != null ? new File(file, str.concat(".lck")) : null;
        HashMap map = f30798e;
        synchronized (map) {
            try {
                Object reentrantLock = map.get(str);
                if (reentrantLock == null) {
                    reentrantLock = new ReentrantLock();
                    map.put(str, reentrantLock);
                }
                lock = (Lock) reentrantLock;
            } catch (Throwable th) {
                throw th;
            }
        }
        this.f30801c = lock;
    }

    public final void a(boolean z10) {
        this.f30801c.lock();
        if (z10) {
            File file = this.f30800b;
            try {
                if (file == null) {
                    throw new IOException("No lock directory was provided.");
                }
                File parentFile = file.getParentFile();
                if (parentFile != null) {
                    parentFile.mkdirs();
                }
                FileChannel channel = new FileOutputStream(file).getChannel();
                channel.lock();
                this.f30802d = channel;
            } catch (IOException unused) {
                this.f30802d = null;
            }
        }
    }

    public final void b() {
        try {
            FileChannel fileChannel = this.f30802d;
            if (fileChannel != null) {
                fileChannel.close();
            }
        } catch (IOException unused) {
        }
        this.f30801c.unlock();
    }
}
