Reputation: 17375
When I want to make sure that the changes of a MappedByteBuffer should be synced back to disc do I need randomAcccessFile.getFD().sync()
or mappedByteBuffer.force()
or both? (In my simple tests none of them seems to be required - confusing ...)
Somebody has an idea about the actual underlying operations or at least could explain the differences if any?
Upvotes: 2
Views: 1028
Reputation: 17375
First, FileDescriptor.sync is equivalent to FileChannel.force (calling the POSIX fsync method)
Second, in the Book "Java NIO" from Ron Hitchens (via google books) in the chapter about MappedByteBuffer it says
MappedByteBuffer.force() is similar to the method of the same name in the FileChannel class. It forces any changes made to the mapped buffer to be flushed out to permanent disc storage. When updating a file through MappedByteBuffer object, you should always use MappedByteBuffer.force() rather than FileChannel.force(). The channel object may not be aware of all file updates made through the mapped buffer. MappedByteBuffer doesn't give you the option of not flushing file metadata - it's always flushed too. Note, that the same considerations regarding nonlocal filesystems apply here as they do for FileChannel.force
So, yes. You need to call MappedByteBuffer.force!
BUT then I found this bug which indicates that both calls could be still necessary at least on Windows.
Upvotes: 2