ThMemoryStream

Implements an in-memory stream that allows reading and writing data directly within RAM, using a dynamically allocated memory buffer.
Provides standard stream operations such as reading, writing, seeking, and resizing, without any file or network I/O.
Ideal for temporary storage, buffering, or manipulation of data blocks entirely in memory.

Public Procedures and Functions

Assign_And_Protect_Memory_Block

Assigns a block of memory from another ThMemoryStream and marks it as protected, preventing modification.
Allows sharing a segment of memory between streams with safety from unintended changes.
function Assign_And_Protect_Memory_Block(SourceMS: ThMemoryStream; FromPosition, BlockSize: NativeUInt): Boolean;

Parameters

  • SourceMS: ThMemoryStream; The source memory stream to assign the block from.
  • FromPosition: NativeUInt; The starting position of the block within the source stream.
  • BlockSize: NativeUInt; The size, in bytes, of the block to assign.

Return Value

Returns True if the block was assigned and protected successfully.
Returns False if the operation failed.

Reduce_Spare_Allocation

Reduces the amount of extra allocated memory in the stream to minimize unused capacity.
Helps optimize memory usage by trimming excess buffer allocation.
procedure Reduce_Spare_Allocation;

Read

Reads up to Count bytes from the memory buffer into the specified Buffer.
Advances the position in the stream by the number of bytes read.
function Read(var Buffer; Count: Cardinal): Cardinal; override;

Parameters

  • Buffer: var (untyped); Memory location to store the data read from the memory buffer.
  • Count: Cardinal; The maximum number of bytes to read from the stream.

Return Value

Returns the number of bytes actually read from the memory buffer.

Write

Writes up to Count bytes from the specified Buffer to the memory buffer.
Expands the buffer as needed and advances the stream position.
function Write(const Buffer; Count: Cardinal): Cardinal; override;

Parameters

  • Buffer: const (untyped); The data to be written into the memory buffer.
  • Count: Cardinal; The maximum number of bytes to write to the stream.

Return Value

Returns the number of bytes actually written to the memory buffer.

Copy_All_Content_Into_WideString

Copies the entire content of the memory stream into a Unicode string.
The resulting string contains all data in wide character format.
procedure Copy_All_Content_Into_WideString(var Result: String);

Parameters

  • Result: var String; Receives the copied wide character content from the memory stream.

Copy_WideText

Copies a block of wide characters (Unicode) from a specified position and count in the memory stream.
Returns the extracted substring as a Unicode string.
function Copy_WideText(FromPos: NativeInt; Count: Cardinal): String;

Parameters

  • FromPos: NativeInt; Starting position for the copy operation (can be negative for relative indexing).
  • Count: Cardinal; The number of wide characters to copy.

Return Value

Returns the substring containing the copied wide characters.

Replace_AnsiString

Searches and replaces occurrences of an ANSI string within the memory stream.
Supports specifying the search range and case sensitivity.
function Replace_AnsiString(FindString, Replaceto: RawByteString; FromPos: Int64 = 0; ToPos: Int64 = 0; CaseSensitive: Boolean = False): Boolean;

Parameters

  • FindString: RawByteString; The ANSI string to search for in the stream.
  • Replaceto: RawByteString; The ANSI string to replace each occurrence with.
  • FromPos: Int64; Starting position for the search (default is 0).
  • ToPos: Int64; Ending position for the search (default is 0, meaning until the end).
  • CaseSensitive: Boolean; If True, search is case sensitive (default is False).

Return Value

Returns True if at least one occurrence was replaced.
Returns False if nothing was replaced or the operation failed.

Load_From_File

Loads data from a file into the memory stream.
Supports reading locked files and copying from the end of the file if needed.
function Load_From_File(FileName: String; Access_Locked_File: Boolean = False; CopyFromEnd: Int64 = 0): Boolean;

Parameters

  • FileName: String; The name or path of the file to load data from.
  • Access_Locked_File: Boolean; If True, attempts to read from files that are currently locked (default is False).
  • CopyFromEnd: Int64; Number of bytes to load from the end of the file (default is 0, meaning the whole file).

Return Value

Returns True if the file was loaded successfully.
Returns False if the operation failed.

Save_To_File

Saves the content of the memory stream to a file.
Optionally resets the stream after saving.
function Save_To_File(FileName: String; Reset_After_Save: Boolean = False): Boolean;

Parameters

  • FileName: String; The name or path of the file to save data to.
  • Reset_After_Save: Boolean; If True, clears the memory stream after saving (default is False).

Return Value

Returns True if the file was saved successfully.
Returns False if the operation failed.

CopyFrom_MS

