Write, WriteText, Read, and ReadText Method Behavior for Line Endings and Zero Terminators

When writing to and reading from a stream object using the Write, WriteText, Read, and ReadText methods, it is important to be aware of the following behavior regarding line endings and zero byte terminators:

  • Write adds a 0 byte at the end of the stream; WriteText does not.
  • Read Reads until a 0 byte or the specified length of the string.
  • ReadText Reads the until a zero byte, an end-of-line, the specified number of bytes, or the maximum length.

To help understand this, consider the following code example. This example assumes that a BLOB field called MyBlobField exists in a table called MyBlobTable.

var
    myInt: Integer;
    MyRecord: Record MyBlobTable;
    MyOutStream: OutStream;
    MyInStream: InStream;
    Result: Text;
    CRLF: Text[2];
}
begin
    MyRecord.MyBlobField.CreateOutStream(MyOutStream);
    MyOutStream.WriteText('A');
    MyOutStream.WriteText;
    MyOutStream.WriteText('B');
    MyOutStream.WriteText;
    MyOutStream.WriteText('C');

    MyRecord.MyBlobField.CreateInStream(MyInStream);
    MyInStream.ReadText(Result); // Reads A
    Message(Result);
    MyInStream.ReadText(Result); // Reads B
    Message(Result);
    MyInStream.ReadText(Result); // Reads C
    Message(Result);

    MyRecord.MyBlobField.CreateInStream(MyInStream);
    MyInStream.Read(Result); // Reads A\B\C
    Message(Result);

    CRLF[1] := 13;
    CRLF[2] := 10;
    MyRecord.MyBlobField.CreateOutStream(MyOutStream);
    MyOutStream.Write('A' + CRLF + 'B');
    MyOutStream.Write('C');

    MyRecord.MyBlobField.CreateInStream(MyInStream);
    MyInStream.ReadText(Result); // Reads A
    Message(Result);

    MyInStream.ReadText(Result); // Reads B
    Message(Result);

    MyInStream.ReadText(Result); // Reads C
    Message(Result);


    MyRecord.MyBlobField.CreateInStream(MyInStream);
    MyInStream.Read(Result); // Reads A + CRLF + B
    Message(Result);

    MyInStream.Read(Result); // Reads C
    Message(Result);

    Clear(MyRecord.MyBlobField);
    MyRecord.MyBlobField.CreateOutStream(MyOutStream);
    MyOutStream.WriteText('A' + CRLF + 'B');
    MyOutStream.WriteText('C');

    MyRecord.MyBlobField.CreateInStream(MyInStream);
    MyInStream.Read(Result); // Reads A + CRLF + BC
    Message(Result);


    MyRecord.MyBlobField.CreateInStream(MyInStream);
    MyInStream.ReadText(Result); // Reads A
    Message(Result);
    MyInStream.ReadText(Result); // Reads BC
    Message(Result);

end;

See Also

Write and WriteText Methods
Read and ReadText Methods
AL Development Environment