big streams with DataSnap

别来无恙 提交于 2019-11-30 22:51:26

Actually, i think i`ve got it. I'm posting this as an answer maybe somebody else need this.

procedure TfrmMain.butStreamClick(Sender: TObject);
const
  iBufSize = 128;
var
  sStr : TStream;
  sMem : TMemoryStream;
  buf: PByte;
  iRead: integer;
begin
  cycleConnection;

  with TsrvMethodsClient.Create( SQLConn.DBXConnection, False ) do begin

    sStr := getStream( 500000 ); //500k stream

    GetMem(buf, iBufSize);
    sMem := TMemoryStream.Create;
    try
      repeat
        iRead := sStr.Read( Pointer(buf)^, iBufSize);

        if iRead > 0 then sMem.WriteBuffer( Pointer(buf)^, iRead);
        if iRead < iBufSize then break;
      until iRead < iBufSize;
    finally
      FreeMem(buf, iBufSize);
    end;

    Free;
  end;
  FreeAndNil(sStr);
  FreeAndNil(sMem);
end;

P.S.

Searching through DataSnap code samples i`ve found that one (speed related) improvement would be to have iBufSize set to 61440 (or equivalent hex value $F000) which seems to be the biggest size can be received in one go. If receiving stream is bigger then reported size will be -1 and the code above is needed to read the entire stream.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!