Difference between revisions of "TBufStream"

From Lazarus wiki
Jump to navigationJump to search
(TBufFileStream)
Line 20: Line 20:
  
 
In use, the WBufStream will accept and store strings from the StringList, one by one, until its buffer is full (default 16k), it then uses the FileStream to write that content to disk, emptying its own buffer and starts again.
 
In use, the WBufStream will accept and store strings from the StringList, one by one, until its buffer is full (default 16k), it then uses the FileStream to write that content to disk, emptying its own buffer and starts again.
 +
 +
In the same unit is '''TBufferedFileStream''' that appears to be even more suited to our need here -
 +
<syntaxhighlight lang="pascal">var
 +
  BufFileStream: TBufferedFileStream;
 +
 +
begin
 +
  ....
 +
  BufFileStream := TBufferedFileStream.Create('somefile.text', fmCreate);
 +
  StringList.SaveToStream(BufFileStream);
 +
  BufFileStream.Free;
 +
</syntaxhighlight>
 +
  
 
==See Also==
 
==See Also==
Line 25: Line 37:
 
* https://www.freepascal.org/docs-html/current/fcl/bufstream/treadbufstream.html
 
* https://www.freepascal.org/docs-html/current/fcl/bufstream/treadbufstream.html
 
* https://www.freepascal.org/docs-html/current/fcl/bufstream/tbufstream.html
 
* https://www.freepascal.org/docs-html/current/fcl/bufstream/tbufstream.html
 +
* https://www.freepascal.org/docs-html/current/fcl/bufstream/tbufferedfilestream.html
 
* [[TFileStream]]
 
* [[TFileStream]]

Revision as of 14:42, 2 July 2021

TBufStream

TBufStream is the common ancestor for the TReadBufStream and TWriteBufStream streams. It completely handles the buffer memory management and position management. An instance of TBufStream should never be created directly. It also keeps the instance of the source stream.

TReadBufStream and TWriteBufStream can be used to speed up (or reduce memory consumption) of I/O processes where your application is reading or writing small 'lumps' of data and the underlying operating system is far more efficient handling data in sizable 'lumps'.

And example is presented below where a TStringList contained a large number of strings totaling about 100,000 characters was to be saved to disk. Using TWriteBufStream (instead of StringList.SaveToFile) reduced to time required by a factor of four. In this example, the StringList already contains it's data and for clarity try/finally structures have not been used. Your production code should differ ...

var
  WBufStream: TWriteBufStream;
  FileStream: TFileStream; 
begin
  ....
  FileStream := TFileStream.Create('somefile.text', fmCreate);
  WBufStream := TWriteBufStream.Create(FileStream);
  StringList.SaveToStream(WBufStream);
  WBufStream.Free;
  FileStream.Free;

In use, the WBufStream will accept and store strings from the StringList, one by one, until its buffer is full (default 16k), it then uses the FileStream to write that content to disk, emptying its own buffer and starts again.

In the same unit is TBufferedFileStream that appears to be even more suited to our need here -

var
  BufFileStream: TBufferedFileStream;

begin
  ....
  BufFileStream := TBufferedFileStream.Create('somefile.text', fmCreate);
  StringList.SaveToStream(BufFileStream);
  BufFileStream.Free;


See Also