title: Unable to read beyond the end of the stream
body: <p>I did some quick method to write a file from a stream but it's not done yet. I receive this exception and I can't find why:</p>
  
  <pre><code>Unable to read beyond the end of the stream
  </code></pre>
  
  <p>Is there anyone who could help me debug it?</p>
  
  <pre><code>public static bool WriteFileFromStream(Stream stream, string toFile)
  {
      FileStream fileToSave = new FileStream(toFile, FileMode.Create);
      BinaryWriter binaryWriter = new BinaryWriter(fileToSave);
  
      using (BinaryReader binaryReader = new BinaryReader(stream))
      {
          int pos = 0;
          int length = (int)stream.Length;
  
          while (pos &lt; length)
          {
              int readInteger = binaryReader.ReadInt32();
  
              binaryWriter.Write(readInteger);
  
              pos += sizeof(int);
          }
      }
  
      return true;
  }
  </code></pre>
  
  <p>Thanks a lot!</p>