0

我想在一个为 STDIN 提供 NSInputStream 的类上提供一个访问器,它可能是几百兆字节(或千兆字节,尽管可能不太可能)的数据。

当调用者得到这个 NSInputStream 时,它应该能够从中读取,而不必担心耗尽它包含的数据。换句话说,另一个代码块可能会请求 NSInputStream 并期望能够从中读取。

如果没有首先将所有数据复制到 NSData 对象中(我假设)会导致内存耗尽,那么我有什么选择来处理这个问题?返回的 NSInputStream 不必是同一个实例,它只需要提供相同的数据。

我现在能想到的最好的方法是将 STDIN 复制到一个临时文件,然后使用该文件返回 NSInputStream 实例。这几乎是处理它的唯一方法吗?如果我走临时文件路线,有什么需要注意的吗?

编辑 | 我应该提一下,它实际上不是STDIN,这是在多线程 FastCGI 应用程序中,它FCGX_Request.in是来自 STDIN 的流。

4

1 回答 1

2

When reading data from a pipe or socket, you have three options:

  • Process it and forget it.
  • Add it to a complete record in memory and process it before or after doing so.
  • Add it to a complete file and process it before or after doing so.

That's the complete list. There's nowhere else to record it but short-term or long-term storage, so the only other thing you can do with data you read is to not record it at all.

The only other way to get the data again is for whatever sent it to you to send it again.

于 2010-05-29T05:51:12.240 回答