should write in loop to ensure all data been written.