fix possible encoding errors in output
close buffer before returning. don^t decode to unicode, we^re exporting as utf-8