20th Chapter-Development Delphi Object Type Data management function (i) (3)

Source: Internet
Author: User
Tags definition constructor count integer

20.1.2 Thandlestream Object

The behavior of a Thandlestream object is particularly like a FileStream object, which differs from storing data in the stream through a file handle that has been created rather than a filename.

The Thandlestream object defines the handle property, which provides read-only access to file handles, and the handle property can be used as a parameter to Delphi's RTL file management function, using file-class functions to read and write data. Thandlestream overrides the constructor create, which has a handle parameter that specifies the file handle associated with the Thandlestream object.

Methods for 20.1.2.1 Thandlestream properties:

1. Handle Properties

Statement: Property handle:integer;

The Handle property provides read-only access to a file handle that is passed by the Thandlestream construction method. Therefore, in addition to the method provided by Thandlestream, the handle can be manipulated with a file management function. In fact, the method of Thandlestream is to use the file management function to do the actual reading and writing operation.

2. Create method

Statement: Constructor Create (Ahandle:integer);

The Create method uses the passed-in handle parameter to create a Thandlestream object that is associated with a particular file handle and assigns the Ahandle to the handle property of the stream.

3. Read, write and Seek methods

These three methods are tstream virtual methods, but they are overridden in thandlestream to achieve a specific medium-file data access. The implementation of these three methods is described in detail later.

Realization principle of 20.1.2.2 Thandlestream

Thandlestream are inherited from Tstream, so you can share the properties and most of the methods in Tstream. Thandlestream mainly adds a property handle and covers the Create, Read, write, and seek four methods in implementation.

1. Implementation of attributes

The implementation of the handle property, as with the implementation of most of Delphi's properties, declares a variable fhandle that holds the data in the private part of the object definition, and then declares the property handle in the public part of the definition, where the read and write control part of the attribute definition plus read-only control, Read control only reads directly the value of the Fhandle variable, which is implemented as follows:

Thandlestream = Class (Tstream)

Private

Fhandle:integer;

Public

...

Property Handle:integer read Fhandle;

End

2. The realization of the method

The Thandlestream create method, taking Ahandle as the parameter, simply assigns the Ahandle value to Fhandle in the method, which is implemented as follows:

Constructor Thandlestream.create (Ahandle:integer);

Begin

Fhandle: = Ahandle;

End

To implement data object storage for files, the Thandlestream read, write, and Seek methods cover the corresponding methods in Tstream. Their implementations call the file management functions of Windows.

The Read method calls the Fileread function to implement a file read operation, which is implemented as follows:

function Thandlestream.read (var Buffer; Count:longint): Longint;

Begin

Result: = Fileread (Fhandle, Buffer, Count);

If result =-1 then result: = 0;

End

The Write method calls the FileWrite function to implement a file write operation, which is implemented as follows:

function thandlestream.write (const Buffer; Count:longint): Longint;

Begin

Result: = FileWrite (Fhandle, Buffer, Count);

If result =-1 then result: = 0;

End

The Seek method calls the FileSeek function to implement the movement of the file pointer, which is implemented as follows:

function Thandlestream.seek (offset:longint; Origin:word): Longint;

Begin

Result: = FileSeek (Fhandle, Offset, Origin);

End

20.1.3 TFileStream Object

The TFileStream object is a stream object that stores data on a disk file. TFileStream is inherited from Thandlestream, it is the same as Thandlestream to implement file access operations. The difference is that Thandlestream accesses the file with a handle, and TFileStream accesses the file with the file name. In fact TFileStream is a layer of packaging on Thandlestream, whose kernel is Thandlestream properties and methods.

New properties and methods are not added to the TFileStream. It simply covers the construction method of Create and destructor destory. With two parameters filename and mode in the Create method. FileName describes the name of the file to create or open, and mode describes the file patterns such as Fmcreate, Fmopenread, and Fmopenwrite. The Create method first uses the Filecreate or FileOpen function to create or open a file named filename, and then assigns the resulting file handle to the Fhandle. TFileStream file read and write operations are inherited from Thandlestream read

Var

Stream:tstream;

Begin

Stream: = Tfilestream.create (FileName, fmcreate);

Try

Savetostream (Stream);

Finally

Stream.free;

End

End

The implementations of the Savetostream and SaveToFile, Loadfromstream, and LoadFromFile methods of many objects in Delphi have similar nesting structures.

20.1.5 Tmemorystream Object

The Tmemorystream object is a stream object that manages data in dynamic memory, which is inherited from Tcustommemorystream, in addition to the properties and methods inherited from Tcustommemorystream, It also adds and overwrites some of the methods used to read data from disk files and other note-consoles. It also provides a dynamic memory management method for writing and eliminating memory content. Here are some of its properties and methods.

Properties and methods of 20.1.5.1 Tmemorystream

1. Capacity Properties

Statement: Property copacity:longint;

The capacity property determines the size of the memory pool that is allocated to the memory stream. This is somewhat different from the Size property. The Size property is a description of the amount of data in the stream. You can set the value of a capacity in a program larger than the maximum memory required for the data, which avoids frequent reallocation.

2. ReAlloc method

Statement: function ReAlloc (var newcapacity:longint): pointer; Virtual

The ReAlloc method allocates dynamic memory in 8K, the size of the memory is specified by newcapacity, and the function returns a pointer to the allocated memory.

3. SetSize method

The SetSize method eliminates the data that is contained in the memory stream and sets the size of the memory pool in the memory stream to a size byte. If the size is zero, the SetSize method frees the existing pool of memory and the memory property to nil; otherwise, the SetSize method resizes the memory pool to size.

4. Clear method

Statement: Procedure Clear;

The clear method frees up memory pools in memory and resets the memory property to nil. The size and position properties are 0 after the clear method is called.

5. Loadfromstream method

Statement: Procedure Loadfromstream (Stream:tstream);

The Loadfromstream method copies the entire contents of the stream specified by the stream into the MemoryStream, replacing the existing content so that MemoryStream becomes a copy of the stream.

6. LoadFromFile method

Statement: Procedure LoadFromFile (count filename:string);

The LoadFromFile method copies all the contents of the filename-specified file into the MemoryStream and replaces the existing content. When the LoadFromFile method is invoked, MemoryStream becomes a full copy of the contents of the file in memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.