XaskeL

Untitled

Oct 17th, 2019
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package androidx.core.util;
  2.  
  3. import android.util.Log;
  4. import androidx.annotation.NonNull;
  5. import androidx.annotation.Nullable;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. public class AtomicFile {
  13. private final File mBackupName;
  14. private final File mBaseName;
  15.  
  16. public AtomicFile(@NonNull File file) {
  17. this.mBaseName = file;
  18. StringBuilder sb = new StringBuilder();
  19. sb.append(file.getPath());
  20. sb.append(".bak");
  21. this.mBackupName = new File(sb.toString());
  22. }
  23.  
  24. @NonNull
  25. public File getBaseFile() {
  26. return this.mBaseName;
  27. }
  28.  
  29. public void delete() {
  30. this.mBaseName.delete();
  31. this.mBackupName.delete();
  32. }
  33.  
  34. @NonNull
  35. public FileOutputStream startWrite() throws IOException {
  36. if (this.mBaseName.exists()) {
  37. if (this.mBackupName.exists()) {
  38. this.mBaseName.delete();
  39. } else if (!this.mBaseName.renameTo(this.mBackupName)) {
  40. StringBuilder sb = new StringBuilder();
  41. sb.append("Couldn't rename file ");
  42. sb.append(this.mBaseName);
  43. sb.append(" to backup file ");
  44. sb.append(this.mBackupName);
  45. Log.w("AtomicFile", sb.toString());
  46. }
  47. }
  48. try {
  49. return new FileOutputStream(this.mBaseName);
  50. } catch (FileNotFoundException unused) {
  51. if (this.mBaseName.getParentFile().mkdirs()) {
  52. try {
  53. return new FileOutputStream(this.mBaseName);
  54. } catch (FileNotFoundException unused2) {
  55. StringBuilder sb2 = new StringBuilder();
  56. sb2.append("Couldn't create ");
  57. sb2.append(this.mBaseName);
  58. throw new IOException(sb2.toString());
  59. }
  60. } else {
  61. StringBuilder sb3 = new StringBuilder();
  62. sb3.append("Couldn't create directory ");
  63. sb3.append(this.mBaseName);
  64. throw new IOException(sb3.toString());
  65. }
  66. }
  67. }
  68.  
  69. public void finishWrite(@Nullable FileOutputStream fileOutputStream) {
  70. if (fileOutputStream != null) {
  71. sync(fileOutputStream);
  72. try {
  73. fileOutputStream.close();
  74. this.mBackupName.delete();
  75. } catch (IOException e) {
  76. Log.w("AtomicFile", "finishWrite: Got exception:", e);
  77. }
  78. }
  79. }
  80.  
  81. public void failWrite(@Nullable FileOutputStream fileOutputStream) {
  82. if (fileOutputStream != null) {
  83. sync(fileOutputStream);
  84. try {
  85. fileOutputStream.close();
  86. this.mBaseName.delete();
  87. this.mBackupName.renameTo(this.mBaseName);
  88. } catch (IOException e) {
  89. Log.w("AtomicFile", "failWrite: Got exception:", e);
  90. }
  91. }
  92. }
  93.  
  94. @NonNull
  95. public FileInputStream openRead() throws FileNotFoundException {
  96. if (this.mBackupName.exists()) {
  97. this.mBaseName.delete();
  98. this.mBackupName.renameTo(this.mBaseName);
  99. }
  100. return new FileInputStream(this.mBaseName);
  101. }
  102.  
  103. @NonNull
  104. public byte[] readFully() throws IOException {
  105. FileInputStream openRead = openRead();
  106. try {
  107. byte[] bArr = new byte[openRead.available()];
  108. int i = 0;
  109. while (true) {
  110. int read = openRead.read(bArr, i, bArr.length - i);
  111. if (read <= 0) {
  112. return bArr;
  113. }
  114. i += read;
  115. int available = openRead.available();
  116. if (available > bArr.length - i) {
  117. byte[] bArr2 = new byte[(available + i)];
  118. System.arraycopy(bArr, 0, bArr2, 0, i);
  119. bArr = bArr2;
  120. }
  121. }
  122. } finally {
  123. openRead.close();
  124. }
  125. }
  126.  
  127. private static boolean sync(@NonNull FileOutputStream fileOutputStream) {
  128. try {
  129. fileOutputStream.getFD().sync();
  130. return true;
  131. } catch (IOException unused) {
  132. return false;
  133. }
  134. }
  135. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×