I had a similar issue with PHP. While I was getting a chunk of the data with
$chunkData = $stream->read(self::CHUNK_SIZE);
I would imagine that the size of the chunk would always be the same as self::CHUNK_SIZE except for the last chunk. So I was sending wrong values.