17.9 TMemoryStream

The TMemoryStream object implements a stream that stores it’s data in memory. The data is stored on the heap, with the possibility to specify the maximum amout of data, and the the size of the memory blocks being used.

 TYPE
    TMemoryStream = OBJECT (TStream)
          BlkCount: Sw_Word;       { Number of segments }
          BlkSize : Word;          { Memory block size  }
          MemSize : LongInt;       { Memory alloc size  }
          BlkList : PPointerArray; { Memory block list  }
       CONSTRUCTOR Init (ALimit: Longint; ABlockSize: Word);
       DESTRUCTOR Done;                                               Virtual;
       PROCEDURE Truncate;                                            Virtual;
       PROCEDURE Read (Var Buf; Count: Sw_Word);                      Virtual;
       PROCEDURE Write (Var Buf; Count: Sw_Word);                     Virtual;
    END;
    PMemoryStream = ^TMemoryStream;

TMemoryStream.Init

Declaration:
Constructor TMemoryStream.Init (ALimit: Longint; ABlockSize: Word);
Description:
Init instantiates a new TMemoryStream object. The memorystreamobject will initially allocate at least ALimit bytes memory, divided into memory blocks of size ABlockSize. The number of blocks needed to get to ALimit bytes is rounded up.

By default, the number of blocks is 1, and the size of a block is 8192. This is selected if you specify 0 as the blocksize.

Errors:
If the stream cannot allocate the initial memory needed for the memory blocks, then the stream’s status is set to stInitError.
See also:
Done (589)

For an example, see e.g TStream.CopyFrom (574).

TMemoryStream.Done

Declaration:
Destructor TMemoryStream.Done; Virtual;
Description:
Done releases the memory blocks used by the stream, and then cleans up the memory used by the stream object itself.
Errors:
None.
See also:
Init (589)

For an example, see e.g TStream.CopyFrom (574).

TMemoryStream.Truncate

Declaration:
Procedure TMemoryStream.Truncate; Virtual;
Description:
Truncate sets the size of the memory stream equal to the current position. It de-allocates any memory-blocks that are no longer needed, so that the new size of the stream is the current position in the stream, rounded up to the first multiple of the stream blocksize.
Errors:
If an error occurs during memory de-allocation, the stream’s status is set to stError
See also:
TStream.Truncate (571)

Listing: objectex/ex20.pp


Program ex20;

{ Program to demonstrate the TMemoryStream.Truncate method }

Uses Objects;

Var L : String;
    P : PString;
    S : PMemoryStream;
    I,InitMem : Longint;

begin
  initMem:=Memavail;
  L:='Some constant string';
  { Buffer size of 100 }
  S:=New(PMemoryStream,Init(1000,100));
  Writeln ('Free memory : ',Memavail);
  Writeln ('Writing 100 times "',L,'" to stream.');
  For I:=1 to 100 do
    S^.WriteStr(@L);
  Writeln ('Finished. Free memory : ',Memavail);
  S^.Seek(100);
  S^.Truncate;
  Writeln ('Truncated at byte 100. Free memory : ',Memavail);
  Dispose (S,Done);
  Writeln ('Finished. Lost ',InitMem-Memavail,' Bytes.');
end.

TMemoryStream.Read

Declaration:
Procedure Read (Var Buf; Count: Sw_Word); Virtual;
Description:
Read reads Count bytes from the stream to Buf. It updates the position of the stream.
Errors:
If there is not enough data available, no data is read, and the stream’s status is set to stReadError.
See also:
TStream.Read, Write (591)

For an example, see TStream.Read (573).

TMemoryStream.Write

Declaration:
Procedure Write (Var Buf; Count: Sw_Word); Virtual;
Description:
Write copies Count bytes from Buf to the stream. It updates the position of the stream.

If not enough memory is available to hold the extra Count bytes, then the stream will try to expand, by allocating as much blocks with size BlkSize (as specified in the constuctor call Init (589)) as needed.

Errors:
If the stream cannot allocate more memory, then the status is set to stWriteError
See also:
TStream.Write (574), Read (590)

For an example, see TStream.Read (573).