Wednesday, February 07, 2007

 

InStream and OutStream

Recently I encountered some problems using the InStream and OutStream, and since they were introduced in Navision, it has been very confusing on how they should be used.

One of the most convinient usages of Streams is the aid of reading and writting big texts, and by big we mean really big texts. Since Navision has a limit of around 1024 to 2048 characters, Streams provide one of the more convinient ways of dealing with webpages and long text documents directly in Navision.

However, bare in mind that there are 2 ways of writting and 2 ways of reading Streams: binary and text wised. So, whenever you are using OutStream.Write, you are writting binary, and when using OutStream.WriteTEXT, you are, literally writting only the text part of the parameter.

So, when using READ, versus READTEXT, you will be seeking for the NULL termination that the binary text will WRITE out. That is why you will get an AL error if this NULL termination was not found.

As an example:

text[3]: t1; // text of length 3
text[4]: t2; // text of length 4
IS:InStream;
OS:OutStream;

t1:= "ABC";
t2:= "ABC";

OS.WRITE(t1); // This will write to the Stream: 'A' 'B' 'C' chr(0)
OS.WRITE(t2); // This will write to the Stream: 'A' 'B' 'C' chr(0)

// If using the same Stream ....
IS.READ(t1); // Should read 'A' 'B' 'C' chr(0)
IS.READ(t2); // Should read 'A' 'B' 'C' chr(0)

This only works in 4.00.SP3 versions and will fail in previous versions, however, there is a side effect to the correction. Take this example:

text[3]: t1; // text of length 3
text[1]: t2; // text of length 1

integer: i;
IS:InStream;
OS:OutStream;

t1:= "ABC";

OS.WRITE(t1); // This will write to the Stream: 'A' 'B' 'C' chr(0)
OS.WRITE(t1); // This will write to the Stream: 'A' 'B' 'C' chr(0)

// If using the same Stream ....
i := IS.READ(t2); // This will read 'A' 'B' into a text[1] !!!

// i will be 2.

Also, notice that, as a side effect, this code is not valid:

text[1]: t2; // text of length 1
IS:InStream;
text[1024]: biggerText;
// Reading any Stream, for instance a text file

WHILE (IS.READ(textChar) > 0) DO
BEGIN
biggerText := biggerText + textChar;
END;

What happened here? It is actually simple: "IS.READ(textChar)"is not reading 1 character, but 2.

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?