title: Function reading line from stream is blocking
body: <p>I have written the following C# function:</p>
  
  <pre><code>static string ReadLineCRLF(System.IO.Stream Stream, ref byte[] sharedBuffer, int bufferSize = 1024)
  {
      StringBuilder responseString = new StringBuilder("");
      string returnString = "";
      byte[] buffer = new byte[bufferSize];
      bool stopreading = false;
      bool firstexecution = true;
      while (!stopreading)
      {
          int readBytes;
          if (firstexecution &amp;&amp; sharedBuffer.Length &gt; 0)
          {
              readBytes = sharedBuffer.Length;
              sharedBuffer.CopyTo(buffer, 0);
          }
          else
          {
              readBytes = Stream.Read(buffer, 0, bufferSize); //BLOCKING HERE
          }
          firstexecution = false;
          if (readBytes &gt; 0)
          {
              int crIndex = Array.IndexOf(buffer, (byte)13); //13 = ASCII value for a carriage return
              if (crIndex &gt; -1 &amp;&amp; Array.IndexOf(buffer, (byte)10, crIndex + 1) == crIndex + 1) //10 = ASCII value for line feed
              {
  
                  stopreading = true;
                  sharedBuffer = readBytes - crIndex - 2  &gt; 0 ? ArraySlice&lt;byte&gt;(buffer, crIndex+2, readBytes-crIndex-2) : new byte[] { };
                  readBytes = crIndex;
              }
              if (readBytes &gt; 0)
              {
                  responseString.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, readBytes));
              }
              if (stopreading)
              {
                  returnString = responseString.ToString();
              }
          }
          if (!stopreading &amp;&amp; readBytes &lt;= 0)
          {
              returnString = null;
              stopreading = true;
              sharedBuffer = new byte[] { };
          }
      }
      return returnString;
  
  }
  </code></pre>
  
  <p>This function is blocking at <code>readBytes = Stream.Read(buffer, 0, bufferSize);</code> according to my Stack Explorer and using up a lot of computer performance. The only thing this function should do, is reading one line from a stream which is only ended by CRLF ("\r\n").</p>
  
  <p>According to the <a href="http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx" rel="nofollow">MSDN</a> <code>Stream.Read</code> returns <code>less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.</code> and it should usually block and not use up CPU-performance. <code>The implementation will block until at least one byte of data can be read, in the event that no data is available. Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file)</code>.</p>
  
  <p>So why does it - according to my CLR Stack Explorer - use up so much performance (up to 70%)? I do not see any logical mistake and I think it should wait until some bytes can be received. It also seems that this behaviour does not always occur but one or two days after the start of the application execution on a Windows server.</p>
  
  <p>ADDITIONAL EXPLANATION:
  As the bytes are read using chunks, it might be that too many bytes are read and stored in the buffer. Therefore I use a shared buffer, allowing it to use it again to go on reading the next line. Once a line is completely read, I remove it from the buffer.</p>
  
  <p>The ArraySlice function looks like this:</p>
  
  <pre><code>public static T[] ArraySlice&lt;T&gt;(T[] data, int index, int length)
  {
      T[] result = new T[length];
      Array.Copy(data, index, result, 0, length);
      return result;
  }
  </code></pre>