Rapid-Q Documentation by William Yu (c)1999, appended by John Kelly Appendix A: QMEMORYSTREAM

 

QMEMORYSTREAM Component

QMemorystream is used to store temporary data to memory for manipulation. It can be used for dynamic arrays, or simply reducing memory after data is used.

QMemorystream Properties
FieldTypeR/WDefaultSupport





LineCountINTEGERRW
LineCount determines how many lines there are in the memorystream. Note that LineCount ignores the CR character, and will only detect LF, so it treats CRLF as one line, but LFLF as 2 lines.
PointerINTEGERRWXG
Pointer specifies the memory address of the memorystream.
PositionINTEGERRWWXG
Position specifies the current position of the memory pointer, any read/write operations will begin from here.
SizeINTEGERRWWXG
Size determines the number of bytes allocated to the memorystream.


QMemorystream Methods
MethodTypeDescriptionParamsSupport





CloseSUBClose the stream
Comment: If you close the memorystream, you reset the whole stream. No need to open it again (there isn't even an open method for qmemorystream), just write to the memorystream again. Alain 22 Aug 2002
0WXG
CopyFromSUB (Stream, Bytes%)Copy from another stream
Either QFileStream or QMemoryStream
2WXG
Use CopyFrom to copy a QFILESTREAM or a QMEMORYSTREAM to the current memorystream.
Details:
If Bytes% = 0 then the Stream is reset to position 0 and the whole stream is then copied.
Example:
$INCLUDE "RAPIDQ.INC"
DIM File1 AS QFILESTREAM
DIM Mem AS QMEMORYSTREAM

File1.Open("test.txt", fmOpenRead)
Mem.CopyFrom(File1, 123) '-- Copy 123 bytes from File1
ExtractResSUB (Resource AS LONG)Extract resource to memorystream1WXG
Extracts a resource from your program to the memorystream.
Details:
The parameter Resource is not the resource handle, but the absolute position of the resource within your file. This requires that you use the keyword Resource(n) to specify the resource number.
Example (dumping all resources):
$INCLUDE "RAPIDQ.INC"
$RESOURCE res_1 as "res.1"
$RESOURCE res_2 as "res.2"

DIM Mem AS QMEMORYSTREAM

FOR I = 0 TO ResourceCount-1
    Mem.ExtractRes(Resource(I))
NEXT
LoadArraySUB (Array(), NumElements&)Load data into array2WXG
MemCopyFromSUB (Address&, Bytes&)Copies contents of Address& to memorystream2WXG
MemCopyToSUB (Address&, Bytes&)Copies contents to Address&2WXG
ReadSUB (variable)Generic Read, determines the storage space and saves data in variable1WXG
ReadLineFUNCTION () AS STRINGReads an entire line0W
ReadNumFUNCTION (n) AS DOUBLERead n bytes, returns number1WXG
ReadStrFUNCTION (n) AS STRINGRead n bytes, returns the string1WXG
ReadBinStrFUNCTION (n) AS STRINGRead n bytes, returns the binary string1W
ReadUDTSUB (MyType)Read and store data in a MyType structure1WXG
SaveArraySUB (Array(), NumElements&)Save array2WXG
SeekSUB (Position%, From%)Seek to Position%, From% see RAPIDQ.INC2WXG
WriteSUB (variable)Generic Write, determines the storage space and saves data to memory1WXG
WriteLineSUB (S AS STRING)Writes string with CR+LF1W
WriteNumSUB (number, bytes%)Writes a number to memory2WXG
WriteBinStrSUB (string, bytes%)Writes a binary string to file (slow)2W
WriteStrSUB (string, bytes%)Writes string to memory2WXG
WriteUDTSUB (MyType)Write data stored in a MyType structure1WXG


QMemoryStream Examples
DIM Mem AS QMemoryStream

  S$ = "Hello world!"
  Mem.WriteStr(S$, LEN(S$))
  Mem.Position = 0
  S$ = Mem.ReadStr(LEN(S$))
  PRINT S$                            '' print it
  Mem.Close

'------------------------------------------------------

  DIM Mem AS QMemoryStream
  DIM I AS INTEGER

  I = 12
  Mem.Write(I)
  S$ = "Hello world!"
  Mem.Write(S$)
  Mem.Position = 0
  Mem.Read(I)
  S$ = SPACE$(I)
  Mem.Read(S$)
  PRINT S$                            '' print it
  Mem.Close

'------------------------------------------------------

  TYPE MyType
      Name AS STRING*30
      Phone AS STRING*8
      Age AS INTEGER
  END TYPE

  DIM Person AS MyType
      Person.Name = "Joe"
      Person.Phone = "555-5555"

  DIM Mem AS QMemoryStream
      Mem.WriteUDT(Person)
      

To correctly deallocate memory from QMEMORYSTREAM you should 
set Position and Size to 0 and then set one value. To see how 
this works, run Task Manager to follow memory in process.
Then run this code:

DIM Mem AS QMemoryStream

Showmessage "Pre memory"
Mem.Size = 3200005
S$ = Space$(3200000)
Mem.WriteStr(S$, LEN(S$))
Showmessage "Memory allocated"

'now deallocate
Mem.Position = 0
Mem.Size = 0
Showmessage "Memory should be all deallocated"

'loading a value finally resets memory
S$ = "hi"
Mem.WriteStr(S$, LEN(S$))
Showmessage "Finally deallocated by assigning value"
Mem.Close
Application.Terminate

Prev Component Contents Next Component