What is stream?

Source: Internet
Author: User
What is stream? Stream, in short, is an abstract data processing tool built on an object-oriented basis. The stream defines some basic operations for data processing, such as reading data and writing data, Program The operator performs all operations on the stream without worrying about the real flow of the data at the other end of the stream. A stream can not only process files, but also process dynamic memory, network data, and other data forms. If you are proficient in flow operations and use the convenience of flow in the program, writing the program will greatly improve the efficiency.
Basic concepts and function declaration in Delphi
In Delphi, the base class of all stream objects is the tstream class, which defines the common attributes and methods of all streams.
The attributes defined in the tstream class are described as follows:
1. Size: This attribute returns the data size in the stream in bytes.
2. position: the position of the access pointer in the control flow.
There are four virtual methods defined in tstream:
1. Read: This method reads data from the stream. Function prototype:
Function read (VAR buffer; count: longint): longint; virtual; abstract;
The buffer parameter is the buffer placed during data reading. Count is the number of bytes of data to be read. The return value of this method is the number of bytes actually read, it can be smaller than or equal to the value specified in count.
2. Write: This method writes data to the stream. Function prototype:
Function write (VAR buffer; count: longint): longint; virtual; abstract;
The buffer parameter is the buffer of the data to be written into the stream, and the count parameter is the number of bytes in length. The return value of this method is the number of bytes actually written into the stream.
3. Seek: This method moves the read pointer in the stream. Function prototype:
Function seek (offset: longint; origint: Word): longint; virtual; abstract;
The offset parameter is the number of offset bytes. The origint parameter indicates the actual meaning of the Offset. The possible values are as follows:
Sofrombeginning: Offset indicates the position from which the pointer starts to move data. The offset value must be greater than or equal to zero.
Sofromcurrent: Offset indicates the relative position of the pointer to the current pointer after moving.
Sofromend: offset is the position at which the pointer is moved from the end of the data. The offset value must be less than or equal to zero. The return value of this method is the position of the pointer after moving.
4. setsize: This method changes the data size. Function prototype:
Function setsize (newsize: longint); Virtual;
In addition, the tstream class also defines several static methods:
1. readbuffer: This method is used to read data from the current position of the stream. Function prototype:
Procedure readbuffer (VAR buffer; count: longint );
The Parameter definition is the same as the preceding read. Note: When the number of data bytes to be read is different from the number of bytes to be read, an ereaderror error occurs.
2. writebuffer: This method is used to write data to the stream at the current position. Function prototype:
Procedure writebuffer (VAR buffer; count: longint );
The Parameter definition is the same as the preceding Write statement. Note: When the number of written data bytes is different from the number of bytes to be written, an ewriteerror error occurs.
3. copyfrom: This method is used to copy data streams from other streams. Function prototype:
Function copyfrom (Source: tstream; count: longint): longint;
The source parameter is the stream that provides data, and the count parameter is the number of copied data bytes. When count is greater than 0, copyfrom copies the data of Count bytes from the current position of the Source parameter; when count is equal to 0, copyfrom sets the position attribute of the Source parameter to 0, then copy all data from the source;
Tstream also has other derived classes, the most common of which is the tfilestream class. To use the tfilestream class to access files, you must first create an instance. The statement is as follows:
Constructor create (const filename: string; Mode: Word );
Filename is the file name (including the path), and the mode parameter is the file opening mode. It includes the file opening mode and sharing mode. Its possible values and meanings are as follows:

Open Mode:
Fmcreate: Creates a file with the specified file name. If the file already exists, open it.
Fmopenread: open a specified file in read-only mode
Fmopenwrite: open a specified file in write-only mode
Fmopenreadwrite: open a specified file in write mode
Sharing Mode:
Fm1_compat: The Shared Mode is compatible with FCBs.
Fm1_exclusive: do not allow other programs to open the file in any way
Fmsharedenywrite: do not allow other programs to open the file in write mode.
Fmsharedenyread: other programs are not allowed to open the file in Read mode.
Fmsharedenynone: other programs can open this file in any way

Tstream also has a derived class tmemorystream, which is frequently used in practical applications. It is called a memory stream, that is, to create a stream object in the memory. Its basic methods and functions are the same as above.

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.