Skip to main content

HS.Util.DeviceStream

stream class HS.Util.DeviceStream extends %Stream.Object

This class is intended to support serialization of large JSON objects to a device. In particular, a REST handler emits its response by writing to the default device using 'write'. Without this class, it is necessary to convert the %DynamicObject to a %String and then write the string (e.g. Do write jsonObj.%ToJSON() However, if the string is longer than MAXSTRING, this results in an error. This class presents a %Stream.Object interface and passes all written data to the default device (via write). When %DynamicObject serializes to a %Stream.Object, it emits to the stream in chunks so we don't run into the MAXSTRING limitation. Usage: #dim devStream as %Stream.Object = ##class(HS.Util.DeviceStream).%New() do json.%ToJSON(.devStream)

Method Inventory

Methods

method Read(ByRef len As %Integer = 32656, ByRef sc As %Status) as %CacheString
Inherited description: Reads up to len characters from the current position in the stream. The current position is advanced by the number of characters read. Upon exit, len is set to the actual number of characters read. If a read occurs when the stream position is at the end of the stream, len will be set to -1 and Read() will return a null string (""). If no len is passed in, ie. 'Read()()' then it is up to the Read implementation as to how much data to return. Some stream classes use this to optimize the amount of data returned to align this with the underlying storage of the stream.

You must call Rewind() if you want to read a stream from the beginning again. Calling Read() after Write() implicitly ends the Write() operation and rewinds to the start of the stream.

Returns a string up to len characters long. The byref argument sc will return a %Status if any error occurred during the read.

method ReadLine(ByRef len As %Integer = 32000, ByRef sc As %Status, ByRef eol As %Boolean) as %CacheString
Inherited description: Read a line from the stream. This will look for the line terminator in the stream and once it finds the terminator it will return the string minus the terminator character/s. If it reaches the end of the stream before it finds a terminator it will return the data it has so far, and if you specify a maximum size in len it will only read up to this number of characters. On exit len will contain the actual number of characters read. The byref argument sc will return a %Status() if any error occured during the read and the byref argument eol is true if it found the line terminator and false otherwise. So for example you can read in a stream a line at a time and output the results to the current device with:
While 'stream.AtEnd { Write stream.ReadLine(,.sc,.eol) If $$$ISERR(sc) { Write "ERROR" Quit } If eol { Write ! } }
method ReadLineIntoStream(ByRef sc As %Status) as %Stream.Object
method Write(data As %CacheString) as %Status
Inherited description: Appends the string data to the stream and advances the current stream position by the number of characters in data.

Note that a write operation immediately following a read or rewind will clear out the existing data in the stream.

Returns a %Status value indicating success or failure.

method WriteLine(data As %CacheString = "") as %Status
Inherited description: Appends the string data along with a line terminator to the stream and advances the current stream position by the number of characters in data plus the line terminator.

Returns a %Status value indicating success or failure.

Inherited Members

Inherited Properties

Inherited Methods

FeedbackOpens in a new tab