Why use buffers to read/write Streams

Posted by James Hay on Stack Overflow See other posts from Stack Overflow or by James Hay
Published on 2010-05-12T11:55:57Z Indexed on 2010/05/12 18:34 UTC
Read the original article Hit count: 199

Filed under:
|
|

Following reading various questions on reading and writing Streams, all the various answers define something like this as the correct way to do it:

private void CopyStream(Stream input, Stream output)
{
   byte[] buffer = new byte[16 * 1024];
   int read;
   while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
   {
      output.Write(buffer, 0, read);
   } 
}

Two questions:

Why read and write in these smaller chunks?

What is the significance of the buffer size used?

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about streams