File.chunks fails to read the entire file for some file-like objects
|Reported by:||Ian Kelly||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||no|
The File.chunks method gets the size of the file and then decrements a counter by the chunk size for each read until it reaches 0. The problem is that the read method is not guaranteed to return the full amount requested, and so the amount read may be less than the chunk size, resulting in the counter being decremented erroneously. I ran into this bug while using a file-like object that wraps a java.io.InputStream object, whose read method returns as soon as data is available.
This patch is one possible solution, although it still has the problem that it will result in a busy-loop if the read method is entirely non-blocking. It also potentially yields non-terminal chunks smaller than the chunk size, which may not be desirable.
See also #9632, which may affect the implementation here if it is accepted.