0

Given a FileChannel with length x and a Bytebuffer also of length of x. What are the circunstances where I read less than x bytes from the file system ?. Considering that the position of the channel will be always zero (there will be always data to be read) and size of x can vary from few bytes to whatever big number you can think of.

The only scenario I could identify was when you try to read into an empty buffer / array. My question is more specifically about the underlying JVM / OS behavior.

Sample code

int x = 100;
RandomAccessFile raf = new RandomAccessFile(new File("test"), "rw");
raf.setLength(x);
FileChannel channel = raf.getChannel();
ByteBuffer bb = ByteBuffer.allocate(x);
int read = channel.read(bb, 0);
Josh
  • 154
  • 1
  • 9
  • 1
    This is probably a duplicate of https://stackoverflow.com/q/3967147/3182664 , although, to be honest, when I had a [related question](https://stackoverflow.com/q/29945685/3182664), I stumbled over the question regarding `read`, and found the answer there not really satisfactory :-/ it seems to boil down to: You never know, so to be sure, you may have to use some loop to be sure to read all bytes... – Marco13 Feb 02 '18 at 19:42
  • Thanks @Marco13 for pointing that out. It seems you're right, loop is the way to go here. – Josh Feb 02 '18 at 19:54

0 Answers0