java - FileChannel#write 总是会写入整个缓冲区吗?

标签 java filechannel

(这与 Would FileChannel.read read less bytes than specified if there's enough data? 相关(或者说是“相反”)

长话短说:

这会一直写入整个缓冲区吗...

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

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

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

?


由于 comment in another answer ,我想问一下,通过调用 FileChannel#write(ByteBuffer)Buffer 写入 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 中的调用,但这些很快就会分化为不同的 native 实现 - 毕竟,这不是必需的......

最佳答案

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

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.

因此,虽然文本暗示完整写入是一般情况,不完整写入是异常(exception)情况,但它为可能无法(能够)遵守这种一般情况的替代/ future 实现敞开大门。

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


[1] 这可能是对非阻塞 channel 的引用。

关于java - FileChannel#write 总是会写入整个缓冲区吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29945685/

相关文章:

java - 如何修复c3p0连接池初始化异常?

java - FileChannel -> BufferedReader -> Reader 的缺点

java - NIO GZIP 压缩和文件复制

java - FileChannel#force 等同于 OutputStream#flush 吗?我总是需要调用它吗?

java - 使用文件 channel 随机访问文件?

java - 为什么 JTextComponent.setText(String) 不规范化行结尾?

java - 在 View 之间导航时如何保留 Vaadin View 实例

java - 最简单的设置和忘记分布式数据库来存储简单的 JSON 类结构?

java - JAVA 中的对象数组给出 InputMismatchException

java - 使用 Java 7 AsynchronousFileChannel 附加到文件