* Create a temp file
*/
final File temp = File.createTempFile(this.file.getName(), null);
final FileChannel tempChannel = new RandomAccessFile(temp, "rw")
.getChannel();
/*
* Copy entire edits tree, including the root file, to temp file
*/
for (final RegionSegment<PartialLoader> segment : this.edits) {
final PartialLoader loader = new ConstrainedPartialLoader(segment
.getData(), segment);
loader.transferTo(tempChannel);
}
this.channel.close();
tempChannel.close();
System.gc();
/*
* We're done with the original file. All changes are now in the temp file
* Try rename first, if it doesn't exist then do it by copy
*/
if (file.delete() == false) {
throw new IOException(
"Unable to delete original file during flushByCopy()");
}
if (temp.renameTo(file) == false) {
throw new IOException(
"Unable to move temporary file during flushByCopy()");
}
final String accessMode = (mode.isContent() ? "rw" : "r");
/*
* Now we need to reopen the channel
*/
this.channel = new RandomAccessFile(file, accessMode).getChannel();
}