有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将FileChannel#写入始终写入整个缓冲区?

(这与Would FileChannel.read read less bytes than specified if there's enough data?有关(或者更确切地说与之相反)

TL;博士

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

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

。。。或者有必要使用这样的循环:

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

?


由于comment in another answer,我想问一下,对于通过调用^{}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.

以及重写方法的文档,^{}

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


在上述关于从a ^{阅读的问题中,评论中对本文件的确切措辞和解释进行了一些讨论。我认为文档中的关键区别在于,对于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的语句,但这显然是重写方法的产物)

根据我的测试,文件大小高达80MB(!),写入操作总是一次写入整个缓冲区。但当然,这只是一个测试,不足以做出深刻的陈述。我试图跟踪related OpenJDK classes中的调用,但这些调用很快就会分化为不同的本机实现——毕竟,这应该不是必需的


共 (1) 个答案

  1. # 1 楼答案

    不,不能保证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.

    文件频道。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]这可能是指非阻塞通道