FileDocCategorySizeDatePackage
JournaledFile.javaAPI DocAndroid 5.1 API3657Thu Mar 12 22:22:10 GMT 2015com.android.internal.util

JournaledFile

public class JournaledFile extends Object
deprecated
Use {@link com.android.internal.os.AtomicFile} instead. It would be nice to update all existing uses of this to switch to AtomicFile, but since their on-file semantics are slightly different that would run the risk of losing data if at the point of the platform upgrade to the new code it would need to roll back to the backup file. This can be solved... but is it worth it and all of the testing needed to make sure it is correct?

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.FilechooseForRead()
Returns the file for you to read.

more
Prefers the real file. If it doesn't exist, uses the temp one, and then copies it to the real one. If there is both a real file and a temp one, assumes that the temp one isn't fully written and deletes it.

        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.FilechooseForWrite()
Returns a file for you to write.

more
If a write is already happening, throws. In other words, you must provide your own locking.

Call {@link #commit} to commit the changes, or {@link #rollback} to forget the changes.

        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 voidcommit()
Commit changes.

        if (!mWriting) {
            throw new IllegalStateException("no file to commit");
        }
        mWriting = false;
        mTemp.renameTo(mReal);
    
public voidrollback()
Roll back changes.

        if (!mWriting) {
            throw new IllegalStateException("no file to roll back");
        }
        mWriting = false;
        mTemp.delete();