filechannel.force を呼び出して例外をスローするとどうなりますか?
filechannel.write が成功し、バッファ コンテンツ全体をファイル チャネルに書き込むことができたとき、すべてがディスクにフラッシュされたかどうかを確認したいと思います。例外をスローできる force(true) を呼び出します。ファイルチャネルに書き込んだデータはどうなりますか? ディスクに何もフラッシュされませんでしたか、それともデータの一部がフラッシュされた可能性がありますか? 操作をロールバックするにはどうすればよいですか?
public boolean flush(ByteBuffer[] buffers, long buffersRemaining)
{
try
{
FileChannel fileChannel = new RandomAccessFile(target, "rw").getChannel();
long writtenBytes = fileChannel.write(buffers);
//we think we have written all the data into filechannel
if(writtenBytes == buffersRemaining)
{
//we try to flush, but we get an exception
try
{
fileChannel.force(true);
fileChannel.close();
}
catch(IOException exception)
{
//???
return false;
}
}
}
catch(Exception exception)
{
//log exception
return false;
}
}