3

When opening (or creating) a file in a c++ application, you can give hints to the operating system to suite your needs. For example, if you want to customize the behaviour of the cache manager, you can disable write caching on a per-file basis.

Is there a way to achieve the same effect of what is possible in c++ when opening a file with the flag FILE_FLAG_WRITE_THROUGH?

from: https://msdn.microsoft.com/en-gb/library/windows/desktop/aa363858(v=vs.85).aspx#caching_behavior

A write-through request via FILE_FLAG_WRITE_THROUGH also causes NTFS to flush any metadata changes, such as a time stamp update or a rename operation, that result from processing the request.

I'm aware that this is possible with JNI but since, there is a similar option in POSIX (O_DIRECT) I wonder if java support this kind of feature somehow.

snovelli
  • 5,804
  • 2
  • 37
  • 50
  • 2
    Not really. Java by design abstracts such platform specifics away. This question is probably illuminating as it concerns the Linux equivalent of Windows' `FILE_FLAG_WRITE_THROUGH`: http://stackoverflow.com/questions/15228412/force-jvm-to-do-all-io-without-page-cache-e-g-o-direct I suspect the disparaging remarks about such unbuffered IO were made by persons who do not work regularly with stringent performance and/or data integrity requirements. – Andrew Henle May 18 '16 at 10:43

1 Answers1

1

There is StandardOpenOption.SYNC and StandardOpenOption.DSYNC:

From the Synchronized File I/O Integrity Documentation:

The SYNC and DSYNC options are used when opening a file to require that updates to the file are written synchronously to the underlying storage device. In the case of the default provider, and the file resides on a local storage device, and the seekable channel is connected to a file that was opened with one of these options, then an invocation of the write method is only guaranteed to return when all changes made to the file by that invocation have been written to the device. These options are useful for ensuring that critical information is not lost in the event of a system crash. If the file does not reside on a local device then no such guarantee is made. Whether this guarantee is possible with other provider implementations is provider specific.

Javadoc for SYNC and DSYNC options

In Linux/MacOS systems, this translates to the SYNC/DSYNC options for the open function for opening files.

In Windows, either of those options being set translates to using the FILE_FLAG_WRITE_THROUGH option, which can be seen in the source in WindowsChannelFactory:

if (flags.dsync || flags.sync)
        dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;

To use these flags, if you are unfamiliar with the nio File API in Java it goes something like this:

Path file = Paths.get("myfile.dat");
SeekableByteChannel c = Files.newByteChannel(file, StandardOpenOption.SYNC);

you can use the channel directly to read/write data using byte buffers, or convert to a familiar input stream or output stream using the Channels class:

InputStream is = Channels.newInputStream(c);    
prunge
  • 22,460
  • 3
  • 73
  • 80