Streaming

  1. Streaming of object data[NEW]

Streaming of object data[NEW]

From: infocus@swbell.net (Mark Shapiro)

I've been using TFileStream and TMemoryStream to read and write large quantities of record object data to and from disk. Here's an example of streaming to disk the contents of a container called DataArray that contains several thousand record objects:


  DataStream := TFileStream.Create('vccmem.dat', fmCreate);
  MakeDate := FormatDateTime('mmmm d, yyyy',date);
  DataStream.Write(MakeDate, SizeOf(MakeDate));
  RCount := DataArray.Size;
  DataStream.Write(RCount,SizeOf(RCount));

  for q := 1 to DataArray.Size do
  begin
    Application.ProcessMessages;
    ARecord := DataArray[q] as TNameRecord;
    DataStream.Write(ARecord.id, SizeOf(ARecord.id));
    DataStream.Write(ARecord.first_name, SizeOf(ARecord.first_name));
    DataStream.Write(ARecord.last_name, SizeOf(ARecord.last_name));
    DataStream.Write(ARecord.title, SizeOf(ARecord.title));
    DataStream.Write(ARecord.address, SizeOf(ARecord.address));
    DataStream.Write(ARecord.city, SizeOf(ARecord.city));
    DataStream.Write(ARecord.state, SizeOf(ARecord.state));
    DataStream.Write(ARecord.zip, SizeOf(ARecord.zip));
    DataStream.Write(ARecord.phone, SizeOf(ARecord.phone));
    DataStream.Write(ARecord.fax, SizeOf(ARecord.fax));
    DataStream.Write(ARecord.e_mail, SizeOf(ARecord.e_mail));
  end;
  DataStream.Destroy;

Here is the code for streaming that data back into memory on program start-up and placing the data in a container called NameArray:


  DataStream := TMemoryStream.Create;
  DataStream.LoadFromFile('vccmem.dat');
  DataStream.Read(MakeDate, SizeOf(MakeDate));
  DataStream.Read(RCount, SizeOf(RCount));

  for q := 1 to RCount do
  begin
    NameRecord := TNameRecord.Create;
    DataStream.Read(NameRecord.id, SizeOf(NameRecord.id));
    DataStream.Read(NameRecord.first_name, 
      SizeOf(NameRecord.first_name));
    DataStream.Read(NameRecord.last_name, 
      SizeOf(NameRecord.last_name));
    DataStream.Read(NameRecord.title, SizeOf(NameRecord.title));
    DataStream.Read(NameRecord.address, SizeOf(NameRecord.address));
    DataStream.Read(NameRecord.city, SizeOf(NameRecord.city));
    DataStream.Read(NameRecord.state, SizeOf(NameRecord.state));
    DataStream.Read(NameRecord.zip, SizeOf(NameRecord.zip));
    DataStream.Read(NameRecord.phone, SizeOf(NameRecord.phone));
    DataStream.Read(NameRecord.fax, SizeOf(NameRecord.fax));
    DataStream.Read(NameRecord.e_mail, SizeOf(NameRecord.e_mail));
    NameArray.Insert(NameRecord);
  end;


Please email me and tell me if you liked this page.