Is the power of FileChannel # equal to the equivalent of OutputStream # flush? Should I always call it?

I have a class that works like FilterOutputStream .

 public class WritableFilterChannel implements WritableChannel { public WritableFilterChannel(final WritableByteChannel channel) { super(); this.channel = channel; } // isOpen(), close() delegates to the channel // write(ByteBuffer) overridden to work differently protected WritableByteChannel channel; } 

When I pass an instance of FileChannel , there is no path to force() other than close() .

Is FileChannel#force equivalent to OutputStream#flush ? Do I always need this?

Should I do this?

 @Override public void close() { if (channel instanceof FileChannel) throws IOException { ((FileChannel) channel).force(); // general solution? } channel.close(); } 
0
source share
1 answer

"Equivalent" is too strong a word. FileChannel.force(false) is similar to OutputStream.flush() . FileChannel force () offers more reliable guarantees of the state of a file after it is returned than the OutputStream flush () method.

Obviously, you do not need to close () the FileChannel that you invoked the force () method on. You should only close the channel when you're done with it. However, there is no guarantee that closing the channel will cause the equivalent of a force operation on it. If you need behavior that force () indicates as part of closing the channel, you must explicitly name it the way you do in your closing ().

+1
source

Source: https://habr.com/ru/post/1336234/


All Articles