Opening streams – PB Docs 125

Opening streams

Streams contain the raw data of an OLE object. You would not
want to alter a stream created by a server application. However,
you can add your own streams to storage files. These streams can
store information about the storages. You can write streams that
provide labels for each storage or write a stream that lists the
members of the storage.

To access a stream in an OLE storage file, you define a stream
variable and instantiate it. Then you open a stream from a storage
that has already been opened. Opening a stream establishes a connection
between the stream variable and the stream data within a storage.

The following code declares and creates OLEStorage and OLEStream variables,
opens the storage, and then opens the stream:

PowerBuilder has several stream functions for opening and
closing a stream and for reading and writing information to and
from the stream.

Table 19-11: Stream functions

Function

Result

Open

Opens a stream into the specified OLEStream
variable. You must have already opened the storage that contains
the stream.

Length

Obtains the length of the stream in bytes.

Seek

Positions the read/write pointer
within the stream. The next read or write operation takes place
at the pointer.

Read

Reads data from the stream beginning
at the read/write pointer.

Write

Writes data to the stream beginning at
the read/write pointer.

If the pointer is not at the end, Write overwrites
existing data. If the data being written is longer than the current
length of the stream, the stream’s length is extended.

Close

Closes the stream, breaking the connection
between it and the OLEStream variable.

Example: writing and reading streams

This example displays a picture of a product in the OLE control ole_product when
the DataWindow object dw_product displays
that product’s inventory data. It uses the file constructed
with the utility application described in the earlier example (see “Example: building
a storage”
). The
pictures are stored in an OLE storage file, and the name of each
picture’s storage is also the product identifier in a database
table. This example adds label information for each picture, stored
in streams whose names are the product ID plus the suffix _lbl.

Figure 19-6 shows
the structure of the file.

Figure 19-6: OLE storage file structure

oleap08.gif

The example has three scripts:

  • The window’s Open event script opens the storage
    file and retrieves data for the DataWindow object. (Note that the
    application’s Open event connects to the database.)

  • The RowFocusChanged event of the DataWindow object
    displays the picture. It also opens a stream with a label for the
    picture and displays that label in a StaticText. The name of the
    stream is the product identifier plus the suffix _lbl.

    If the label is empty (its length is zero), the script writes
    a label. To keep things simple, the data being written is the same
    as the stream name. (Of course, you would probably write the labels
    when you build the file and read them when you display it. For the
    sake of illustration, reading and writing the stream are both shown
    here.)

  • The window’s Close event script saves the
    storage file and destroys the variable.

The OLEStorage variable stg_prod_pic is
an instance variable of the window:

The script for the window’s Open event is:

The script for the RowFocusChanged event of dw_prod is:

The script for the window’s Close event is:


Document get from Powerbuilder help
Thank you for watching.
Was this article helpful?
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x