Realization principle of 20.1.1.2 Tstream
The Tstream object is the underlying class for the stream object, which is the basis of the stream object. In order to store data objects on different media, subsequent stream objects are mainly improved on the read and write methods. Therefore, understanding Tstream is the core of mastering stream object management. Borland Company provided the interface of the Stream object documentation, but for its implementation and application methods are not mentioned, the author is from the Borland Delphi 2.0 client/server Suite source code and part of the example program to master the flow of object technology.
The following is the beginning of the implementation of the Tstream properties and methods.
1. Implementation of Tstream Property
As described earlier, Tstream has the position and size two attributes, which, as abstract data types, abstract the domains that are frequently accessed to read and write data in a variety of storage media. So how do they do that?
Read-write control in the definition of a part attribute is described in the chapter on custom part writing. Position and size are also read and write controlled. The definition is as follows:
Property Position:longint read GetPosition write setposition;
Property Size:longint read GetSize;
As you can know, position is a read-write property, and size is readonly.
The implementation of the position attribute is embodied in getposition and SetPosition. When the program is running, any read position value and assignment to position automatically triggers the private method getposition and SetPosition. The declarations of two methods are as follows:
function TStream.GetPosition:Longint;
Begin
Result: = Seek (0, 1);
End
Procedure Tstream.setposition (Pos:longint);
Begin
Seek (Pos, 0);
End
When setting the location, the Delphi compilation mechanism will automatically pass the position to POS.
The first parameter is the move offset, the second parameter is the starting point of the move, and the return value is the position of the pointer after the move.
The implementation of the Size property is read-controlled, completely masking the write operation. Read Control method GetSize implemented as follows:
function TStream.GetSize:Longint;
Var
Pos:longint;
Begin
Pos: = Seek (0, 1);
Result: = Seek (0, 2);
Seek (Pos, 0);
End
2. Implementation of Tstream method
⑴copyfrom method
CopyFrom is a useful method in the Stream object, which is used to copy data in different storage mediums. For example, between memory and external files, between memory and database fields, and so on. It simplifies the details of many memory allocations, file opening and reading and writing, and unifies all copy operations onto stream objects.
Previously described: The CopyFrom method takes source and count two parameters and returns a long integer type. This method copies the contents of count bytes from source into the current stream, and copies all data if the count value is 0.
function Tstream.copyfrom (source:tstream; Count:longint): Longint;
Const
Maxbufsize = $F 000;
Var
BufSize, N:integer;
Buffer:pchar;
Begin
If Count = 0 Then
Begin
Source.position: = 0;
coung= "ZH-CN" > a part in a resource file is invoked when the programmer does not need to call it himself. If the read is not a resource file Readresheader, an exception event is triggered.
Procedure Tstream.readresheader;
Var
Readcount:longint;
HEADER:ARRAY[0..79] of Char;
Begin
Fillchar (header, SizeOf (header), 0);
Readcount: = Read (header, SizeOf (header)-1);
if (Byte (@Header [0]) ^) = $FF) and (Word (@Header [1]) ^) Then
Seek (StrLen (Header + 3) + 10-readcount, 1)
Else
Raise Einvalidimage.createres (Sinvalidimage);
End
Readcomponentres reads a part in a Windows resource file, in order to determine whether it is a resource file, it first invokes the Readresheader method, and then calls the Readcomponent method to read the part specified by instance. Here's how it's implemented:
function Tstream.readcomponentres (instance:tcomponent): tcomponent;
Begin
Readresheader;
Result: = Readcomponent (Instance);
End
The corresponding write method with Readcomponentres is to Writecomponentres,delphi call these two methods to read and write the form file (DFM file), in the following book will be used in these two methods to read the DFM file example.
⑷writecomponent and Writedescendant methods
The Writedescendant method of the Stream object creates the Twriter object during the implementation process, and then writes the instance to the stream using the Twriter Writedescendant method. The Writecomponent method simply calls the Writedescendant method to write the instance to the stream. They are implemented as follows:
Procedure Tstream.writecomponent (instance:tcomponent);
Begin
Writedescendent (Instance, nil);
End
Procedure Tstream.writedescendent (Instance, ancestor:tcomponent);
Var
Writer:twriter;
Begin
Writer: = Twriter.create (Self, 4096);
Try
Writer.writedescendent (Instance, Ancestor);
Finally
Writer.free;
End
End
⑸writedescendantres and Writecomponentres methods
The Writedescendantres method is used to write a part to a Windows resource file, and the Writecomponentres method simply invokes the Writedescendantres method, which is implemented as follows:
Procedure Tstream.writecomponentres (const resname:string; Instance:
Tcomponent);
Begin
Writedescendentres (Resname, Instance, nil);
End
Procedure Tstream.writedescendentres (const resname:string; Instance,
Ancestor:tcomponent);
Var
Headersize:integer;
Origin, Imagesize:longint;
HEADER:ARRAY[0..79] of Char;
Begin
Byte ((@Header [0]) ^): = $FF;
Word (@Header [1]) ^): = 10;
Headersize: = StrLen (Strupper (Strplcopy (@Header [3], Resname, 63)) + 10;
Word ((@Header [HeaderSize-6]) ^): = $1030;
Longint ((@Header [HeaderSize-4]) ^): = 0;
WriteBuffer (Header, headersize);
Origin: = Position;
Writedescendent (Instance, Ancestor);
ImageSize: = Position-origin;
Position: = Origin-4;
WriteBuffer (ImageSize, SizeOf (Longint));
Position: = Origin + imagesize;
End
Writecompnentres is the corresponding object writing method with Readcomponentres, these two methods can read Delphi's DFM file, and use Delphi system function.