这是从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;我不明白究竟什么是抵消,为什么Max(0,Fs.Size-1024)在它的占位符中,如果你去下面(在代码中)
// alternatively: // Fs.Seek(-1024, soFromEnd);到底是什么'-1024'...为什么总是使用1024 / -1024? 并且会在偏移的步伐工作中单独工作(我正在尝试使用暂停恢复支持来进行下载管理),并且在上面的代码中用tmemmorystream替换tfilestream对程序有任何影响?
如果它很重要:我使用d2007和d2010
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
最满意答案
因为这是试图创建一个下载管理器,可以停止和恢复下载,这里的想法是,当你恢复时,它想要退后一点,并重新请求一些以前发送的数据,以防万一断开连接是由导致收到的数据损坏的错误引起的。 我见过的大多数下载管理者将退后至少4 KB; 看起来这个只做1 KB。
如果你将fs.Size单独放在占位符中,那么它根本不会退回,这可能会让你对数据损坏的可能性开放。
并且用TMemoryStream替换TFileStream意味着你正在下载到RAM而不是光盘,并且如果计算机崩溃或失去电源或者你的应用程序崩溃,所有的进度都会丢失。 所以这不是一个好主意。 此外,下载到内存将下载的大小限制为可用的地址空间大小,这会使下载大文件(例如DVD的ISO)变得不可能,或者至少比需要的难得多。
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.
更多推荐
发布评论