If the given size is less than the file's current size then the file is truncated, discarding any bytes beyond the new end of the file. If the given size is greater than or equal to the file's current size then the file is not modified. In either case, if this channel's file position is greater than the given size then it is set to that size.
I was truncating the file by doing:
channel.truncate(0)
At this point, I was expecting the position() of the channel to be set to 0 - i.e. the begining of the file.
What was really happening, though, was that the FileChannel was not resetting it's position to 0 - in fact the position stayed where it was. And I would see NULL bytes for the size of the file before the truncation prefixed to the new data that is being written to the file.
The easy fix, of course, was to:
channel.truncate(0);
channel.position(0);
As easy as the fix is, it did waste an hour of my work, that did not deserve to be wasted.
My configuration:
Intel MBP 2009 Snow Leopard.
Java 6.
I am not sure if this is a platform specific issue, but will find out soon.
Good Luck!