package p037.p109;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import p037.p109.p111.C2420;
import p037.p109.p111.C2421;
import p037.p115.p116.InterfaceC2442;
import p037.p115.p116.InterfaceC2443;
import p138.p139.p143.p144.C2803;

/* JADX INFO: renamed from: މ.ސ.ފ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C2414 implements InterfaceC2443 {

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final Context f7651;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final String f7652;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final File f7653;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public final int f7654;

    /* JADX INFO: renamed from: އ, reason: contains not printable characters */
    public final InterfaceC2443 f7655;

    /* JADX INFO: renamed from: ވ, reason: contains not printable characters */
    public C2377 f7656;

    /* JADX INFO: renamed from: މ, reason: contains not printable characters */
    public boolean f7657;

    @Override // p037.p115.p116.InterfaceC2443, java.io.Closeable, java.lang.AutoCloseable
    public synchronized void close() {
        this.f7655.close();
        this.f7657 = false;
    }

    @Override // p037.p115.p116.InterfaceC2443
    public String getDatabaseName() {
        return this.f7655.getDatabaseName();
    }

    @Override // p037.p115.p116.InterfaceC2443
    public synchronized InterfaceC2442 getWritableDatabase() {
        if (!this.f7657) {
            m4494();
            this.f7657 = true;
        }
        return this.f7655.getWritableDatabase();
    }

    @Override // p037.p115.p116.InterfaceC2443
    public void setWriteAheadLoggingEnabled(boolean z) {
        this.f7655.setWriteAheadLoggingEnabled(z);
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m4494() {
        String databaseName = this.f7655.getDatabaseName();
        File databasePath = this.f7651.getDatabasePath(databaseName);
        C2377 c2377 = this.f7656;
        C2420 c2420 = new C2420(databaseName, this.f7651.getFilesDir(), c2377 == null || c2377.f7540);
        try {
            c2420.f7672.lock();
            if (c2420.f7673) {
                try {
                    FileChannel channel = new FileOutputStream(c2420.f7671).getChannel();
                    c2420.f7674 = channel;
                    channel.lock();
                } catch (IOException e) {
                    throw new IllegalStateException("Unable to grab copy lock.", e);
                }
            }
            if (!databasePath.exists()) {
                try {
                    m4495(databasePath);
                    c2420.m4502();
                    return;
                } catch (IOException e2) {
                    throw new RuntimeException("Unable to copy database file.", e2);
                }
            }
            if (this.f7656 == null) {
                c2420.m4502();
                return;
            }
            try {
                int iM4503 = C2421.m4503(databasePath);
                if (iM4503 == this.f7654) {
                    c2420.m4502();
                    return;
                }
                if (this.f7656.m4444(iM4503, this.f7654)) {
                    c2420.m4502();
                    return;
                }
                if (this.f7651.deleteDatabase(databaseName)) {
                    try {
                        m4495(databasePath);
                    } catch (IOException e3) {
                        Log.w("ROOM", "Unable to copy database file.", e3);
                    }
                } else {
                    Log.w("ROOM", "Failed to delete database file (" + databaseName + ") for a copy destructive migration.");
                }
                c2420.m4502();
                return;
            } catch (IOException e4) {
                Log.w("ROOM", "Unable to read database version.", e4);
                c2420.m4502();
                return;
            }
        } catch (Throwable th) {
            c2420.m4502();
            throw th;
        }
        c2420.m4502();
        throw th;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m4495(File file) throws IOException {
        ReadableByteChannel channel;
        if (this.f7652 != null) {
            channel = Channels.newChannel(this.f7651.getAssets().open(this.f7652));
        } else {
            if (this.f7653 == null) {
                throw new IllegalStateException("copyFromAssetPath and copyFromFile == null!");
            }
            channel = new FileInputStream(this.f7653).getChannel();
        }
        File fileCreateTempFile = File.createTempFile("room-copy-helper", ".tmp", this.f7651.getCacheDir());
        fileCreateTempFile.deleteOnExit();
        FileChannel channel2 = new FileOutputStream(fileCreateTempFile).getChannel();
        try {
            if (Build.VERSION.SDK_INT <= 23) {
                InputStream inputStreamNewInputStream = Channels.newInputStream(channel);
                OutputStream outputStreamNewOutputStream = Channels.newOutputStream(channel2);
                byte[] bArr = new byte[4096];
                while (true) {
                    int i = inputStreamNewInputStream.read(bArr);
                    if (i <= 0) {
                        break;
                    } else {
                        outputStreamNewOutputStream.write(bArr, 0, i);
                    }
                }
            } else {
                channel2.transferFrom(channel, 0L, Long.MAX_VALUE);
            }
            channel2.force(false);
            channel.close();
            channel2.close();
            File parentFile = file.getParentFile();
            if (parentFile != null && !parentFile.exists() && !parentFile.mkdirs()) {
                StringBuilder sbM4889 = C2803.m4889("Failed to create directories for ");
                sbM4889.append(file.getAbsolutePath());
                throw new IOException(sbM4889.toString());
            }
            if (fileCreateTempFile.renameTo(file)) {
                return;
            }
            StringBuilder sbM48892 = C2803.m4889("Failed to move intermediate file (");
            sbM48892.append(fileCreateTempFile.getAbsolutePath());
            sbM48892.append(") to destination (");
            sbM48892.append(file.getAbsolutePath());
            sbM48892.append(").");
            throw new IOException(sbM48892.toString());
        } catch (Throwable th) {
            channel.close();
            channel2.close();
            throw th;
        }
    }
}
