memory_output_stream deals with all required pointer arithmetic and allows easy transition to fragmented buffers.