title: OutOfMemory Exception while converting MemoryStream to Array
body: <p>I use the following code to return a byte array in <code>HttpResponseMessage</code>:</p>
  
  <pre><code>using (WebResponse response = (HttpWebResponse)request.GetResponse())
  {
     byte[] bytes = ReadFully(response.GetResponseStream());
     ......
  }
  public static byte[] ReadFully(Stream input)
  {
    byte[] buffer = new byte[16*1024];
    using (MemoryStream ms = new MemoryStream())
    {
      int read;
      while ((read = input.Read(buffer, 0, buffer.Length)) &gt; 0)
      {
          ms.Write(buffer, 0, read);
      }
      return ms.ToArray(); // This line throws OutOfMemory exception
    }
  }
  </code></pre>
  
  <p>An <code>OutOfMemory</code> exception is thrown in the last <code>return ms.ToArray()</code> statement. </p>
  
  <p>I need to set the resulting <code>byte[]</code> as <code>HttpResponseMessage.Content</code>. </p>