fix encode, need to call Flush