Skip to content

Instantly share code, notes, and snippets.

@tateisu
Created April 7, 2012 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tateisu/2329685 to your computer and use it in GitHub Desktop.
Save tateisu/2329685 to your computer and use it in GitHub Desktop.
private void loadFromDiskLocked() {
//(snip)
if (mBackupFile.exists()) {
mFile.delete();
mBackupFile.renameTo(mFile);
}
//(snip)
if (FileUtils.getFileStatus(mFile.getPath(), stat) && mFile.canRead()) {
//(snip) ここでファイルから読み込む
}
//(snip)
}
// Note: must hold mWritingToDiskLock
private void writeToFile(MemoryCommitResult mcr) {
// Rename the current file so it may be used as a backup during the next read
if (mFile.exists()) {
//(snip)
if (!mBackupFile.exists()) {
if (!mFile.renameTo(mBackupFile)) {
Log.e(TAG, "Couldn't rename file " + mFile
+ " to backup file " + mBackupFile);
mcr.setDiskWriteResult(false);
return;
}
} else {
mFile.delete();
}
}
// Attempt to write the file, delete the backup and return true as atomically as
// possible. If any exception occurs, delete the new file; next time we will restore
// from the backup.
try {
FileOutputStream str = createFileOutputStream(mFile);
//(snip)
// Writing was successful, delete the backup file if there is one.
mBackupFile.delete();
//(snip)
return;
} catch (XmlPullParserException e) {
Log.w(TAG, "writeToFile: Got exception:", e);
} catch (IOException e) {
Log.w(TAG, "writeToFile: Got exception:", e);
}
// Clean up an unsuccessfully written file
if (mFile.exists()) {
if (!mFile.delete()) {
Log.e(TAG, "Couldn't clean up partially-written file " + mFile);
}
}
//(snip)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment