Storages and Files are
one of the most important parts of the newObjects pack1 DLL. They are
base set - that deals with Storages and streams and extended set -
components that provide record based access to streams. The
description below is about the base components
The newObjects pack1 library deals with two
different abstract objects - Storages and Streams. All
the file system objects, memory blocks and other supported sources and targets are
represented as on of these types. For example the directories in the file system are
Storages, files are Streams, memory blocks are Streams too.
The library implements some
of the most often used storages and streams - such as:
Storages: Directories and OLE Compound files.
Streams: Files, Streams in OLE Compound files and memory
streams.
The library is able also to work with external implementations of the standard COM
IStorage and IStream interfaces.
What is Storage? Storage is an abstract container that may contain
other storages (sub-storages) and/or streams. Thus the storage is very similar to a
typical directory, but it is more abstract and in theory can be used to represent any
other directory-like container. The library uses the SFStorage
object to represent storages.
The following code creates a directory and then creates a subdirectory in it:
Dim main, dir1, dir2
Set main = CreateObject("newObjects.utilctls.SFMain")
Set dir1 = main.CreateDirectory("C:\directory1")
Set dir2 = dir1.CreateStorage("directory2")
Finally the directory C:\directory1\directory2 will be created. Note that the
sub-directory is created by calling the CreateStorage
method of the SFStorage object (dir1) returned by the CreateDirectory
method of SFMain object. This action can be translated in human language as "creating
a substorage of the same type in the already existing one". The "same
type" means that there is no standard way, nor guarantee that it is possible to
create a substorage of a different type. For example it is obvious - an OLE Compound file
can be created in a file system directory but not a file system directory in an OLE Compound file.
What is Stream? An stream is a sequence of bytes. Bytes can be read or
written sequentally from/to the stream. Each read operation begins at the position where
the previous operation has been completed. Thus if you are reading in a cycle one byte per
read opertion 1-rst read operation will read the first byte in the stream, 2-nd read
operation will read the second byte and so on. The same is true for the write operations.
Such kind of streams are called sequential streams. This abstract model
is a good representation of a sequence of data received from (or transmited to) some kind
of communication device. But there are many other examples of data blocks - files, memory
blocks and others. They can be accessed in more effective way by directly reading or
writting at a given position in the file or memory for example. These objects are often
named seekable streams (or also known as random access files) and they
add to the stream abstract model ability to maintain a current stream position
pointer. That pointer defines where the next read or write operation will begin. The
pointer can be moved to any byte in the stream at any time. Thus a seekable stream is very
similar to a typical file. Features like the name of a file or its attributes are external
features of the stream specific for a certain kind of stream (file in this case).
Teh following code opens a file (existing) moves to a certain position in the file,
reads some bytes and writes them to another stream - memory stream. The both, the file and
the memory stream, are represented as streams and except the lines required to create/open
them there is no need for the code to "know" the nature of each stream:
Dim main, file, mem, bytes
Set main = CreateObject("newObjects.utilctls.SFMain")
Set file = main.OpenFile("C:\myfile.bin")
Set mem = main.CreateMemoryStream
file.Pos = 1024 ' Move to the 1024-th byte in the file
bytes = file.ReadBin(100) ' Read 100 bytes - up to 1124-th byte
mem.WriteBin bytes ' write them to the memory stream
Thus anything that behaves like a stream (file) can be accessed the same way! Of course
the initial creation (obtaining, opening) of a given stream depends on its nature and
requires call of a specific method or use of an external object, but after that step it is
just a stream represented by the SFStream object.
The newObjects pack1 library provides several additional objects - such as
SFRecord and SFField. Because of the general abstraction they can be used over any stream - no matter
if it is a memory stream, file, a stream in an OLE Compound file or a stream
behavior
provided by an external object. What is the catch? The universality requires abstraction,
thus all the features specific to a specific streams only are not directly accessible
through the SFStream object. For example it cannot be used to
obtain the file attributes because file has attributes but memory stream does not. One
look at the methods and properties of the SFStream object will show that the price is not
too high - only a few features are not accessible and, of course, for
most of them there is another way to
manage them - through the SFMain object. Also the features not common for all the stream
types are rarely used.
|