Search code examples
delphiindytfilestream

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


Solution

  • Since this is trying to create a download manager that can stop and resume downloads, the idea here is that when you resume, it wants to step back a little bit and re-request some of the data that was previously sent just in case the disconnect was caused by an error that caused the data received to be corrupted. Most download managers that I've seen will step back by at least 4 KB; looks like this one is only doing 1 KB.

    If you put fs.Size alone in the placeholder then it wouldn't step back at all, which could leave you open to the possibility of corrupt data.

    And replacing TFileStream with TMemoryStream would mean that you're downloading to RAM instead of to disc, and if the computer crashes or loses power or your app crashes somehow, all the progress is lost. So that's not a good idea. Also, downloading to RAM limits the size of your download to the available size of your address space, which would make downloading large files (ISOs of DVDs, for example) either impossible or at least much more difficult than it needs to be.