Rabbit-R1/original r1/java/sources/androidx/media3/common/util/AtomicFile.java
2024-05-21 17:08:36 -04:00

117 lines
3.7 KiB
Java

package androidx.media3.common.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/* loaded from: classes2.dex */
public final class AtomicFile {
private static final String TAG = "AtomicFile";
private final File backupName;
private final File baseName;
public AtomicFile(File file) {
this.baseName = file;
this.backupName = new File(file.getPath() + ".bak");
}
public boolean exists() {
return this.baseName.exists() || this.backupName.exists();
}
public void delete() {
this.baseName.delete();
this.backupName.delete();
}
public OutputStream startWrite() throws IOException {
if (this.baseName.exists()) {
if (!this.backupName.exists()) {
if (!this.baseName.renameTo(this.backupName)) {
Log.w(TAG, "Couldn't rename file " + this.baseName + " to backup file " + this.backupName);
}
} else {
this.baseName.delete();
}
}
try {
return new AtomicFileOutputStream(this.baseName);
} catch (FileNotFoundException e) {
File parentFile = this.baseName.getParentFile();
if (parentFile == null || !parentFile.mkdirs()) {
throw new IOException("Couldn't create " + this.baseName, e);
}
try {
return new AtomicFileOutputStream(this.baseName);
} catch (FileNotFoundException e2) {
throw new IOException("Couldn't create " + this.baseName, e2);
}
}
}
public void endWrite(OutputStream outputStream) throws IOException {
outputStream.close();
this.backupName.delete();
}
public InputStream openRead() throws FileNotFoundException {
restoreBackup();
return new FileInputStream(this.baseName);
}
private void restoreBackup() {
if (this.backupName.exists()) {
this.baseName.delete();
this.backupName.renameTo(this.baseName);
}
}
/* loaded from: classes2.dex */
private static final class AtomicFileOutputStream extends OutputStream {
private boolean closed = false;
private final FileOutputStream fileOutputStream;
public AtomicFileOutputStream(File file) throws FileNotFoundException {
this.fileOutputStream = new FileOutputStream(file);
}
@Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
if (this.closed) {
return;
}
this.closed = true;
flush();
try {
this.fileOutputStream.getFD().sync();
} catch (IOException e) {
Log.w(AtomicFile.TAG, "Failed to sync file descriptor:", e);
}
this.fileOutputStream.close();
}
@Override // java.io.OutputStream, java.io.Flushable
public void flush() throws IOException {
this.fileOutputStream.flush();
}
@Override // java.io.OutputStream
public void write(int i) throws IOException {
this.fileOutputStream.write(i);
}
@Override // java.io.OutputStream
public void write(byte[] bArr) throws IOException {
this.fileOutputStream.write(bArr);
}
@Override // java.io.OutputStream
public void write(byte[] bArr, int i, int i2) throws IOException {
this.fileOutputStream.write(bArr, i, i2);
}
}
}