Re: Streams



Maarten Wiltink wrote:
You're probably being bitten by the stream reallocating the string it
uses for storage every time you append to it. A quick glance at the
code seems to suggest that there's no easy way to prevent that. Every
Write() truncates the contents so you can't pre-allocate the way you
can with TStringList. You'd have to write your own derived class that
doesn't truncate; it should be very little work.

A search would probably turn up a ready-made TStringBuilder class for Delphi, inspired by .Net's StringBuilder class. It's designed especially for constructing strings incrementally without excessive memory re-allocations.


Nonetheless, J French had the right idea: send the data directly to the compression stream and skip the intermediate storage -- the array copy, the string list, etc. The trick is finding a compression stream that supports that. The free JCL has such a class, TJclZLibCompressStream.

var
  FileStream, CompressionStream: TStream;
begin
  FileStream := TFileStream.Create(...);
  try
    CompressionStream := TJclZLibCompressStream.Create(FileStream);
    try
      // Write data directly to this stream. It will be compressed and
      // and piped through to the output file.
    finally
      CompressionStream.Free;
    end;
  finally
    FileStream.Free;
  end;
end;

--
Rob
.