Close and flush the buffer