Using FileWriter.truncate() to extend the length of a file?

I noticed that FileWriter.truncate() can only be used to shorten a file, and
there does not seem to be a good way to grow a file using FileWriter without
appending data to it.  By contrast, the POSIX ftruncate function can be used
to grow a file (zero padding it):

>From ftruncate(2):
  "If the file previously was larger than this size, the extra data is lost.
 If  the  file  previously
   was shorter, it is extended, and the extended part reads as null bytes
('\0')."

I think there are use cases for wanting to grow a file in advance of writing
data to it.  For example, you might implement a system that downloads chunks
of a file in parallel using multiple XMLHttpRequest objects.  The chunks may
arrive out-of-order.

A possible alternative API would be to support seeking beyond the end of the
file or writing to a starting offset that is beyond the length fo the file.
 It may also be reasonable to support all of those in addition to truncating
to an offset greater than the length of the file.

Regards,
-Darin

Received on Thursday, 26 August 2010 21:05:28 UTC