title: Using Stream.Read() vs BinaryReader.Read() to process binary streams
body: <p>When working with binary streams (i.e. <code>byte[]</code> arrays), the main point of using <code>BinaryReader</code> or <code>BinaryWriter</code> seems to be simplified reading/writing of primitive data types from a stream, using methods such as <code>ReadBoolean()</code> and taking encoding into account. Is that the whole story? Is there an inherent advantage or disadvantage if one works directly with a <code>Stream</code>, without using <code>BinaryReader/BinaryWriter</code>? Most methods, such as <code>Read()</code>, seem to be the same in both classes, and my guess is that they work identically underneath. </p>
  
  <p>Consider a simple example of processing a binary file in two different ways (edit: I realize this way is ineffective and that a buffer can be used, it's just a sample):</p>
  
  <pre><code>// Using FileStream directly
  using (FileStream stream = new FileStream("file.dat", FileMode.Open))
  {
      // Read bytes from stream and interpret them as ints
      int value = 0;
      while ((value = stream.ReadByte()) != -1)
      {
          Console.WriteLine(value);
      }
  }
  
  
  // Using BinaryReader
  using (BinaryReader reader = new BinaryReader(FileStream fs = new FileStream("file.dat", FileMode.Open)))
  {
      // Read bytes and interpret them as ints
      byte value = 0;    
      while (reader.BaseStream.Position &lt; reader.BaseStream.Length)
      {
          value = reader.ReadByte();
          Console.WriteLine(Convert.ToInt32(value));
      }
  }
  </code></pre>
  
  <p>The output will be the same, but what's happening internally (e.g. from OS perspective)? Is it - generally speaking - important which implementation is used? Is there any purpose to using <code>BinaryReader/BinaryWriter</code> if you don't need the extra methods that they provide?
  For this specific case, MSDN says this in regard to <code>Stream.ReadByte()</code>:</p>
  
  <blockquote>
    <p>The default implementation on Stream creates a new single-byte array
    and then calls Read. While this is formally correct, it is
    inefficient.</p>
  </blockquote>
  
  <p>Using <code>GC.GetTotalMemory()</code>, this first approach does seem to allocate 2x as much space as the second one, but AFAIK this shouldn't be the case if a more general <code>Stream.Read()</code> method is used (e.g. for reading in chunks using a buffer). Still, it seems to me that these methods/interfaces could be unified easily...</p>