Closed mohd-akram closed 1 month ago
Per documentation, IO#read
does not guarantee to fill the entire slice.
It reads as much data as is immediately available on the socket buffer. This can of course differ depending on utilization of the connection which explains why the code may fill the entire slice sometimes but sometimes not.
If you want to ensure filling the slice, you'll need to do repeated reads until there is no data left. IO#read_fully
can be useful for that, but it expects there to be enough data available for a fill. You might also consider IO.copy
which has no size requirements.
Thanks for clarifying. IO.copy
seems to have the same caveat ("at most"). It would be nice to have a method that reads as much as possible up to a limit. It seems #11896 might address this.
IO.copy
continues reading until there is no more data available (i.e. IO#read
returns 0).
It seems the documentation needs some clarification.
Bug Report
I had this bit of working code before, but the behavior seems to have changed at some point. Run this and open Chrome, paste a good amount of text and submit the form. You should get a 400 error. For some reason
part.body.read
does not attempt to fill the buffer even though there's plenty of space left. I expect it to return size < slice size only if there isn't more data to read. This was the behavior before.