Skip to main content

StreamToStringUTF16BE

function StreamToStringUTF16BE(M : TStream) : string

Example

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

Usage

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

Parameters

NameTypeDescription
MTStreamExisting readable, seekable caller-owned stream. The function resets its position.

Returns

The decoded Velox string, or an empty string for empty input.

Errors

Nil-object, seek, read and allocation errors propagate. Odd length and malformed surrogate structure are not rejected by this installed decoder.

Usage notes

Big-endian UTF-16 is less common on Windows. Confirm the wire contract explicitly; do not select it solely because a payload contains a BOM.

Additional Technical Info

StreamToStringUTF16BE interprets the complete stream as big-endian UTF-16 code units. It does not negotiate byte order or strip a preamble.

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 calls MemoryStreamToString(M, TEncoding.BigEndianUnicode). The installed TBigEndianUnicodeEncoding inherits the UTF-16 character-count calculation and combines each pair of bytes in big-endian order into one Delphi WideChar code unit.

Edge cases and quirks

  • A leading FE FF BOM becomes U+FEFF in the result; explicit decoding does not remove it.
  • An odd trailing byte is silently ignored because the inherited character count is ByteCount div 2.
  • Surrogate pairs are not validated. The decoder copies code units, including unpaired surrogates.
  • Little-endian input is byte-swapped into different characters rather than auto-detected.
  • The shared helper rewinds the stream, ignores a short-read count and supports only a signed-LongInt byte count safely.

Side effects

Changes M.Position; no content or ownership change.

Performance and concurrency

Linear time and memory. The input array and output string coexist; do not mutate a shared stream concurrently.

Related entries

External references

Created 2026-07-15