title: Setting the offset in a stream
body: <p>It says here msdn.microsoft.com/en-us/library/system.io.stream.read.aspx that the <code>Stream.Read</code> and <code>Stream.Write</code> methods both advance the position/offset in the stream automatically so why is the examples here <a href="http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx</a> and <a href="http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx</a> manually changing the offset?</p>
  
  <p>Do you only set the offset in a loop if you know the size of the stream and set it to 0 if you don't know the size and using a buffer?</p>
  
  <pre><code>   // Now read s into a byte buffer.
      byte[] bytes = new byte[s.Length];
      int numBytesToRead = (int) s.Length;
      int numBytesRead = 0;
      while (numBytesToRead &gt; 0)
      {
          // Read may return anything from 0 to 10.
          int n = s.Read(bytes, numBytesRead, 10);
          // The end of the file is reached.
          if (n == 0)
          {
              break;
          }
          numBytesRead += n;
          numBytesToRead -= n;
      }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
  {
      const int size = 4096;
      byte[] buffer = new byte[size];
      using (MemoryStream memory = new MemoryStream())
      {
      int count = 0;
      do
      {
          count = stream.Read(buffer, 0, size);
          if (count &gt; 0)
          {
          memory.Write(buffer, 0, count);
          }
      }
      while (count &gt; 0);
      return memory.ToArray();
      }
  }
  </code></pre>