130 lines
4.0 KiB
Java
130 lines
4.0 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 K = a.K("Couldn't rename file ");
|
||
|
K.append(this.mBaseName);
|
||
|
K.append(" to backup file ");
|
||
|
K.append(this.mBackupName);
|
||
|
Log.w("AtomicFile", K.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 K2 = a.K("Couldn't create ");
|
||
|
K2.append(this.mBaseName);
|
||
|
throw new IOException(K2.toString());
|
||
|
}
|
||
|
} else {
|
||
|
StringBuilder K3 = a.K("Couldn't create directory ");
|
||
|
K3.append(this.mBaseName);
|
||
|
throw new IOException(K3.toString());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|