Skip to content

TBufferedFileStream

Implements a buffered file stream with a multi-page buffer.

Declaration

Source position: bufstream.pp line 88

type

  TBufferedFileStream = class(TFileStream)
  public
    constructor Create(const AFileName: string; Mode: Word);
    constructor Create(const AFileName: string; Mode: Word; 
                  Rights: Cardinal);
    destructor Destroy;
    function Seek(Offset: LongInt; Origin: Word) : LongInt; overload;
    function Seek(const Offset: Int64; Origin: TSeekOrigin) : Int64; overload;
    function Read : LongInt;
    function Write : LongInt;
    procedure Flush;
    procedure InitializeCache;
  end;

Description

TBufferedFileStream is a TFileStream descendant which implements a buffered file stream. It provides a buffer with multiple pages used for random read / write access in the file stream.

By default, It uses a fixed-size buffer consisting of 8 pages with a 4,096 bytes per page. Both page count and page size configurable using the InitializeCache method in the class. The buffer is automatically maintained when the stream size or position is changed, and when reading or writing content to/from the stream.

Pages which have been modified in the buffer are written to the file stream as needed (all pages are used and a read/write operation is performed), when the Flush method is called, and when the class instance is freed. Thus, the class will never use more than the total size obtained by multiplying the CacheBlockSize and aCacheBlockCount arguments to InitializeCache.

This class is suitable when you need to do a lot of random access to a file: hence the use of different pages of configurable size. If you simply wish to speed up sequential reads you better use TReadBufStream or TWriteBufStream for speeding up sequential writes.

Methods

Member Visibility Description
Create public Constructor for the class instance.
Destroy public Destructor for the class instance.
Flush public Flushes modified pages in the buffer to the file stream.
InitializeCache public Re-initializes the internal buffer for the buffered file stream.
Read public Reads the specified number of bytes into the Buffer parameter.
Seek public Moves the position in the buffer relative to the specified origin.
Write public Writes the specified number of bytes in Buffer to the internal page buffer(s).
See all Methods

See also

Name Description
TFileStream
THandleStream
TStream
TReadBufStream Read-only buffered stream.
TWriteBufStream Write-only buffered stream.