1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Streams, streams, ... TStream
A stream is what its name suggests: a flowing "river of data". A stream has a beginning, an end, and you're always somewhere in between of these two points. Learn about using the TStream class in Delphi: how to use stream objects to read from, write to, or copy information stored in a particular medium.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Download sample application
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Images in database
• DLL in EXE
• VCL using articles

Article submitted by Dirk Claessens, text co-authored by Zarko Gajic.

What is a stream? TStream?
A stream is what its name suggests: a flowing "river of data". A stream has a beginning, an end, and you're always somewhere in between of these two points.
Using Delphi's TStream objects you can read from or write to various kinds of storage media, such as disk files, dynamic memory, and so on.
What data can a stream contain?
A stream can contain anything you like, in the order you like. In the example project accompanying this article, fixed-size records are used for simplicity purposes, but you can write any mix of variable-sized data to a stream. Remember however, that _you_ are responsible for the householding. There is no way Delphi can "remember" what kind of data are in a stream, or in what order!
Streams versus arrays
Arrays have the disadvantage of having a fixed size that must be known at compile time. Ok, you can use dynamic arrays.
A stream on the other hand, can grow up to the size of available memory, which is considerably large size on today's systems, without any "householding" chores.
A stream cannot be indexed, as an array can. But as you'll see below, "walking" up and down a stream is very easy.
Streams can be saved/loaded to/from files in one simple operation.
Flavours of streams
TStream is the base (abstract) class type for stream objects. Being abstract means that TStream should never be used as such, but only in it's descendant forms. For streaming any kinds of information, choose a descendant class according to the specific data and storage needs. For example:
  • TFileStream (for working with files)
  • TMemoryStream (for working with a memory buffer)
  • TStringStream (for manipulating in-memory strings)
  • 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)
As you'll see in the sample project, TmemoryStream and TFileStream are remarkably interchangeable and compatible.

Delphi streams example

Download sample project!

Want to discuss using streams? Post your question on the Delphi Programming Forum.

©2016 About.com. All rights reserved.