I'm working with TStringList with TMemeorySteam a lot in my project.
  msTmp := TMemoryStream.Create;
  try
     lTemp.SaveToStream(msTmp, TEncoding.Unicode);
  finally
    msTmp.Free;
  end;
  .....
  lTemp := TStringList.Create;
  try
     lTemp.LoadFromFile(msTmp, TEncoding.Unicode);
  finally
    lTemp.Free;
  end;
How i can tell the LoadFromStream() and SaveToStream()   that the stream is UTF-16 encoded by default in my project without including the encoded type in each call.
so i can call LoadFromStream(msTmp) only and it will load with UTF-16 encoded.
Have a look at the TStrings.DefaultEncoding property.
The default encoding for the current object.
DefaultEncoding is used when the nil encoding is specified in a call to
LoadFromStreamorSaveToStream.By default, DefaultEncoding is set to
Default. The user can change DefaultEncoding if another default encoding is desired forLoadFromStreamorSaveToStream.
However, watch out for the TStrings.Encoding property:
Character encoding determined during reading from a stream or file.
Encodingis a read-only property that contains the value of the character encoding detected when theLoadFromStreamorLoadFromFilemethods are called. If a file or stream does not contain a BOM (the encoding value cannot be detected) thenEncodingis set to the value specified in theDefaultEncodingproperty.
Encodingis used in theSaveToStreamandSaveToFilemethods.
If the
Encodingparameter [ofLoadFromStream] is not given, then the strings are loaded using the appropriate encoding. The value of the encoding is obtained by calling theGetBufferEncodingroutine of theTEncodingclass.LoadFromStreamthen saves the value of the encoding in theEncodingproperty, to be used if the stream is saved.
So, as long as you do not call LoadFrom...(), you can set DefaultEncoding to TEncoding.Unicode and then call SaveTo...() without specifying a value for the Encoding parameter.
But, once you call LoadFrom...(), the Encoding property takes priority over the DefaultEncoding property for subsequent calls to SaveTo...(). As long as the files are not BOM'ed, the Encoding property will match the DefaultEncoding property.  But if a non-UTF16LE BOM is encountered, all bets are off.
To ensure TEncoding.Unicode is always used, you should continue using the Encoding parameter of LoadFrom...() and SaveTo...(), that is why they exist.  They take first priority,  the (Default)Encoding properties are fallbacks when input/output encodings are not explicitly stated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With