Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package androidx.core.util;
- import android.util.Log;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- 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;
- StringBuilder sb = new StringBuilder();
- sb.append(file.getPath());
- sb.append(".bak");
- this.mBackupName = new File(sb.toString());
- }
- @NonNull
- public File getBaseFile() {
- return this.mBaseName;
- }
- public void delete() {
- this.mBaseName.delete();
- this.mBackupName.delete();
- }
- @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 sb = new StringBuilder();
- sb.append("Couldn't rename file ");
- sb.append(this.mBaseName);
- sb.append(" to backup file ");
- sb.append(this.mBackupName);
- Log.w("AtomicFile", sb.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 sb2 = new StringBuilder();
- sb2.append("Couldn't create ");
- sb2.append(this.mBaseName);
- throw new IOException(sb2.toString());
- }
- } else {
- StringBuilder sb3 = new StringBuilder();
- sb3.append("Couldn't create directory ");
- sb3.append(this.mBaseName);
- throw new IOException(sb3.toString());
- }
- }
- }
- 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);
- }
- }
- }
- 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);
- }
- }
- }
- @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();
- }
- }
- private static boolean sync(@NonNull FileOutputStream fileOutputStream) {
- try {
- fileOutputStream.getFD().sync();
- return true;
- } catch (IOException unused) {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment