If you are able to change the
TMemoryStream to its descendant
TBytesStream you can just use the
Convert function from
if curEncoding <> TEncoding.UTF8 then begin
bytes := TEncoding.Convert(curEncoding, TEncoding.UTF8, stream.Bytes);
stream := TBytesStream.Create(bytes);
Not sure if it is the most efficient way, but at least it is one way and it only needs a couple of lines, which in turn is also some sort of efficiency.