Skip to main content

StreamToStringUTF16

function StreamToStringUTF16(M : TStream) : string

Example

procedure ScriptEvent(var Value: variant);
var
Buffer: TMemoryStream;
begin
Buffer := TMemoryStream.Create;
try
StringToStreamUTF16('AB', Buffer);
Value := StreamToStringUTF16(Buffer);
finally
Buffer.Free;
end;
end;

Usage

StreamToStringUTF16 reads an entire stream from the beginning and decodes it as little-endian UTF-16.

Parameters

NameTypeDescription
MTStreamExisting readable, seekable caller-owned stream. Its incoming position is ignored.

Returns

The decoded Velox UTF-16 string. Empty input returns an empty string.

Errors

Nil-object, seek, read and allocation failures propagate. Odd length and malformed surrogate structure do not themselves raise in this installed decoder.

Usage notes

Use this only for an explicitly UTF-16LE payload. If a BOM is part of the transport contract and should be consumed, use StreamToString.

Additional Technical Info

StreamToStringUTF16 interprets the complete stream as UTF-16 little-endian code units. It selects the byte order explicitly; it does not inspect a BOM or switch encodings.

The example round-trips fictional text through the matching writer. It is source-reviewed and is not executed by the documentation workflow.

Implementation

The wrapper delegates to MemoryStreamToString(M, TEncoding.Unicode). After the shared rewind/read step, installed TUnicodeEncoding.GetString calculates ByteCount div 2 characters and copies each little-endian 16-bit code unit into the result.

Edge cases and quirks

  • A leading FF FE BOM is decoded as U+FEFF because explicit decoding does not skip a preamble.
  • An odd final byte is silently ignored by the current Delphi ByteCount div 2 implementation.
  • The decoder copies UTF-16 code units without validating surrogate pairing. Unpaired surrogates can therefore remain in the resulting Delphi string.
  • Big-endian bytes decode incorrectly rather than triggering byte-order detection.
  • The helper rewinds the stream, ignores a short Read result and has a signed-LongInt size boundary.

Side effects

Changes M.Position; does not modify or free the stream.

Performance and concurrency

Linear in byte count; the complete bytes and decoded string coexist. Shared-stream access requires external synchronisation.

Related entries

External references

Created 2026-07-15