我必须处理一个传递给我的函数的大字节数组.我需要将来自此传入字节数组的内容以较小的"块"复制到出站字节数组.
对于在出站阵列中创建的每个"数据块",我需要调用一个Web服务.
返回时,我需要继续循环传入的字节数组,继续传递整个或部分数据块,直到处理完整的传入数组(即以块为单位发送到Web服务).
我是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
}
Run Code Online (Sandbox Code Playgroud)
我知道这是一团糟,不会奏效; 有人可以草拟一个合适的循环来完成这项工作吗?非常感谢.
Dan*_*ant 19
您可能想要查看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
}
Run Code Online (Sandbox Code Playgroud)