Copy encoding from input text to output

Also return nil for missing metadata, instead of calling
rb_str_new2(NULL), which crashes