Objective-C:从 URL 读取特定的缓冲区大小

发布于 2024-10-13 06:10:33 字数 455 浏览 2 评论 0原文

我需要从 URL 异步下载大文件,并在处理数据之前将缓冲区(例如 4096 字节)填充到特定大小。使用诸如 NSURLConnection 之类的内置对象会很棒,但我遇到了一些问题。

使用 NSURLConnectiondidReceiveData: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2024-10-20 06:10:33

使用 NSURLConnection 你必须将数据读入你自己的缓冲区(例如 NSData 对象)。那么为什么不每 4096 个字节就从该缓冲区读取一次呢?

听起来您只对数据到达时的处理感兴趣,而不是保留所有数据。因此,也许您可​​以在收到数据(伪代码)时执行类似的操作:

while buffer.size > 4096:
  copy first 4096 bytes into separate buffer
  process separate buffer
  delete first 4096 bytes from buffer

然后在连接关闭时处理剩下的内容(如果需要)。

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):

while buffer.size > 4096:
  copy first 4096 bytes into separate buffer
  process separate buffer
  delete first 4096 bytes from buffer

And then process whatever is left (if necessary) when the connection closes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文