As a FYI: this snippet doesn't handle partial writes.