Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tfilestream.seek and offset confusion

this is a code snippet taken from https://forums.embarcadero.com/message.jspa?messageID=219481

if FileExists(dstFile) then
begin
  Fs := TFileStream.Create(dstFile, fmOpenReadWrite);
  try
    Fs.Seek(Max(0, Fs.Size-1024), soFromBeginning);
    // alternatively:
    // Fs.Seek(-1024, soFromEnd);
    Http.Request.Range := IntToStr(Fs.Position) + '-';
    Http.Get(Url, Fs);
  finally
    Fs.Free;
  end;
end;

i do not understand what exactly is off-set and why Max(0,Fs.Size-1024) in its placeholder and if you go below(in code)

// alternatively:
    // Fs.Seek(-1024, soFromEnd);

what exactly is '-1024'...why do the always use 1024/-1024?? and would fs.size alone in the offset paceholder work( i am tring to make a download manage with pause resume support) and would replacing tfilestream with tmemmorystream in code above have any effect on program?

if it matters :i use d2007 and d2010

like image 604
Omair Iqbal Avatar asked Dec 28 '22 13:12

Omair Iqbal


1 Answers

Looks like you're seeking to 1024 from the end of the file (or 0, if the file isn't yet that big). It's all got to do with resuming a transmission. you figure that the end of your file is corrupt. Trim off the crap (or start over from 0) so you aren't adding good data after bad.

Analogy: you're building an ice castle. It gets dark and you get freezing rain overnight. The next day, you get your chainsaw and saw off an inch of crud, exposing the good clean ice. Now you start building from there.

like image 166
Chris Thornton Avatar answered Dec 31 '22 14:12

Chris Thornton