Objective-C:从 URL 读取特定的缓冲区大小
我需要从 URL 异步下载大文件,并在处理数据之前将缓冲区(例如 4096 字节)填充到特定大小。使用诸如 NSURLConnection
之类的内置对象会很棒,但我遇到了一些问题。
使用 NSURLConnection
,didReceiveData:data
消息自行决定何时触发。例如,我可能第一次收到 954 字节,随后收到 1048 字节,直到下载完成。无论如何,我可以修改 NSURLConnection 以强制它在触发接收消息之前读取我需要的缓冲区大小吗?
我还研究过使用 CFNetwork 类,但这似乎更困难,并且希望尽可能避免它。
另外,我也许可以通过使用众所周知的 AsyncSocket
类来完成此任务,但随后我必须解析 HTTP 标头,而且我感觉与其他 Web 服务器相比会出现更多问题配置。
先感谢您。
I need to download large files from a URL asynchronously and fill a buffer (for example 4096 bytes) to a specific size before processing the data. It would be great to use a built-in object such as NSURLConnection
but I am having some problems.
With NSURLConnection
, the didReceiveData:data
message decides when to fire on its own. For example, I might receive 954 bytes the first time and subsequently 1048 bytes until it's done downloading. Is there anyway I can modify NSURLConnection
to force it to read to the buffer size I need before firing the receive message?
I also looked into using the CFNetwork classes, but this seems alot more difficult and would like to avoid it if possible.
Additionally I might be able to accomplish this by using the well known AsyncSocket
class, but then I'd have to parse HTTP headers and I have a feeling more issues would come up down the line with other web server configurations.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 NSURLConnection 你必须将数据读入你自己的缓冲区(例如 NSData 对象)。那么为什么不每 4096 个字节就从该缓冲区读取一次呢?
听起来您只对数据到达时的处理感兴趣,而不是保留所有数据。因此,也许您可以在收到数据(伪代码)时执行类似的操作:
然后在连接关闭时处理剩下的内容(如果需要)。
With NSURLConnection you have to read the data into your own buffer anyway (e.g. an NSData object). So why not just read from that buffer every 4096 bytes?
It sounds like you're only interested in processing the data as it arrives, and not in keeping all of it. So perhaps you could do something like this whenever you receive data (pseudocode):
And then process whatever is left (if necessary) when the connection closes.