return number of bytes written more consistently

This is at least a partial solution to issue #1701