java – 将FileChannel#write总是写入整个缓冲区吗?

(这与Would FileChannel.read read less bytes than specified if there’s enough data?相关(或更确切地说是“相反”))

TL; DR:

这总是会写出整个缓冲区……

ByteBuffer bytes = ...;
fileOutputStream.getChannel().write(bytes);

…或者是否有必要使用这样的循环:

ByteBuffer bytes = ...;
while (bytes.remaining() > 0)
{
    fileOutputStream.getChannel().write(bytes);
}

由于comment in another answer,我想询问是否有关于通过调用FileChannel#write(ByteBuffer)将缓冲区写入FileChannel的行为的任何保证.

仅供参考:文档说明

Writes a sequence of bytes to this channel from the given buffer.

Bytes are written starting at this channel’s current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified by the WritableByteChannel interface.

以及被覆盖方法的文档,WritableByteChannel#write(ByteBuffer)

Writes a sequence of bytes to this channel from the given buffer.

An attempt is made to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, src.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is written, where 0 <= n <= r. This byte sequence will be transferred from the buffer starting at index p, where p is the buffer’s position at the moment this method is invoked; the index of the last byte written will be p + n – 1. Upon return the buffer’s position will be equal to p + n; its limit will not have changed.

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket’s output buffer.

This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.

Parameters:
src – The buffer from which bytes are to be retrieved

Returns: The number of bytes written, possibly zero

在上面提到的关于从FileChannel阅读的问题中,在评论中已经对该文档的确切措辞和解释进行了一些讨论.我认为文档中的关键区别在于对于read方法,文档说

A read operation might not fill the buffer, and in fact it might not read any bytes at all.

与此相反,write方法的文档说

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all.

对我来说,这意味着FileChannel上的写操作只会在写完所有字节后返回,因为文档中没有另外指定(除了返回值可能为0的语句,但这显然是一个工件)从被覆盖的方法)

从我的文件大小高达80 MB(!)的测试中,写操作总是立即写入整个缓冲区.但当然,这只是一个考验,并不足以作出深刻的陈述.我试图跟踪related OpenJDK classes中的调用,但这些调用很快就会分散到不同的本机实现中 – 毕竟,这不应该是必要的……

解决方法:

不,不能保证write()会耗尽整个缓冲区.文档确实试图建立实现应该一次性写入所有字节的期望,但是它没有做出任何承诺:

Unless otherwise specified, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state[1], may write only some of the bytes or possibly none at all.

FileChannel.write()同样为不完整的写入留出了空间:

Writes a sequence of bytes to this channel from the given buffer.

Bytes are written starting at this channel’s current file position unless the channel is in append mode, in which case the position is first advanced to the end of the file. The file is grown, if necessary, to accommodate the written bytes, and then the file position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified by the WritableByteChannel interface.

因此,虽然文本暗示完整写入是一般情况而不完整写入是例外,但它为未来(能够)遵守这种一般情况的替代/未来实现留下了空间.

正如您所指出的,这与read()的方法不同.我想这是因为,在整合文档时,所有已知和预期的实现都遵循执行完整写入的一般情况.

[1]这可能是对非阻塞频道的引用.

上一篇:docker安装mysql


下一篇:Centos7在线安装MySQL数据库