Persist User Data on a Virtual File

I need to know when a filre has changed while IntelliJ was down. I use a UserData on the Virtual File but that is not restored after a restart. Then I tried to use the BulkFileListener but I don't receive an update during the initial refresh.

So is there a way to psersist user data on a virtual file so that I can detect a change when IntelliJ / Plugin is restarted? Or is there a way to discover changes on a file when the IntelliJ is restarted?

- Andy

2 comments
Comment actions Permalink

You can persist arbitrary data using com.intellij.openapi.vfs.newvfs.FileAttribute.


0
Comment actions Permalink

Thanks that did the trick. If anybody needs to know how I did it this is the code:

 
private static final FileAttribute MODIFICATION_STAMP_FILE_ATTRIBUTE = new FileAttribute("modificationStampFileAttribute", 1, true);

@Nullable
public static long
getModificationStamp(VirtualFile file) {
    long ret = -1;
    Long temporary = file.getUserData(Util.MODIFICATION_DATE_KEY);
    if(temporary == null) {
        if(file instanceof NewVirtualFile) {
            final DataInputStream is = MODIFICATION_STAMP_FILE_ATTRIBUTE.readAttribute(file);
            if(is != null) {
                try {
                    try {
                        if(is.available() > 0) {
                            String value = IOUtil.readString(is);
                            ret = convertToLong(value, ret);
                            if(ret > 0) {
                                file.putUserData(Util.MODIFICATION_DATE_KEY, ret);
                            }
                        }
                    } finally {
                        is.close();
                    }
                } catch(IOException e) {
                    // Ignore it but we might need to throw an exception
                    
String message = e.getMessage();
                }
            }
        }
    }
    return ret;
}

public static void setModificationStamp(VirtualFile file) {
    // Store it in memory first
    
file.putUserData(Util.MODIFICATION_DATE_KEY, file.getModificationStamp());
    if(file instanceof NewVirtualFile) {
        final DataOutputStream os = MODIFICATION_STAMP_FILE_ATTRIBUTE.writeAttribute(file);
        try {
            try {
                IOUtil.writeString(StringUtil.notNullize(file.getTimeStamp() + ""), os);
            }
            finally {
                os.close();
            }
        }
        catch (IOException e) {
            // Ignore it but we might need to throw an exception
            
String message = e.getMessage();
        }
    }
}
0

Please sign in to leave a comment.