1. leave loop while buf delete. 2. optimize code