Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need loop to process "chunks" from byte array

Status
Not open for further replies.

jjamjatra

Programmer
Aug 7, 2001
2
US
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 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 byte 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 = incomingArray
i++;
}
uploadObject.Size = BytesRead;
uploadObject.MTOMPayload = outboundBuffer;

// call web service here and pass the uploadObject

// get next "chunk" until incomingArray is fully processed
}

Could one of you experts take this idea and show me the proper way to loop? I've read that System.Array class may have methods that are preferable for performance but I was still lost about the looping.

Thanks very much in advance.
 
Here is a shot at the pseudocode
Code:
Loop through incoming array
  add to BytesRead
  Add to array of bytes to get sent out
  if BytesRead is equal to chunkSize
    then call the webservice and send that chunk
    also set the BytesRead back to 0 and clear the outbound buffer.
End Loop
Send out leftover bytes and close the connection
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top