130 lines
4 KiB
Java
130 lines
4 KiB
Java
|
package androidx.core.util;
|
||
|
|
||
|
import android.util.Log;
|
||
|
import androidx.annotation.NonNull;
|
||
|
import androidx.annotation.Nullable;
|
||
|
import c.d.b.a.a;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.FileNotFoundException;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.io.IOException;
|
||
|
public class AtomicFile {
|
||
|
private final File mBackupName;
|
||
|
private final File mBaseName;
|
||
|
|
||
|
public AtomicFile(@NonNull File file) {
|
||
|
this.mBaseName = file;
|
||
|
this.mBackupName = new File(file.getPath() + ".bak");
|
||
|
}
|
||
|
|
||
|
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.mBackupName.delete();
|
||
|
}
|
||
|
|
||
|
public void failWrite(@Nullable FileOutputStream fileOutputStream) {
|
||
|
if (fileOutputStream != null) {
|
||
|
sync(fileOutputStream);
|
||
|
try {
|
||
|
fileOutputStream.close();
|
||
|
this.mBaseName.delete();
|
||
|
this.mBackupName.renameTo(this.mBaseName);
|
||
|
} catch (IOException e) {
|
||
|
Log.w("AtomicFile", "failWrite: Got exception:", e);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void finishWrite(@Nullable FileOutputStream fileOutputStream) {
|
||
|
if (fileOutputStream != null) {
|
||
|
sync(fileOutputStream);
|
||
|
try {
|
||
|
fileOutputStream.close();
|
||
|
this.mBackupName.delete();
|
||
|
} catch (IOException e) {
|
||
|
Log.w("AtomicFile", "finishWrite: Got exception:", e);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@NonNull
|
||
|
public File getBaseFile() {
|
||
|
return this.mBaseName;
|
||
|
}
|
||
|
|
||
|
@NonNull
|
||
|
public FileInputStream openRead() throws FileNotFoundException {
|
||
|
if (this.mBackupName.exists()) {
|
||
|
this.mBaseName.delete();
|
||
|
this.mBackupName.renameTo(this.mBaseName);
|
||
|
}
|
||
|
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.mBaseName.exists()) {
|
||
|
if (this.mBackupName.exists()) {
|
||
|
this.mBaseName.delete();
|
||
|
} else if (!this.mBaseName.renameTo(this.mBackupName)) {
|
||
|
StringBuilder L = a.L("Couldn't rename file ");
|
||
|
L.append(this.mBaseName);
|
||
|
L.append(" to backup file ");
|
||
|
L.append(this.mBackupName);
|
||
|
Log.w("AtomicFile", L.toString());
|
||
|
}
|
||
|
}
|
||
|
try {
|
||
|
return new FileOutputStream(this.mBaseName);
|
||
|
} catch (FileNotFoundException unused) {
|
||
|
if (this.mBaseName.getParentFile().mkdirs()) {
|
||
|
try {
|
||
|
return new FileOutputStream(this.mBaseName);
|
||
|
} catch (FileNotFoundException unused2) {
|
||
|
StringBuilder L2 = a.L("Couldn't create ");
|
||
|
L2.append(this.mBaseName);
|
||
|
throw new IOException(L2.toString());
|
||
|
}
|
||
|
} else {
|
||
|
StringBuilder L3 = a.L("Couldn't create directory ");
|
||
|
L3.append(this.mBaseName);
|
||
|
throw new IOException(L3.toString());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|