C# 需要循环从字节数组复制块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/583970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need loop to copy chunks from byte array
提问by John Adams
I have to process a large byte array that is passed to my function. I need to copy the content from this incoming byte array in smaller "chunks" to an outbound byte array.
我必须处理传递给我的函数的大字节数组。我需要将这个传入字节数组的内容以较小的“块”复制到出站字节数组。
For every "chunk" of data created in the outbound array, I need to call a web service.
对于在出站数组中创建的每个“块”数据,我需要调用一个 Web 服务。
Upon return, I need to resume looping through the incoming byte array, continuing to pass a whole or partial chunk of data until the complete incoming array is processed (i.e. sent to the web service in chunks).
返回时,我需要继续循环传入的字节数组,继续传递整个或部分数据块,直到处理完完整的传入数组(即以块的形式发送到 Web 服务)。
I am very new to C# and I am struggling with a loop that works. I know how to call the web service to handle a "chunk" but I can't get the looping correct. Here is a sketch of the pathetic mess I currently have:
我对 C# 很陌生,我正在为一个有效的循环而苦苦挣扎。我知道如何调用 Web 服务来处理“块”,但我无法正确循环。这是我目前所拥有的可悲混乱的草图:
int chunkSize = 10000;
byte[] outboundBuffer = new byte[chunkSize];
while (BytesRead > 0)
{
long i = 0;
foreach (byte x in incomingArray)
{
BytesRead += 1;
outboundBuffer[i] = incomingArray[i]
i++;
}
uploadObject.Size = BytesRead;
uploadObject.MTOMPayload = outboundBuffer;
// call web service here and pass the uploadObject
// get next "chunk" until incomingArray is fully processed
}
I know this is a mess and won't work; could someone sketch a proper loop to get this done? Thanks very much.
我知道这是一团糟,行不通;有人可以绘制一个适当的循环来完成这项工作吗?非常感谢。
采纳答案by Daniel LeCheminant
You might want to look into Array.Copyor Buffer.BlockCopy; this will clean things up a bit, since you won't have to copy all of the bytes individually:
您可能想查看Array.Copy或Buffer.BlockCopy;这将清理一下,因为您不必单独复制所有字节:
int incomingOffset = 0;
while(incomingOffset < incomingArray.Length)
{
int length =
Math.Min(outboundBuffer.Length, incomingArray.Length - incomingOffset);
// Changed from Array.Copy as per Marc's suggestion
Buffer.BlockCopy(incomingArray, incomingOffset,
outboundBuffer, 0,
length);
incomingOffset += length;
// Transmit outbound buffer
}
回答by Albert
You seem to have broken down your task logically, after all, you've coherently described it with words. Now just make your code do it.
你似乎已经逻辑地分解了你的任务,毕竟你已经用文字连贯地描述了它。现在只需让您的代码做到这一点。
Pseudo code might be something like:
伪代码可能类似于:
while (there are chunks in incoming array)
copy 1 chunk to outbound array
create uploadObject
call webservice
endwhile
回答by Marc Gravell
You probably want Buffer.BlockCopy
(the rawest of the copies; ideally suitable for byte[]
).
您可能想要Buffer.BlockCopy
(最原始的副本;非常适合byte[]
)。
Of course, the other option is to use a MemoryStream
in place of the outbound array, and just Write
to it each time, then call ToArray()
or GetBuffer()
on the MemoryStream
(with GetBuffer()
, you need to watch the length; with ToArray()
it is trimmed for you automatically):
当然,另一种选择是使用 aMemoryStream
代替出站数组,并且Write
每次只使用它,然后调用ToArray()
or GetBuffer()
on MemoryStream
(使用GetBuffer()
,您需要注意长度;使用ToArray()
它会自动为您修剪):
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[BUFFER_SIZE];
int bytesReceived;
while((bytesReceived = GetNextChunk(buffer, 0, BUFFER_SIZE)) > 0) {
ms.Write(incomingArray, 0, bytesReceived);
}
byte[] final = ms.ToArray();
回答by Chetan Sastry
Be wary of calling web-services in a loop synchronously. Synchronous web-service calls take indefinite time due to the nature of HTTP and your loop might run for a long time. It is preferable to use an asynchronous approach.
小心在循环中同步调用 Web 服务。由于 HTTP 的性质,同步 Web 服务调用需要无限时间,并且您的循环可能会运行很长时间。最好使用异步方法。
回答by Gautam
Why not just use Array.Copy? http://msdn.microsoft.com/en-us/library/system.array.copy.aspx
为什么不直接使用 Array.Copy? http://msdn.microsoft.com/en-us/library/system.array.copy.aspx
eg.
例如。
Array.Copy(srcBuffer, destBuffer, 1024);