System.Classes.TStream.Size

From Appmethod Libraries
Jump to: navigation, search

Object Pascal

property Size: Int64 read GetSize write SetSize64;

C++

__property __int64 Size = {read=GetSize, write=SetSize64};

Properties

Type Visibility Source Unit Parent
property public
System.Classes.pas
System.Classes.hpp
System.Classes TStream

Description

Indicates the size in bytes of the stream.

Use Size to find the size of the stream. Size is used internally in routines that read and write to and from the stream. Setting the Size property of TStream does nothing. However, descendants of TStream can override this property to allow applications to change the size of the resource accessed using the stream.

The Size property typically indicates the size of the stream in bytes. But a descendent of TStream can use -1 to indicate an unknown size. When the size is unknown, use the return value from TStream.Read to determine end of the stream.

Example of Stream Size using DataSnap and DBX

When you are reading a DataSnap stream in blocks, the actual TStream descendant that is used to read the stream is TDBXStreamReaderStream. TDBXStreamReaderStream overrides methods of TStream such as the method System.Classes.TStream.GetSize, which is the reader for the property Size.

DBX connections support reading streams in block. The default block size is 32K. Streams can be passed from the client to the server, or from the server to the client. When the receiver is reading the stream (whether the receiver is the client or the server), the stream is passed from the sender to the receiver in blocks. When the stream is large enough to require multiple block readings, the Size property of the receiver's stream will be -1, which indicates that the size of the stream is unknown. As the receiver reads the stream, DataSnap makes a request to the sender for the next block, as needed. To read the stream, the client calls the Read() method until the return value is less than the value requested.

The following code shows how to read a stream to the end, by checking the return value from Read():

var
  LBuffer: TArray<Byte>;
  LReadCount: Integer;
begin
  SetLength(LBuffer, 1024*10); // 10K buffer for this example.  The buffer size can be larger or smaller.
  repeat
    LReadCount := AStream.Read(LBuffer[0], Length(LBuffer));
    if LReadCount > 0 then
    begin
      // Process buffer
    end;
  until LReadCount < Length(LBuffer);

See Also

Code Examples