I found a workaround to the code that raised this issue, which is actually much nicer code. However it relies on the existence of the leastSize property. Unfortunately this property is scheduled for deprecation. This means you have to work with fixed size buffers rather than using a dynamic sized buffer, to avoid deprecated features. Obviously if read(buffer, IOMode) return the number of ubytes read (à la UNIX API style) then there is no actual problem, but being able to read a whole available buffer at a time is a really nice optimisation. One that apparently is being deprecated.

Unless I am just using the wrong algorithm and/or am missing a better way of doing things.