我想将诸如 HTTPClientResponse.bodyReader (类型InterfaceProxy!InputStream
)之类的 vibe.d 流以及其他潜在的 vibe.d 流保存到文件中,如何在不将所有数据复制到 RAM 的情况下以内存有效的方式最好地做到这一点?
1 回答
0
通常,对于使用 HTTP 客户端下载文件,您可以使用vibe.inet.urltransfer包,它提供了一个download
方便的功能,可以执行 HTTP 请求、处理重定向并将最终输出存储到文件中。
download(url, file);
但是,如果您想获取原始输入流(例如,当不处理重定向时),您可以使用vibe.core.file : openFile打开/创建文件作为文件流,然后写入该文件。
然后写入文件流,您有两个选项:
- 要么你直接打电话
file.write(otherStream)
- 否则,您可以使用vibe.core.stream :管道
直接调用对象是 vibe.d urltransfer 模块中使用的内容,也建议用于文件,因为它将直接从流中读取到写入缓冲区,而不是使用额外的临时write
缓冲区。FileStream
pipe
样本:
// createTrunc creates a file if it doesn't exist and clears it if it does exist
// You might want to use readWrite or append instead.
auto fil = openFile(filename, FileMode.createTrunc);
scope(exit) fil.close();
fil.write(inputStream);
于 2020-09-08T11:49:57.017 回答