Turn: The concept of Delphi flow

Source: Internet
Author: User

Delphi operation of the stream

First, the concept of flow

Flow is simply a kind of abstract data processing tool based on object-oriented, it defines some basic operations of data processing, such as reading data, writing data, etc., programmer only need to master the convection to operate, and not care about the flow of the other side of the real flow of data. In fact, the flow is to transform the entire object into a single byte of data, and then form a data stream, which is shaped like a solid rock into a small sand, and finally can form quicksand.

Second, the main function of the flow

The main function of a stream is to convert files and non-file data to and from each other (that is, I/O operations between them). If the picture file is saved to the database, then the picture data in the database becomes non-file data, it belongs to a record of a certain field of data. The data that is transmitted on the network, the stored data in memory, etc. are also non-file data, which need to be manipulated to transform into files.

Third, Delphi the basic concept and function declaration of midstream

Properties of the ㈠ stream

In Delphi, the base class for all stream objects is the Tstream class, which defines the common properties and methods for all flows. The following are the properties defined in the Tstream class:

1.Size: This property returns the size of the data in the stream in bytes.

2.Position: This property controls the position of the access pointer in the stream.

Tstream the virtual method defined:

1. Read: This method implements the reading of data from a stream.

The function prototype is:

Function Read (var Buffer; Count:longint): longint;virtual;abstract;

The buffer that the parameter buffer is placed on when the data is read out, Count is the number of bytes of data that needs to be read, and the method returns the number of bytes actually read, which can be less than or equal to the value specified in count.

2.Write: This method is implemented to write data to the stream.

The function prototype is:

Function Write (var Buffer; Count:longint): longint;virtual;abstract;

The buffer for the data in the stream to be written to, count is the length of the data bytes, and the method returns the number of bytes in the actual write flow.

3. Seek: This method implements the movement of the read pointer in the stream.

The function prototype is:

Function Seek (Offset:longint;origint:word): longint;virtual;abstract;

The parameter offset is the offset byte number, and the parameter origint indicates the actual meaning of offset, and its possible values are as follows:

Sofrombeginning:offset is the position at which the pointer moves after the data begins. Offset must be greater than or equal to zero at this time.

Sofromcurrent:offset is the relative position of the pointer to the current pointer after the move.

Sofromend:offset is the position at which the pointer moves after the end of the data. Offset must be less than or equal to zero at this time. The return value of the method is the position of the pointer after the move.

4, Setsize: This method realizes the change data size. The function prototype is:

Function Setsize (newsize:longint); virtual;

Tstream static methods for class definitions:

1. Readbuffer: The function of this method is to read data from the current position in the stream.

The function prototype is:

Procedure Readbuffer (var Buffer; Count:longint);

The parameters are defined in the same way as read above. Note: A Ereaderror exception is generated when the number of bytes read is not the same as the number of bytes that need to be read.

2. WriteBuffer: The function of this method is to write data to the stream at the current location.

The function prototype is:

Procedure WriteBuffer (var Buffer; Count:longint);

The parameter definition is the same as the above write. Note: A Ewriteerror exception is generated when the number of bytes written is not the same as the number of bytes that need to be written.

3. CopyFrom: The function of this method is to copy the data stream from the other stream.

The function prototype is:

Function CopyFrom (Source:tstream; Count:longint): Longint;

The parameter source is the stream that provides the data, and count is the number of data bytes copied. When count is greater than 0 o'clock, CopyFrom copies the count bytes of data from the current position of the source parameter, and when Count equals 0 o'clock, CopyFrom sets the position property of the source parameter to 0, and then copies all data from the source;

Tstream Derived Classes

1. TFileStream class (File stream).

To access a file using the TFileStream class, first create an instance. The statement reads as follows:

Constructor Create (const filename:string; Mode:word);

FileName is the file name (including the path), the parameter mode is the way to open the file, it includes the open mode and sharing mode of the file, its possible values and meanings are as follows:

Open mode:

Fmcreate: Creates a file with the specified file name and opens it if the file already exists.

