The previous fix didn't handle all cases correctly. Instead, add a new function to raw_ostream (SetNumBytesInBuffer) to allow always using the full buffer size, and revamp write_impl to more cleanly handle all cases.