The memory stream classes, Progress.IO.MemoryOutputStream and Progress.IO.MemoryInputStream encapsulate the MEMPTR data type, and make it easier to perform MEMPTR operations.

For example, you can write to memory referenced by a MEMPTR without worrying about resizing, keeping track of read/write positions, or remembering when to free it. The memory stream object automatically resizes the MEMPTR structure when needed and frees it when the stream object goes out of scope.

Note: The memory stream object's internal MEMPTR property is read-only. It can be publicly accessed for read operations such as GET-STRING, GET-BYTES, GET-SIZE and so on. Any write operations such as PUT-STRING, PUT-BYTE, or SET-SIZE on this property causes a runtime error indicating that AVM is unable to perform such operation on a read only MEMPTR.

The following ABL code samples demonstrate how memory stream classes simplify the use of MEMPTRs.

MEMPTRs defined without using memory stream classes

1.   DEFINE VARIABLE len          AS INT64.
2.   DEFINE VARIABLE i            AS INT64.
3.   DEFINE VARIABLE mData        AS MEMPTR.
4.   DEFINE VARIABLE iPosition    AS INT.
5.   DEFINE VARIABLE iLen         AS INT.
6.   DEFINE VARIABLE tempM        AS MEMPTR.
7.   DEFINE VARIABLE size         AS INT64.
8.   size = 1000.
9.   SET-SIZE(mData) = size.
10.  iPosition = 1.
11.  FOR EACH CUSTOMER:
12.     iLen = LENGTH(Name).
13.     IF iPosition + iLen > size THEN DO:
14.       tempM = mData.
15.       size = size * 2.
16.       SET-SIZE(mData) = 0.
17.       SET-SIZE(mData) = size.
18.       PUT-BYTES(mData,1) = tempM.
19.       SET-SIZE(tempM) = 0.
20.     END.
21.     PUT-STRING(mData, iPosition, iLen) = Name.
22.     iPosition = iPosition + iLen.
23.  END.
24.  . . . /* Use memptr, for example send on a socket connection */
25.  SET-SIZE(mData) = 0.

MEMPTRs defined as memory stream classes

1.   USING Progress.IO.
2.   DEFINE VARIABLE mStreamObj AS Progress.io.MemoryOutputStream.
3.   mStreamObj = NEW Progress.io.MemoryOutputStream().
4.   FOR EACH CUSTOMER: 
5.     mStreamObj:Write(Name).
6.   END.
7.   /* mStreamObj goes out of scope, gets garbage collected, and memory 
8.   is automatically freed. */

See the ABL Reference for more information about these memory stream classes.