我想要保存一个vibe.d流,如HTTPClientResponse.bodyReader (类型为InterfaceProxy!InputStream),但也保存其他潜在的vibe.d流到一个文件中,我如何才能在不将所有数据复制到内存的情况下,以最有效的内存方式做到这一点?
发布于 2020-09-08 19:49:57
通常,要使用HTTP客户端下载文件,可以使用vibe.inet.urltransfer包,该包提供了一个方便的download函数,该函数可以执行HTTP请求、处理重定向并将最终输出存储到文件中。
download(url, file);但是,如果您想获取原始输入流(例如,在不处理重定向时),您可以使用vibe.core.file : openFile将文件作为文件流打开/创建,然后对其进行写入。
要写入文件流,您有两个选项:
你可以直接调用file.write(otherStream)
,
在FileStream对象上直接调用write是在vibe.durltransfer模块中使用的,也建议用于文件,因为它将直接从流读取到写缓冲区,而不是使用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);https://stackoverflow.com/questions/63793298
复制相似问题