Tfilestream.seek and confusion bias

this is a piece of code 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 don’t understand what exactly is disabled and why Max (0, Fs.Size-1024) in its placeholder and if you go below (in the code)

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

what is "-1024" ... why always use 1024 / -1024 ?? and will fs.size be alone in working with the tone moderator (I go on to do boot control with resume pause support) and replacing tfilestream with tmemmystream in the code above, any effect on the program?

if that matters: i use d2007 and d2010

+4
source share
2 answers

Since this is trying to create a download manager that can stop and resume the download, the idea here is that when you resume, it wants to step back a bit and re-request some data that was sent earlier in the event of a shutdown due to an error that corrupted the received data. Most download managers that I have seen will fall back by at least 4 KB; it looks like it does only 1 kb.

If you put fs.Size only in the placeholder, then it will not back down at all, which may leave you open to the possibility of corrupted data.

And replacing TFileStream with TMemoryStream means that you are loading into RAM instead of a disk, and if the computer crashes or loses power, or something due to an application failure, all results will be lost. So this is not a good idea. In addition, loading into RAM limits the size of your download to the available size of your address space, which makes downloading large files (such as ISO DVDs) impossible or at least much more difficult than necessary.

+4
source

It looks like you are looking for 1024 from the end of the file (or 0 if the file is not yet so large). All this is due to the resumption of transmission. You think the end of your file is corrupt. Trim the shit (or start at 0) so as not to add good data after a failure.

Analogy: you are building an ice castle. It gets dark and you freeze rain in one night. The next day you will receive your chainsaw and sawed to the bottom of the cheese, exposing good clean ice. Now you start building from there.

+6
source

Source: https://habr.com/ru/post/1313385/


All Articles