25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Streams<br />

15.2.2 Copying Data between Streams<br />

All streams contain a CopyFrom method which allows one stream to fill itself from another<br />

stream. This can be used, for example, to copy data from a file into a stream property:<br />

// open a text file using a %Library.File stream<br />

Set file = ##class(%File).%New("\data\textfile.txt")<br />

Do file.Open("RU") ; same flags as OPEN command--use "U" for streams<br />

// Open a Person object containing a Memo stream<br />

// and copy the file into Memo<br />

Set person = ##class(Person).%New()<br />

Do person.Memo.CopyFrom(file)<br />

// save the person object and close it, then close the file<br />

Do person.%Save()<br />

Do person.%Close()<br />

Do file.%Close()<br />

15.2.3 Inserting Stream Data<br />

Streams have both a temporary and a permanent storage location. All inserts go into the<br />

temporary storage area, which is only made permanent when you save the stream. If you start<br />

inserting into a stream, then decide that you want to abandon the insert, the data stored in the<br />

permanent location will not be altered.<br />

If you create a stream, start inserting, then do some reading you can call MoveToEnd and<br />

then continue appending to the temporary stream data. However after you save the stream,<br />

the data is moved to the permanent storage location. If you then reload the stream and start<br />

inserting, it will insert into the temporary storage area, rather than appending to the permanently<br />

stored data.<br />

If this is the behavior you want you will need to create a temporary stream, for example:<br />

Set test = ##class(Test).%OpenId(5)<br />

Set tmpstream = ##class(%GlobalCharacterStream).%New()<br />

Do tmpstream.CopyFrom(test.text)<br />

Do tmpstream.MoveToEnd()<br />

Do tmpstream.Write("append text")<br />

Set test.text = tmpstream<br />

Do tmpstream.%Close()<br />

// Now do whatever you want with the test object<br />

In this example, we create a temporary stream of the required type, then copy from the stream<br />

stored in the Test object, which will put this data in the temporary storage area of our new<br />

stream. Then we append to this stream and put its oref into the Test object and close it to keep<br />

the reference counts correct.<br />

136 <strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!