views:

76

answers:

3

Lets assume we have a simple internet socket, and its going to send 10 megabytes (because i want to ignore memory issues) of random data through. Is there any performance difference or a best practice method that one should use for receiving data? The final output data should be represented by a byte[]. Yes i know writing an arbitrary amount of data to memory is bad, and if I was downloading a large file i wouldn't be doing it like this. But for argument sake lets ignore that and assume its a smallish amount of data. I also realise that the bottleneck here is probably not the memory management but rather the socket receiving. I just want to know what would be the most efficient method of receiving data.

A few dodgy ways can think of is:

  1. Have a List and a buffer, after the buffer is full, add it to the list and at the end list.ToArray() to get the byte[]

  2. Write the buffer to a memory stream, after its complete construct a byte[] of the stream.Length and read it all into it in order to get the byte[] output.

Is there a more efficient/better way of doing this?

+1  A: 

Just write to a MemoryStream and then call ToArray - that does the business of constructing an appropriately-sized byte array for you. That's effectively what a List<byte> would be like anyway, but using a MemoryStream will be a lot simpler.

Jon Skeet
Yeah this does seem like the most simple and yet good answer thanks!
Daniel
A: 

You could write to a memory stream, then use a streamreader or something like that to get the data. What are you doing with the data? I ask because would be more efficient from a memory standpoint to write the incoming data to a file or database table as the data is being received rather than storing the entire contents in memory.

Jeremy
Probably converting it to a string
Daniel
A: 

What is the encoding of your data? is it plain ASCII, or is it something else, like UTF-8/Unicode?

if it is plain ASCII, you could just allocate a StringBuilder() of the required size (get the size from the ContentLength header of the response) and keep on appending your data to the builder, after converting it into a string using Encoding.ASCII.

If it is Unicode/UTF8 then you have an issue - you cannot just call Encoding..GetString(buffer, 0, bytesRead) on the bytes read, because the bytesRead might not constitute a logical string fragment in that encoding. For this case you will need to buffer the entire entity body into memory(or file), then read that file and decode it using the encoding.

feroze
Its just ASCII yeah and that is a very good idea!!! Why will other encodings need the full amount before it can convert properly?
Daniel
Oh because if they are using 2 bytes per character then if you receive 7bytes and try to convert it, problems will occur. But you could just get the 0-6 and buffer the 1 couldn't you? Bit more work but less memory pressure
Daniel