Home > OS >  What does the 'flush' argument of File.writeAsBytes do in Dart.io?
What does the 'flush' argument of File.writeAsBytes do in Dart.io?

Time:12-04

In the dart.io documentation for File.writeAsBytes

It says of the flush argument:

If the argument flush is set to true, the data written will be flushed to the file system before the returned future completes.

But I don't know what "flushed to the file system" means.

Why would someone knowledgable choose to set it to true?

Why would someone knowledgable choose to set it to false?

CodePudding user response:

Since I/O traditionally is very expensive, there are a number of different layers of buffers that exist between your code and the physical disk to try to batch/coalesce I/O operations for efficiency and to avoid making your code wait around for I/O operations to complete.

"Flushed to the file system" means that all buffered write operations have been written to the file system (which is an OS abstraction on top of the physical disk). For example, if you call File.writeAsBytes without flushing, something else that tries to read from that file might still see the old contents, even if the corresponding Future has completed. If you perform the write with flushing, then reads of that file should be guaranteed to see the new contents when the Future has completed.

Why would someone knowledgable choose to set it to true?

You would choose to explicitly flush if you want to know with certainty when data has been written.

Why would someone knowledgable choose to set it to false?

You might not want to flush every write because doing so would be slower. For example, if you're making many small writes, it'd be better to issue all of them without flushing and then possibly flush on the last write.

Note that this is a general operating system concept and isn't at all specific to Dart.

  • Related