.net - Stream reading variations in C#? -


I have seen - reads from one stream in a buffer:

  byte [] Buffer = new byte [1024 * 32]; Int bytes read; While looking at the second parameter of ({bytesread = stream.read (buffer, 0, buffer lang)), {// ...}  

Stream Read (.., 0, ..) which is .

Is this offset every time 0? Who said that he does not overwrite the data on those indexes?

Contrast: I also saw:

  int read, offset = 0; While calculating (calculation & gt; 0 & amp; amp; (read = source. Read (buffer, offset, count)) gt; 0) {offset + = reading; Calculation - = reading; }  

Then offset is offset after each reading (which seems more logical for me)

But I should be missing something:

Was my observation correct? When should I use every case?

NB, my POV network stream is: sending the file. Both code samples are useful, just select your first example to add some code in different scenarios.

  socket socket = ...; Byte [] buffer = new byte [1024 * 32]; Int bytes read; While ((BytesRed = stream.read (buffer, 0, buffer, Lang)) gt; 0) {socket.BeginSend (buffer, 0, bytes read, socketflags, any, empty, empty); // Wait for something else to be completed? }  

In this case buffer is reused Yes, the data will be overwritten every time and this is the same behavior because You share it on time, you use it and you walk.

Your second example is quite different: you are filling out a buffer with the data you read but you do not read all the data all together, every time you offset Increase in target buffer then read a small part.

How are these different? In the first case, the smaller the buffer you want, the smaller it may be (ideally by a byte too) will consume many reading input stream. In the second case buffer should be large enough to accommodate all the data you need.

  // Note that we need to know the size of the file in advance and the buffer will be enough / int to read all of the data we read, offset = 0; While reading (calculation & gt; 0 & amp; amp; (read = source. Read (buffer, offset, counting)); gt; socket.BeginSend (buffer, offset, read, socketflags, any, empty, zero ); // Here we do not need to complete the startend () offset + = reading; calculation - = reading;}  

Which is better? It is hard to say, if you have a All the pieces in the shot are allocated, so seldom you can increase your share and read offset (in case only I can think that There is performance optimization due to the block size on the stream stream or block - you want to read new while starting some processing in parallel - received data) There are at least two major drawbacks:

  • You First should know the size of the file in advance (and it is not always true);
  • If the file is large, then you will run Out of memory .

Normally (IMO) if the first method (reuse only one Fur is very good in most situations, Performance Benefits may be that you have negligible from single networks (sending non-intercept) in the highest network scenario in a network And the drawbacks are serious to summarize:

 1 2 The size of the unknown file Yes Yes Can not exit the memory Yes No Parallel processing favorable Yes No display is not indirect 

Of course both of you You can also "mix" in ways: a large multiple small reading circular buffer , you can read each new indicator and read the previous one in parallel ). With this you can benefit from both methods but there is a bit more difficult to tune (overlapping due to concurrent access and possibly reading / writing).


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -