ThStream
Represents an abstract base class for stream operations, providing a unified interface for reading from and writing to sequential data sources.Defines basic stream manipulation methods, enabling extensions for custom stream types such as memory, file, or network streams.
This class is designed to be inherited and extended by specialized stream classes.
Public Procedures and Functions:
Clear
Resets the stream by removing all its current content and preparing it for new data.Any existing data is discarded.
procedure Clear;
Read
Reads up to Count bytes from the stream into the specified Buffer.The actual number of bytes read may be less than Count if the stream does not contain enough data.
function Read(var Buffer; Count: Cardinal): Cardinal; virtual;
Parameters
- Buffer: var (untyped); Memory location to store the data read from the stream.
- Count: Cardinal; The maximum number of bytes to read from the stream.
Return Value
Returns the number of bytes actually read from the stream.Write
Writes up to Count bytes from the specified Buffer to the stream.The function returns the actual number of bytes successfully written.
function Write(const Buffer; Count: Cardinal): Cardinal; virtual;
Parameters
- Buffer: const (untyped); The data to be written into the stream.
- Count: Cardinal; The maximum number of bytes to write to the stream.
Return Value
Returns the number of bytes actually written to the stream.Read_Byte
Reads a single byte from the stream and advances the current position by one byte.function Read_Byte: Byte;
Return Value
Returns the byte value read from the stream.Write_Byte
Writes a single byte value to the stream and returns whether the operation was successful.function Write_Byte(const Value: Byte): Boolean;
Parameters
- Value: Byte; The byte value to write into the stream.
Return Value
Returns True if the byte was written successfully.Returns False if the operation failed.
Read_Word
Reads a 16-bit word from the stream and advances the current position by two bytes.function Read_Word: Word;
Return Value
Returns the word value read from the stream.Write_Word
Writes a 16-bit word value to the stream and returns whether the operation was successful.function Write_Word(const Value: Word): Boolean;
Parameters
- Value: Word; The word value to write into the stream.
Return Value
Returns True if the word was written successfully.Returns False if the operation failed.
Read_Double
Reads a double-precision floating-point value from the stream and advances the current position by the size of a double.function Read_Double: Double;
Return Value
Returns the double value read from the stream.Write_Double
Writes a double-precision floating-point value to the stream and returns whether the operation was successful.function Write_Double(Value: Double): Boolean;
Parameters
- Value: Double; The double value to write into the stream.
Return Value
Returns True if the value was written successfully.Returns False if the operation failed.
Read_Extended
Reads an extended-precision floating-point value from the stream.On Win32 systems, this uses the Extended type.
On other systems, it uses Double for compatibility.
{$IFDEF WIN32}
function Read_Extended: Extended;
{$ELSE}
function Read_Extended: Double;
{$ENDIF}
Return Value
Returns the extended or double value read from the stream, depending on platform.Write_Extended
Writes an extended-precision floating-point value to the stream and returns whether the operation was successful.On Win32 systems, this uses the Extended type.
On other systems, it uses Double for compatibility.
{$IFDEF WIN32}
function Write_Extended(Value: Extended): Boolean;
{$ELSE}
function Write_Extended(Value: Double): Boolean;
{$ENDIF}
Parameters
- Value: Extended/Double; The value to write into the stream, type depends on platform.
Return Value
Returns True if the value was written successfully.Returns False if the operation failed.
Init_UCS2_Stream
Initializes the stream for handling UCS2 (16-bit Unicode) encoded data.This prepares the stream to read or write UCS2 text sequences.
procedure Init_UCS2_Stream;
Check_Stream_Is_UCS2
Checks whether the stream content is encoded in UCS2 format.Returns a Boolean indicating the encoding type.
function Check_Stream_Is_UCS2: Boolean;
Return Value
Returns True if the stream is identified as UCS2 encoded.Returns False otherwise.
Init_UTF8_Stream
Initializes the stream for handling UTF-8 encoded data.This prepares the stream to read or write UTF-8 text sequences.
procedure Init_UTF8_Stream;
Check_Stream_Is_UTF8
Checks whether the stream content is encoded in UTF-8 format.Returns a Boolean indicating the encoding type.
function Check_Stream_Is_UTF8: Boolean;
Return Value
Returns True if the stream is identified as UTF-8 encoded.Returns False otherwise.
Check_Stream_Is_Windows_Portable_Executable
Checks whether the stream content matches the structure of a Windows Portable Executable (PE) file.Returns a Boolean indicating if the stream is a PE file.
function Check_Stream_Is_Windows_Portable_Executable: Boolean;
Return Value
Returns True if the stream matches the Windows PE format.Returns False otherwise.
Check_Stream_Is_Zlib_Compressed
Checks whether the stream content is compressed using the Zlib compression algorithm.Returns a Boolean indicating the compression type.
function Check_Stream_Is_Zlib_Compressed: Boolean;
Return Value
Returns True if the stream is Zlib compressed.Returns False otherwise.
Read_WideText
Reads a sequence of wide characters (Unicode) from the stream, up to the specified length, and returns it as a string.The read operation interprets the data as UTF-16 or UCS2 encoded text, depending on stream initialization.
function Read_WideText(ReadLength: Cardinal): String;
Parameters
- ReadLength: Cardinal; The number of wide characters to read from the stream.
Return Value
Returns the wide character text read from the stream as a string.Write_WideText
Writes a string of wide characters (Unicode) to the stream.Optional parameters allow setting the number of characters written and a fill character for padding.
function Write_WideText(const Value: String; WriteLength: Cardinal = 0; Fill_Char: Word = 0): Boolean;
Parameters
- Value: String; The wide character string to write into the stream.
- WriteLength: Cardinal; The number of characters to write (default is length of Value).
- Fill_Char: Word; Unicode value used for padding if WriteLength is greater than the length of Value (default is 0).
Return Value
Returns True if the text was written successfully.Returns False if the operation failed.
Read_AnsiText
Reads a sequence of ANSI characters from the stream, up to the specified length, and returns it as a RawByteString.The operation interprets the data using the current code page or stream encoding.
function Read_AnsiText(ReadLength: Cardinal): RawByteString;
Parameters
- ReadLength: Cardinal; The number of bytes to read from the stream as ANSI text.
Return Value
Returns the ANSI text read from the stream as a RawByteString.Write_AnsiText
Writes a RawByteString (ANSI text) to the stream.Optional parameters allow setting the number of bytes written and a fill character for padding.
function Write_AnsiText(const Value: RawByteString; WriteLength: Cardinal = 0; Fill_Char: Byte = 0): Boolean;
Parameters
- Value: RawByteString; The ANSI text to write into the stream.
- WriteLength: Cardinal; The number of bytes to write (default is length of Value).
- Fill_Char: Byte; Byte value used for padding if WriteLength is greater than the length of Value (default is 0).
Return Value
Returns True if the text was written successfully.Returns False if the operation failed.
Write_AnsiLine
Writes an ANSI text line to the stream, appending a line break after the text.This method is typically used to output text in a line-oriented format.
function Write_AnsiLine(const Text: RawByteString): Boolean;
Parameters
- Text: RawByteString; The ANSI text line to write into the stream.
Return Value
Returns True if the line was written successfully.Returns False if the operation failed.
CopyFrom (ThStream overload)
Copies up to Count bytes from another ThStream into the current stream.Supports an optional slower copy mode for compatibility or data integrity scenarios.
function CopyFrom(Source: ThStream; Count: Int64; SlowerCopy: Boolean = False): Int64; overload;
Parameters
- Source: ThStream; The source stream from which data will be copied.
- Count: Int64; The number of bytes to copy from the source stream.
- SlowerCopy: Boolean; If True, uses a slower, safer copy algorithm (default is False).
Return Value
Returns the number of bytes actually copied.CopyFrom (TStream overload)
Copies up to Count bytes from a standard Delphi TStream into the current stream.function CopyFrom(Source: TStream; Count: Int64): Int64; overload;
Parameters
- Source: TStream; The source stream from which data will be copied.
- Count: Int64; The number of bytes to copy from the source stream.
Return Value
Returns the number of bytes actually copied.Pos_AnsiStr
Searches for an ANSI string within the stream, between specified positions, and returns the position of the first occurrence.Supports optional case sensitivity.
function Pos_AnsiStr(FindString: RawByteString; FromPos: Int64; ToPos: Int64 = 0; CaseSensitive: Boolean = False): Int64;
Parameters
- FindString: RawByteString; The ANSI string to search for in the stream.
- FromPos: Int64; The starting position in the stream for the search.
- ToPos: Int64; The ending position for the search (default is 0, meaning until the end).
- CaseSensitive: Boolean; If True, the search is case sensitive (default is False).
Return Value
Returns the position of the first occurrence of FindString.Returns -1 if the string is not found.
Pos_WideStr
Searches for a wide (Unicode) string within the stream, between specified positions, and returns the position of the first occurrence.Supports optional case sensitivity.
function Pos_WideStr(FindString: String; FromPos: Int64; ToPos: Int64 = 0; CaseSensitive: Boolean = False): Int64;
Parameters
- FindString: String; The wide (Unicode) string to search for in the stream.
- FromPos: Int64; The starting position in the stream for the search.
- ToPos: Int64; The ending position for the search (default is 0, meaning until the end).
- CaseSensitive: Boolean; If True, the search is case sensitive (default is False).
Return Value
Returns the position of the first occurrence of FindString.Returns -1 if the string is not found.
Calc_CRC_Word
Calculates a CRC word checksum for a specified range in the stream.Used to verify data integrity between two positions, or the whole stream if parameters are omitted.
function Calc_CRC_Word(ExFromPos: Int64 = -1; ExUntilPos: Int64 = -1): Word;
Parameters
- ExFromPos: Int64; The starting position for CRC calculation (default is -1, meaning from the beginning).
- ExUntilPos: Int64; The ending position for CRC calculation (default is -1, meaning to the end).
Return Value
Returns the computed CRC word value.Delete
Removes a sequence of bytes from the stream, starting at a given position and for a specified length.The stream is adjusted to close the gap left by the deleted section.
function Delete(FromPos, Count: Int64): Boolean;
Parameters
- FromPos: Int64; The starting position of the deletion.
- Count: Int64; The number of bytes to delete from the stream.
Return Value
Returns True if the operation succeeded.Returns False if the operation failed.
Adapter
Returns a Delphi stream adapter object that provides integration with standard Delphi stream interfaces.This allows using the current stream in contexts where a Delphi TStream descendant is required.
function Adapter: ThDelphiStreamAdapter;
Return Value
Returns a ThDelphiStreamAdapter instance linked to the current stream.Properties:
Position
Specifies the current read/write position within the stream as a byte offset from the beginning.Can be read or set to move the stream pointer.
property Position: Int64;
Size
Specifies the total size of the stream, in bytes.Can be read to determine the length of the stream or set to resize it.
property Size: Int64;