DataSnap XE2 and TStream method parameters

后端 未结 2 1813
死守一世寂寞
死守一世寂寞 2021-01-14 23:35

I am working on DataSnap project in Delphi XE2 using TCP/IP protocol that needs to pass a stream of binary data to the server as a method parameter. The problem I am running

2条回答
  •  别那么骄傲
    2021-01-14 23:53

    I got thinking about it and it occurred to me that transferring the data to another memory stream just wastes memory, especially if the file is very large. All we need to do is count the bytes and set the stream size, right?!

    procedure FixStream(const AStream: TStream);
    const
      LBufSize = $F000;
    var
      LBuffer: PByte;
      LReadCount, StreamSize: Integer;
    begin
      GetMem(LBuffer, LBufSize);
      try
        StreamSize := 0;
        repeat
          LReadCount := AStream.Read(LBuffer^, LBufSize);
          Inc(StreamSize, LReadCount);
        until LReadCount < LBufSize;
        AStream.Size := StreamSize;
      finally
        FreeMem(LBuffer, LBufSize);
      end;
    end;
    

    Do you want to give that a try? I'm not able to test the code right now or I would...

提交回复
热议问题