function IsUnicodeStream(const Stm: Classes.TStream): Boolean;
var
StmPos: LongInt; // current position in stream
UnicodeMarker: Word; // marker that indicates a unicode stream
begin
// Record current location in stream
StmPos := Stm.Position;
// Check if stream large enough to contain unicode marker
if StmPos <= Stm.Size - SizeOf(Word) then
begin
// Read first word and check if it is the unicode marker
Stm.ReadBuffer(UnicodeMarker, SizeOf(UnicodeMarker));
Result := (UnicodeMarker = $FEFF);
// Restore stream positions
Stm.Position := StmPos;
end
else
// Stream too small: can't be unicode
Result := False;
end;
//delphi/2231