Set buffer length to bytes read before writing. Fixes #32.

The code works on Unix systems without this, but fails on Windows. It is also incorrect to rely on C for things like these to be predictable.