【发布时间】:2017-09-29 21:54:07
【问题描述】:
尝试将 Stream 对象转换为 byte[] 并使用以下方法:
public static byte[] ReadFully(System.IO.Stream input)
{
byte[] buffer = new byte[16*1024];
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
但是,输入参数“input”用于 2 GB 的大文件,因此代码不会进入 while 循环,因此不会将其转换为字节数组。
对于较小的文件,它工作正常
【问题讨论】:
-
我怀疑您将很难创建一个包含超过 2^31 个元素的数组。为什么需要这样做?
-
尝试使用 sharepoint 2010 - 客户端对象模型下载大文件。我们有2GB的文件。所以要下载它,我们使用了以下代码:FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, "thisFile"); System.IO.Stream 流 = fileInfo.Stream;现在我们的项目要求是我们要返回 byte[] 而不是 Stream 对象,因此尝试将 stream 转换为 byte[]
-
老实说,“项目要求”听起来很啰嗦。你检查过这么大的字节数组是否可行?
-
是的,验证了字节数组在 2 gb 之前是可行的
-
但是“直到”2GB 已经很好了......超过 2GB 怎么样?您是否尝试过创建 3GB 字节数组?