Would FileChannel.read read less bytes than specified if there's enough data?

后端 未结 2 526
温柔的废话
温柔的废话 2020-12-22 01:09

For example I have a file whose content is:

abcdefg

then i use the following code to read \'defg\'.

ByteBuffer bb = ByteBuf         


        
2条回答
  •  孤城傲影
    2020-12-22 01:35

    No, in general you cannot assume that the number of bytes read will be equal to the number of bytes requested, even if there are bytes left to be read in the file.

    If you are reading from a local file, chances are that the number of bytes requested will actually be read, but this is by no means guaranteed (and won't likely be the case if you're reading a file over the network).

    See the documentation for the ReadableByteChannel.read(ByteBuffer) method (which applies for FileChannel.read(ByteBuffer) as well). Assuming that the channel is in blocking mode, the only guarantee is that at least one byte will be read.

提交回复
热议问题