Navigation: TextEd > Extension and Scripts > Classes (non-visual) >

TStream, TFileStream, TMemoryStream

 

 

 

 

TStream is the base class type for stream objects that can read from or write to various kinds of storage media, such as disk files, dynamic memory, and so on.

Use specialized stream objects to read from, write to, or copy information stored in a particular medium.


 

TFileStream enables applications to read from and write to a file on disk.


 

TMemoryStream is a stream that stores its data in dynamic memory.

Use TMemoryStream to store data in a dynamic memory buffer that is enhanced with file-like access capabilities. TMemoryStream provides the general I/O capabilities of a stream object while introducing methods and properties to manage a dynamic memory buffer.

Memory streams are useful as intermediary objects that can hold information as well as read it from or write it to another storage medium. They provide a useful format for comparing the contents of streams, or for manipulating data that is stored in a less accessible medium.


 

Mode = (fmCreate, fmOpenRead, fmOpenWrite, fmOpenReadWrite, fmShareExclusive, fmShareDenyWrite, fmShareDenyNone);

TEncodings = (encAnsi, encUTF8, encUnicode);

 


TStream, TFileStream, TMemoryStream


 


Properties


 

property int Position;

Indicates the current offset into the stream for reading and writing.

Use Position to obtain the current position of the stream. This is the number of bytes from the beginning of the streamed data.


 

property int Size;

Indicates the size in bytes of the stream.


 

(TFileStream)

property string Filename; (Read-only)

Access the filename used to read data from.

 


Methods


 

(TFileStream)

function Create(String Filename, Word Mode);

Create a file stream object where the contents is the given file.


 

Mode = (fmCreate, fmOpenRead, fmOpenWrite, fmOpenReadWrite, fmShareExclusive, fmShareDenyWrite, fmShareDenyNone);


 

Ex.

Word mode = fmCreate or fmOpenReadWrite;

TFileStream stream = TFileStream.Create("C:\\Test.txt", mode);

try

{

   String s = "Hello World!";

   stream.WriteString(s, encAnsi);

}

finally

{

   stream.Free;

};


 

function Create;

Create the stream object.


 

(TMemoryStream)

function Clear;

Use Clear to empty the memory buffer for the memory stream and free all associated memory.


 

function int CopyFrom(TStream Source, int Count);

Copies a specified number of bytes from one stream to another.


 

function Free;

Delete the stream object.


 

(TMemoryStream)

procedure LoadFromFile(string FileName);

Loads the entire contents of a file into the memory buffer.


 

(TMemoryStream)

procedure LoadFromStream(TStream Stream);

Loads the entire contents of a stream into the memory buffer.


 

function int Read(@Buffer, int Count);

Read a specified amount of bytes from the stream into a buffer, e.g. an array of bytes.


 

function String ReadString(int Count, TEncodings encoding);

Read a specified amount of bytes into a string. Use the encoding argument to specify the stream data type.

Note that "Count" is given in bytes.


 

Ex.

 

// Read an ANSI file to a string.

WORD mode = fmOpenReadWrite;

TFileStream stream = TFileStream.Create("C:\\Test.txt", mode);

try

{

   int size = stream.Size;

   String s = stream.ReadString(size, encAnsi);

   int len = Length(s); // len = size;

   ...

}

finally

{

   stream.Free;

}; 

 

// Read a Unicode (UTF16) file to a string.

WORD mode = fmOpenReadWrite;

TFileStream stream = TFileStream.Create("C:\\Test.txt", mode);

try

{

   int size = stream.Size;

   String s = stream.ReadString(size, encUnicode);

   int len = Length(s); // len = size / 2;

   ...

}

finally

{

   stream.Free;

};

 

// Read an UTF8 file to a string.

WORD mode = fmOpenReadWrite;

TFileStream stream = TFileStream.Create("C:\\Test.txt", mode);

try

{

   int size = stream.Size;

   String s = stream.ReadString(size, encUTF8);

   int len = Length(s); // len <= size;

   ...

}

finally

{

   stream.Free;

};

 


 

(TMemoryStream)

procedure SaveToFile(string FileName);

Writes the entire contents of the memory stream to the file with a given file name.


 

(TMemoryStream)

procedure SaveToStream(TStream Stream);

Writes the entire contents of the memory stream to the stream object specified by Stream.


 

function int Write(Buffer, int Count);

Write a specified amount of bytes from a buffer to the stream. The buffer can be an array of bytes.


 

function int WriteString(String Str, TEncodings encoding);

Write a given string into the stream. Specify if the stream should contain ANSI, UTF8 or Unicode data.


 

Ex.

 

// Write a string to an UTF8 file.

WORD mode = fmCreate or fmOpenReadWrite;

TFileStream stream = TFileStream.Create("C:\\Test.txt", mode);

try

{

   String s = "Hello World!";

   stream.WriteString(s, encUTF8);

}

finally

{

   stream.Free;

};


 


 


 

 

 

 

 

Copyright © 2024 Rickard Johansson