package androidx.core.util; import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import b.d.b.a.a; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* loaded from: classes.dex */ public class AtomicFile { private static final String LOG_TAG = "AtomicFile"; private final File mBaseName; private final File mLegacyBackupName; private final File mNewName; public AtomicFile(@NonNull File file) { this.mBaseName = file; this.mNewName = new File(file.getPath() + ".new"); this.mLegacyBackupName = new File(file.getPath() + ".bak"); } private static void rename(@NonNull File file, @NonNull File file2) { if (file2.isDirectory() && !file2.delete()) { Log.e(LOG_TAG, "Failed to delete file which is a directory " + file2); } if (!file.renameTo(file2)) { Log.e(LOG_TAG, "Failed to rename " + file + " to " + file2); } } private static boolean sync(@NonNull FileOutputStream fileOutputStream) { try { fileOutputStream.getFD().sync(); return true; } catch (IOException unused) { return false; } } public void delete() { this.mBaseName.delete(); this.mNewName.delete(); this.mLegacyBackupName.delete(); } public void failWrite(@Nullable FileOutputStream fileOutputStream) { if (fileOutputStream != null) { if (!sync(fileOutputStream)) { Log.e(LOG_TAG, "Failed to sync file output stream"); } try { fileOutputStream.close(); } catch (IOException e) { Log.e(LOG_TAG, "Failed to close file output stream", e); } if (!this.mNewName.delete()) { StringBuilder S = a.S("Failed to delete new file "); S.append(this.mNewName); Log.e(LOG_TAG, S.toString()); } } } public void finishWrite(@Nullable FileOutputStream fileOutputStream) { if (fileOutputStream != null) { if (!sync(fileOutputStream)) { Log.e(LOG_TAG, "Failed to sync file output stream"); } try { fileOutputStream.close(); } catch (IOException e) { Log.e(LOG_TAG, "Failed to close file output stream", e); } rename(this.mNewName, this.mBaseName); } } @NonNull public File getBaseFile() { return this.mBaseName; } @NonNull public FileInputStream openRead() throws FileNotFoundException { if (this.mLegacyBackupName.exists()) { rename(this.mLegacyBackupName, this.mBaseName); } if (this.mNewName.exists() && this.mBaseName.exists() && !this.mNewName.delete()) { StringBuilder S = a.S("Failed to delete outdated new file "); S.append(this.mNewName); Log.e(LOG_TAG, S.toString()); } return new FileInputStream(this.mBaseName); } @NonNull public byte[] readFully() throws IOException { FileInputStream openRead = openRead(); try { byte[] bArr = new byte[openRead.available()]; int i = 0; while (true) { int read = openRead.read(bArr, i, bArr.length - i); if (read <= 0) { return bArr; } i += read; int available = openRead.available(); if (available > bArr.length - i) { byte[] bArr2 = new byte[available + i]; System.arraycopy(bArr, 0, bArr2, 0, i); bArr = bArr2; } } } finally { openRead.close(); } } @NonNull public FileOutputStream startWrite() throws IOException { if (this.mLegacyBackupName.exists()) { rename(this.mLegacyBackupName, this.mBaseName); } try { return new FileOutputStream(this.mNewName); } catch (FileNotFoundException unused) { if (this.mNewName.getParentFile().mkdirs()) { try { return new FileOutputStream(this.mNewName); } catch (FileNotFoundException e) { StringBuilder S = a.S("Failed to create new file "); S.append(this.mNewName); throw new IOException(S.toString(), e); } } else { StringBuilder S2 = a.S("Failed to create directory for "); S2.append(this.mNewName); throw new IOException(S2.toString()); } } } }