I have a class 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 way to force()
other than close()
it.
Is FileChannel#force
is equivalent to OutputStream#flush
? Do I always have to call it?
Do I have to do like this?
@Override
public void close() {
if (channel instanceof FileChannel) throws IOException {
((FileChannel) channel).force(); // general solution?
}
channel.close();
}
channel.close();
doesn’t callforce()
internally, why should yourclose()
method do it? – PacksaddleFileChannel
would be wrapped in my class as an instance ofWritableFileChannel
? Thanks. – Autotomizeclose()
method delegates to the wrapped channel’sclose()
method you already have exactly the same behavior, including all mandatory actions performed by thatclose()
method. There are only two scenarios 1) callingforce()
is required. In that case you don’t need to call it, becausechannel.close();
will already do it. 2) callingforce()
is not required. In that case you don’t need to call it, because it is not required. So, in either case, you don’t need to call it. That’s what makes the question so strange. By the way, the answer is 2). – Packsaddle