How to read global via Stream interface?

Primary tabs

ObjectScript, Caché

I have global with binary data, structured like:




What could be easiest way to read it via Stream interface? I.e. to pass an adapter to a function expecting Stream?


  • + 1
  • 0
  • 205
  • 2
  • 2


look at this exercise, it may help you to get how to achieve it

  Set id = 1
  Set streamGN = $Name(^IRIS.TempStream)
  Kill @streamGN
  Set @streamGN@(id, 1) = "some binary data chunk 1"
  Set @streamGN@(id, 2) = "some binary data chunk 1"

  Set lastChunk = $Order(@streamGN@(id, ""), -1)
  Set @streamGN@(id) = lastChunk
  Set size = 0
  For chunk=1:1:lastChunk {
    Set size = size + $Length(@streamGN@(id, chunk))
  Set @streamGN@(id, 0) = size

  Set stream = ##class(%Stream.GlobalBinary).%Open($Listbuild(id, , streamGN))
  While 'stream.AtEnd {
    Write !,stream.Read()

Nice, thanks. But it seems that this way will work only if the global has an ID as first index. 

If you have your binary or character data in (sequential) global nodes, then:
- create your own stream clas
-in the %OnNew() method fetch the data from your global

and voila, you are ready to use your data with stream methods.

set ^mydata(1)="abcd"
set ^mydata(2)="efgh"

set ^yourdata("id",1)="1234"
set ^yourdata("id",2)="5678"

Class Test.Stream Extends %Stream.TmpBinary
Method %OnNew(Global, initval As %String = "") As %Status [ Private ]
Set i%%Location=$select(initval="":$$$streamGlobal,1:initval)
do ..Clear()
for set Global=$query(@Global,1,data) quit:Global="" do ..Write(data)
do ..Rewind()
Quit $$$OK

set stream=##class(Test.Stream).%New($na(^mydata))
while 'stream.AtEnd { write stream.Read(5) }

set stream=##class(Test.Stream).%New($na(^yourdata("id")))
while 'stream.AtEnd { write stream.Read(5) }

This way involves copying of the data but thanks, can be useful in some cases