Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need loop to copy chunks from byte array

Tags:

c#

bytearray

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.

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).

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:

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.

like image 222
John Adams Avatar asked Feb 24 '09 22:02

John Adams


2 Answers

You might want to look into Array.Copy or Buffer.BlockCopy; this will clean things up a bit, since you won't have to copy all of the bytes individually:

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
 }
like image 90
Daniel LeCheminant Avatar answered Nov 08 '22 11:11

Daniel LeCheminant


You probably want Buffer.BlockCopy (the rawest of the copies; ideally suitable for 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):

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();
like image 27
Marc Gravell Avatar answered Nov 08 '22 12:11

Marc Gravell