Copies up to Count bytes from another ThMemoryStream into the current memory stream.
The copied data is appended or written to the current position.
function CopyFrom_MS(Source: ThMemoryStream; Count: Int64): Int64;

Parameters

  • Source: ThMemoryStream; The source memory stream to copy data from.
  • Count: Int64; The number of bytes to copy.

Return Value

Returns the number of bytes actually copied.

Read_From_Socket

Reads data from a socket into the memory stream, with support for timeouts and an optional read size limitation.
This function allows integration with network operations and can respect process monitor modes if required.
function Read_From_Socket(var _Socket: NativeUInt; const TimeOut: Double; Read_Bytes_Limitation: Word = 0; const App_PM: Boolean = False): Boolean;

Parameters

  • _Socket: var NativeUInt; The socket handle to read data from.
  • TimeOut: const Double; Maximum time in seconds to wait for data.
  • Read_Bytes_Limitation: Word; Limits the number of bytes to read (0 means no limit).
  • App_PM: const Boolean; If True, operates in application process monitor mode.

Return Value

Returns True if data was read successfully.
Returns False if the operation failed or timed out.

Write_8KB_Packet_To_Socket

Writes up to 8 KB of buffered data from the memory stream to the specified socket.
Designed for efficient packetized network communication.
function Write_8KB_Packet_To_Socket(var _Socket: NativeUInt): Boolean;

Parameters

  • _Socket: var NativeUInt; The socket handle to write data to.

Return Value

Returns True if the packet was sent successfully.
Returns False if the operation failed.

Write_Everything_To_Socket

Writes the entire contents of the memory stream to the specified socket.
Optionally clears the buffer if all data was sent.
function Write_Everything_To_Socket(var _Socket: NativeUInt; Clear_Buffer_IF_Sent: Boolean = False): Boolean;

Parameters

  • _Socket: var NativeUInt; The socket handle to write data to.
  • Clear_Buffer_IF_Sent: Boolean; If True, clears the memory buffer after successful send.

Return Value

Returns True if all data was sent successfully.
Returns False if the operation failed.

Encode_SYE_Sapphire

Encrypts the stream content using the SYE Sapphire algorithm and the specified private key.
Updates the memory stream with the encoded data.
function Encode_SYE_Sapphire(const Private_Key: array of Byte): Boolean;

Parameters

  • Private_Key: array of Byte; The private key bytes used for encryption.

Return Value

Returns True if encoding was successful.
Returns False if the operation failed.

Decode_SYE_Sapphire

Decrypts the stream content previously encoded with the SYE Sapphire algorithm, using the provided private key.
Restores the original unencrypted data in the stream.
function Decode_SYE_Sapphire(const Private_Key: array of Byte): Boolean;

Parameters

  • Private_Key: array of Byte; The private key bytes used for decryption.

Return Value

Returns True if decoding was successful.
Returns False if the operation failed.

Encode_OLD_Sapphire

Encrypts the stream content using the legacy Sapphire encryption algorithm and a string key.
Available only on Win32 platforms.
function Encode_OLD_Sapphire(const Sapphire_Key: String): Boolean;

Parameters

  • Sapphire_Key: String; The key string used for legacy encryption.

Return Value

Returns True if encoding was successful.
Returns False if the operation failed.

Decode_OLD_Sapphire

Decrypts the stream content encoded with the legacy Sapphire algorithm, using the specified key.
Available only on Win32 platforms.
function Decode_OLD_Sapphire(const Sapphire_Key: String): Boolean;

Parameters

  • Sapphire_Key: String; The key string used for legacy decryption.

Return Value

Returns True if decoding was successful.
Returns False if the operation failed.

Compress

Compresses the content of the memory stream using a built-in compression algorithm.
Compression level can be specified and is limited to 4GB of data.
function Compress(Level: Byte = 4): Boolean;

Parameters

  • Level: Byte; Compression level (default is 4).

Return Value

Returns True if compression succeeded.
Returns False if the operation failed.

DeCompress

Decompresses the content of the memory stream that was previously compressed.
Decompression is limited to streams up to 4GB in size.
function DeCompress: Boolean;

Return Value

Returns True if decompression succeeded.
Returns False if the operation failed.

Properties:

Memory

Provides direct access to the memory buffer managed by the stream.
This property is read-only and returns a pointer to the internal buffer.
property Memory: Pointer read _Memory_Ptr;

Last_Allocation_Had_Errors

Indicates whether the last memory allocation attempt encountered errors.
This property is read-only and can be used to check for allocation failures.
property Last_Allocation_Had_Errors: Boolean read _Last_Allocation_Had_Errors;