JournaledFilepublic class JournaledFile extends Object
Fields Summary |
---|
File | mReal | File | mTemp | boolean | mWriting |
Constructors Summary |
---|
public JournaledFile(File real, File temp)
mReal = real;
mTemp = temp;
|
Methods Summary |
---|
public java.io.File | chooseForRead()Returns the file for you to read.
File result;
if (mReal.exists()) {
result = mReal;
if (mTemp.exists()) {
mTemp.delete();
}
} else if (mTemp.exists()) {
result = mTemp;
mTemp.renameTo(mReal);
} else {
return mReal;
}
return result;
| public java.io.File | chooseForWrite()Returns a file for you to write.
if (mWriting) {
throw new IllegalStateException("uncommitted write already in progress");
}
if (!mReal.exists()) {
// If the real one doesn't exist, it's either because this is the first time
// or because something went wrong while copying them. In this case, we can't
// trust anything that's in temp. In order to have the chooseForRead code not
// use the temporary one until it's fully written, create an empty file
// for real, which will we'll shortly delete.
try {
mReal.createNewFile();
} catch (IOException e) {
// Ignore
}
}
if (mTemp.exists()) {
mTemp.delete();
}
mWriting = true;
return mTemp;
| public void | commit()Commit changes.
if (!mWriting) {
throw new IllegalStateException("no file to commit");
}
mWriting = false;
mTemp.renameTo(mReal);
| public void | rollback()Roll back changes.
if (!mWriting) {
throw new IllegalStateException("no file to roll back");
}
mWriting = false;
mTemp.delete();
|
|