StreamUtils.copyRange overreads source stream in some cases #32695
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
Description
StreamUtils.copyRange copies the input stream with a buffer size of 4096 bytes. If the desired range is not divisible by 4096, copyRange will overread the input stream by reading whole buffers at a time. This will result in skipping the bytes in the input stream that were read into the buffer but not copied.
Consider the following kotlin code:
Because the input stream spans over two buffers, copyRange will consume the whole stream even though the requested range was 1000 bytes smaller than the stream length.
Actual behavior
When the requested range is not divisible by 4096 and the input stream is longer than the range copyRange will consume extra bytes after the end of the range. This advances the stream unnecessarily and results in those bytes being skipped.
Expected behavior
StreamUtils.copyRange should only read the requested number of bytes from the input stream.
Suggested fix
The number of remaining bytes to be read should be taken into account instead of reading the whole buffer:
The text was updated successfully, but these errors were encountered: