Delphi编程中“流”的应用 什么是流?流,简单来说就是建立在面向对象基础上的一种抽象的处理数据的工具。在流中,定义了一些处理数据的基本操作,如读取数据,写入数据等,程序员是对流进行所有操作的,而不用关心流的另一头数据的真正流向。流不但可以处理文件,还可以处理动态内存、网络数据等多种数据形式。如果你对流的操作非常熟练,在程序中利用流的方便性,写起程序会大大提高效率的。一、Delphi中流的基本概念及函数声明
在Delphi中,所有流对象的基类为TStream类,其中定义了所有流的共同属性和方法。
TStream类中定义的属性介绍如下:
1、Size:此属性以字节返回流中数据大小。
2、Position:此属性控制流中存取指针的位置。Tstream中定义的虚方法有四个:
1、Read:此方法实现将数据从流中读出。函数原形为:
Function Read(var Buffer;Count:Longint):Longint;virtual;abstract;
参数Buffer为数据读出时放置的缓冲区,Count为需要读出的数据的字节数,该方法返回值为实际读出的字节数,它可以小于或等于Count中指定的值。
2、Write:此方法实现将数据写入流中。函数原形为:
Function Write(var Buffer;Count:Longint):Longint;virtual;abstract;
参数Buffer为将要写入流中的数据的缓冲区,Count为数据的长度字节数,该方法返回值为实际写入流中的字节数。
3、Seek:此方法实现流中读取指针的移动。函数原形为:
Function Seek(Offset:Longint;Origint:Word):Longint;virtual;abstract;
参数Offset为偏移字节数,参数Origint指出Offset的实际意义,其可能的取值如下:
soFromBeginning:Offset为移动后指针距离数据开始的位置。此时Offset必须大于或者等于零。
soFromCurrent:Offset为移动后指针与当前指针的相对位置。
soFromEnd:Offset为移动后指针距离数据结束的位置。此时Offset必须小于或者等于零。该方法返回值为移动后指针的位置。
4、Setsize:此方法实现改变数据的大小。函数原形为:
Function Setsize(NewSize:Longint);virtual;另外,TStream类中还定义了几个静态方法:
1、ReadBuffer:此方法的作用是从流中当前位置读取数据。函数原形为:
Procedure ReadBuffer(var Buffer;Count:Longint);
参数的定义跟上面的Read相同。注意:当读取的数据字节数与需要读取的字节数不相同时,将产生EReadError异常。
2、WriteBuffer:此方法的作用是在当前位置向流写入数据。函数原形为:
Procedure WriteBuffer(var Buffer;Count:Longint);
参数的定义跟上面的Write相同。注意:当写入的数据字节数与需要写入的字节数不相同时,将产生EWriteError异常。
3、CopyFrom:此方法的作用是从其它流中拷贝数据流。函数原形为:
Function CopyFrom(Source:TStream;Count:Longint):Longint;
参数Source为提供数据的流,Count为拷贝的数据字节数。当Count大于0时,CopyFrom从Source参数的当前位置拷贝Count个字节的数据;当Count等于0时,CopyFrom设置Source参数的Position属性为0,然后拷贝Source的所有数据;
在Delphi中,所有流对象的基类为TStream类,其中定义了所有流的共同属性和方法。
TStream类中定义的属性介绍如下:
1、Size:此属性以字节返回流中数据大小。
2、Position:此属性控制流中存取指针的位置。Tstream中定义的虚方法有四个:
1、Read:此方法实现将数据从流中读出。函数原形为:
Function Read(var Buffer;Count:Longint):Longint;virtual;abstract;
参数Buffer为数据读出时放置的缓冲区,Count为需要读出的数据的字节数,该方法返回值为实际读出的字节数,它可以小于或等于Count中指定的值。
2、Write:此方法实现将数据写入流中。函数原形为:
Function Write(var Buffer;Count:Longint):Longint;virtual;abstract;
参数Buffer为将要写入流中的数据的缓冲区,Count为数据的长度字节数,该方法返回值为实际写入流中的字节数。
3、Seek:此方法实现流中读取指针的移动。函数原形为:
Function Seek(Offset:Longint;Origint:Word):Longint;virtual;abstract;
参数Offset为偏移字节数,参数Origint指出Offset的实际意义,其可能的取值如下:
soFromBeginning:Offset为移动后指针距离数据开始的位置。此时Offset必须大于或者等于零。
soFromCurrent:Offset为移动后指针与当前指针的相对位置。
soFromEnd:Offset为移动后指针距离数据结束的位置。此时Offset必须小于或者等于零。该方法返回值为移动后指针的位置。
4、Setsize:此方法实现改变数据的大小。函数原形为:
Function Setsize(NewSize:Longint);virtual;另外,TStream类中还定义了几个静态方法:
1、ReadBuffer:此方法的作用是从流中当前位置读取数据。函数原形为:
Procedure ReadBuffer(var Buffer;Count:Longint);
参数的定义跟上面的Read相同。注意:当读取的数据字节数与需要读取的字节数不相同时,将产生EReadError异常。
2、WriteBuffer:此方法的作用是在当前位置向流写入数据。函数原形为:
Procedure WriteBuffer(var Buffer;Count:Longint);
参数的定义跟上面的Write相同。注意:当写入的数据字节数与需要写入的字节数不相同时,将产生EWriteError异常。
3、CopyFrom:此方法的作用是从其它流中拷贝数据流。函数原形为:
Function CopyFrom(Source:TStream;Count:Longint):Longint;
参数Source为提供数据的流,Count为拷贝的数据字节数。当Count大于0时,CopyFrom从Source参数的当前位置拷贝Count个字节的数据;当Count等于0时,CopyFrom设置Source参数的Position属性为0,然后拷贝Source的所有数据;
解决方案 »
- 一个字符串的问题!!!!!
- fastReoprt 4.2 改了很多东西,重大发现 SetSizeAndDimensions(256, PageW, PageH);
- 求一主从表操作的例子源码, 顺祝D兄新年快乐~~~~~~~~~
- 高高高手进来!!!!!!!!
- fastreport分栏打印问题急救,在线等...
- 不需要在界面上显示数据,我应该把代码写在什么控件的什么事件中呢?
- 如何将从一个端口接收的数据流,转发到其他任意数量的端口?
- 请问在DELPHI的嵌入式汇编下怎样操作指针
- delphi7挺有意思,在每个组件的提示信息里面把组件所属的单元都列出了
- 哪里有ftp程序的源代码或相关资料??
- 关于用ado控制sql server2000数据库的问题。
- 风声、雨声、读书声,声声入耳。
TFileStream (for working with files)
TStringStream (for manipulating in-memory strings)
TMemoryStream (for working with a memory buffer)
TBlobStream (for working with BLOB fields)
TWinSocketStream (for reading and writing over a socket connection)
TOleStream (for using a COM interface to read and write)
One of the best features of the VCL is the ability to save the object’s state
to a stream and retrieve it later from the stream. There are many VCL
objects that provide this feature through the functions
SaveToStream(TStream *stream) and LoadFromStream(TStream
*stream). TStream is the base class for VCL-style streams. Do not confuse
VCL streams with C++ IOStreams. The VCL functionality is always available
on top of whatever standard C++ language provides.
TStream defines all the base functionality of a stream that can read from
or write to various kinds of storage media, such as disk files, dynamic
memory, and so on. Stream objects permit applications to seek an arbitrary
position in the stream for either reading or writing. It is the
responsibility of the programmer to interpret the data read, by mapping
to the appropriate object’s structure or data layout. In simple terms, any
object’s contents can be saved to a stream, provided methods similar to
that mentioned above are made available by the respective component
writers. The main features of streams are data access from an arbitrary
position in the stream and unified data access technique without regard to
the storage media of the stream. The descendent class that derives from
TStream takes care of the media access techniques, and hence it is the
responsibility of the component writers and not the application
programmers.
TStream also introduces methods that work in conjunction with components
and files for loading and saving components in simple and inherited
forms. These methods are called automatically by global routines that initiate
component streaming. They can also be called directly to initiate the
streaming process. These are ReadComponent, ReadComponentRes,
WriteComponent, and WriteComponentRes.
Two important properties of the TStream object are Position and Size.
Position indicates the current location of the stream pointer (similar to file
pointer) from the beginning of the stream. Size indicates current memory
(in bytes) of the stream. Methods that load (or add) contents to the
stream set the size of the stream internally; setting this value explicitly
has no effect. When the TStream (descendent) object is created, Size is 0
and Position is 0. As data is added, the size grows, and the current pointer
location moves away from the beginning of the stream. After the data is
added and before it is read, the stream pointer has to be set to the beginning
(or any appropriate location in the stream) explicitly by the
programmer; otherwise, access violations occur. When data is read from
the stream from a location by a specific number of bytes, it only means
that the data is copied from the stream to another memory location by a
specified number of bytes, and the stream pointer is moved forward by
the same number of bytes; data is not removed from the stream. To read
the same set of bytes, move the stream pointer back by the same number
of bytes and do another read. To clear the contents of the stream object,
the descendent class must implement a corresponding method.
The descendents of TStream as defined in the VCL are explained below.
The purpose of these stream objects may be different based on their
design, but they all share the same streaming features explained above.
TFileStream — This stream object makes it easy to read from and
write to a disk file. Component writers can implement methods to
save the components’ status to disk file and rebuild the component
status back from the disk file. There is a Seek(int offset, Word origin)
method that must be used to position the stream pointer at an
appropriate location before the read or write operation begins. In
The VCL Explored 49
5
Chapter
fact, some of the VCL components currently support this feature
(along with some third-party vendor components).
TMemoryStream — This stream object makes it easy to read from
and write to a memory stream. By saving components’ status to
memory stream, application developers can develop uniform and
unique logic to save the component status and transmit across the
network to other computers, where the receiving applications can
restore the components if they know the object structure that the
stream contains. This can be used as a simple alternate method to
transfer objects across the network without bothering to learn
complex architectures like DCOM and CORBA. (This is explained
in a later chapter in more detail.) However, component writers
have to implement methods to save the object status to the memory
stream. Some of the VCL components currently support this
feature (along with some third-party vendor components).
TStringStream — This stream object provides file-like access to
create string objects, as opposed to more structured AnsiString
object. It is useful as an intermediary object that can hold text as
well as read it from or write it to another storage medium.
TWinSocketStream — This stream object provides services that
allow applications to read from or write to socket connections.
Socket connections are of two types: blocking and non-blocking.
In non-blocking socket connections, the read or write across the
socket is performed asynchronously, and hence the events are fired
only when the socket is ready for read or write and data is awaiting
at the port. But in case of blocking connections, the read and
write occur synchronously and the client application cannot do
anything else as long as the read or write operation is in progress
or waiting. Since the reading process cannot wait indefinitely until
the socket is ready for reading with data at the port, the
TWinSocketStream object provides a waiting feature with a timeout
mechanism; it is required to read over the blocking socket.
More on sockets is explained later in another chapter.
TBlobStream — This stream object is designed to enable reading
and writing BLOB field data in a database table. BLOB field in a
table is designed to hold binary large objects including pictures or
large memos. Every time BLOB field data is read from a table or
written to the table, it is necessary to create a TBlobStream object
to enable this read or write operation. There are two ways to
50 Chapter 5
create this stream object. Using the new operator and passing two
arguments (the BLOBField object and the stream access mode) to
the constructor creates an instance of the TBlobStream object to
operate on the specific BLOBField of the table. The second way is
to call the CreateBlobField method on the TTable object and provide
the same two parameters. In either case, the stream object is
instantiated and it is the programmer’s responsibility to delete the
object after use. It is also important to keep in mind that there is
no terminating character that identifies the end of a BLOB stream
data, and hence the parameter count (which is an integer value)
must be supplied with the exact number of bytes to be transferred.
TOleStream — This stream object reads and writes information
over a streaming interface that is provided by an OLE object.
The VCL Explored 51
http://expert.csdn.net/Expert/topic/1710/1710052.xml?temp=.8245355http://expert.csdn.net/Expert/topic/1710/1710052.xml?temp=.5602686