Add support for length-provided streams.
(cherry picked from commit 4b6490e)