开发者

What is the diff between Java FileChannel.force() and StandardOpenOption.Sync?

开发者 https://www.devze.com 2023-04-03 07:02 出处:网络
I\'m not sure if the new Java 7 nio.file.Standa开发者_Python百科rdOpenOption is different from the old FileChannel.force() method.

I'm not sure if the new Java 7 nio.file.Standa开发者_Python百科rdOpenOption is different from the old FileChannel.force() method.

Is there a way to do O_DIRECT also?


I think the difference between the two is that StandardOpenOption does it automatically while you have to call FileChannel.force() to send the data to the underlying storage device. I'm not sure about O_DIRECT.


it is not possible to do O_DIRECT IO using the JVM. One of the reasons (I think) is, that the memory where the file contents go need to be aligned to some 512 bytes boundaries. Memory allocated with ByteBuffers don't fulfill this property.

Another problem which is similar to this 512 bytes alignment is that you can only perform IO operations that are multiples of 512 bytes. So if you want to read a file that has 700 bytes, you'll have troubles.

Here is a similar thread that presents these issues. I described a way in my blog how to implement direct IO into the JVM. It also contains a hint how you could add a O_DIRECT option to the StandardOpenOption class (you have to add the constant to the file /src/solaris/native/sun/nio/fs/genUnixConstants.c in the JDK sources)

0

精彩评论

暂无评论...
验证码 换一张
取 消