Fmopenread: Open the specified file as read-only

Fmopenwrite: Open the specified file as a write-only method

Fmopenreadwrite: Open the specified file as a write

Sharing mode:

Fmsharecompat: Shared mode compatible with FCBs

Fmshareexclusive: Do not allow other programs to open the file in any way

Fmsharedenywrite: Does not allow other programs to open the file in a write manner

Fmsharedenyread: Do not allow other programs to open the file in read mode

Fmsharedenynone: Other programs can open the file in any way

2. Tmemorystream class (Memory stream)

The actual application of memory flow is also very much used, that is, to build a stream object in memory, its basic methods and functions are the same as above.

Source:=tmemorystream.create;

3.TresourceStream (Resource flow)

The resource flow is primarily applied to the operation of the resource file.

Some related functions and methods of ㈤ and flow operation

1.sizeof (variable: Integer): Gets the size of the space occupied by the variable.

2. Stream class variables. SaveToFile (target file name): Changes the flow to a file save.

3. Other class variables. Savetostream (Stream class variable): Transforms the contents of a class variable into a stream.

4. Other class variables. LoadFromFile (target file name): Loads the file into other class variables.

5. Other class variables. Loadfromstream (Stream class variable): Loads a stream class variable into another class variable.

6. Stream class variables. Free: Releases the stream.

Four, Delphi common methods of operation for streams

㈠ converting a file to a file stream

var Source:tfilestream;

Begin

Source:=tfilestream.create (source file, fmopenread or fmshareexclusive);

End;

The meaning of the above code is to open the source file as read-only or prohibit any way to open the file, and create a file stream form, and finally assign it to the file rheology source.

㈡ converting files to memory stream

var Source:tmemorystream;

Begin

Source:=tmemorystream.create;

Source.loadfromfile (source file);

End;

The meaning of the above code is to create a memory rheology source and load the source file into the rheological source.

㈢ saving a memory stream as a file

var Target:tmemorystream;

Begin

Target.savetofile (' target file ');

End

Note that once a file stream is created, it is saved to disk as a file, so there is no action to convert it to a file as if it were a memory stream.

㈣ converting other class variables to flow variables

var Bms:tmemorystream;

Bitmap1:tbitmap;

Begin

Bitmap1.savetostream (BmS);

End

㈤ merging two streams

Var

Target, Source:tfilestream;

Begin

Source:=tfilestream.create (auxiliary document, Fmopenread or fmshareexclusive);

Target:=tfilestream.create (master file, fmopenwrite or fmshareexclusive);

Target.seek (0, Sofromend); // move the pointer to the end of the main file

Target.copyfrom (Source, 0); // Note: Two streams are combined using a CopyFrom method.

Target.free;

Source.free;

End

The above program is actually a combination of two files, the auxiliary file is added to the main file behind, and finally become a file. It is important to note that two stream merges need to move the pointer to the end of the main file before adding the secondary file. This method can be used for file encryption, Trojan bundle, virus infection and so on.

㈥ separation of two streams

Var

Source:tfilestream;

Target:tmemorystream;

Myfilesize:integer;

Begin

myfilesize:= auxiliary file length;

Target: =tmemorystream.create;

Source:=tfilestream.create (master file, Fmopenread or

Fmsharedenynone);

Source.seek (-myfilesize, sofromend);  // Navigate to Resource location

// Note: here -sizeof (myfilesize) The original expression is 0-sizeof (myfilesize) . Meaning move the pointer to myfilesize bytes from the end, ready to read the data from there. Parameter sofromend is the meaning of "away from the end", if the parameter is sofrombeginning, it is "away from the front" meaning.

Target.copyfrom (Source, myfilesize); // Remove Resources

Target.savetofile (targetfile); Store to File

Target.free;

Source.free;

End

The function implemented by the above program is actually to separate two merged files from each other. This method can also be used for file segmentation, file encryption, and so on. Notice how the pointer moves as you learn.

Turn: The concept of Delphi flow

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.