title: Unbuffered StreamReader
body: <p>Is there a way to keep StreamReader from doing any buffering?</p>
  
  <p>I'm trying to handle output from a Process that may be either binary or text. The output will look like an HTTP Response, e.g.</p>
  
  <pre><code>Content-type: application/whatever
  Another-header: value
  
  text or binary data here
  </code></pre>
  
  <p>What I want to do is to parse the headers using a <code>StreamReader</code>, and then either read from its <code>BaseStream</code> or the <code>StreamReader</code> to handle the rest of the content. Here's basically what I started with:</p>
  
  <pre><code>private static readonly Regex HttpHeader = new Regex("([^:]+): *(.*)");
  private void HandleOutput(StreamReader reader)
  {
    var headers = new NameValueCollection();
    string line;
    while((line = reader.ReadLine()) != null)
    {
      Match header = HttpHeader.Match(line);
      if(header.Success)
      {
        headers.Add(header.Groups[1].Value, header.Groups[2].Value);
      }
      else
      {
        break;
      }
    }
    DoStuff(reader.ReadToEnd());
  }
  </code></pre>
  
  <p>This seems to trash binary data. So I changed the last line to something like this:</p>
  
  <pre><code>if(headers["Content-type"] != "text/html")
  {
    // reader.BaseStream.Position is not at the same place that reader
    // makes it looks like it is.
    // i.e. reader.Read() != reader.BaseStream.Read()
    DoBinaryStuff(reader.BaseStream);
  }
  else
  {
    DoTextStuff(reader.ReadToEnd());
  }
  </code></pre>
  
  <p>... but StreamReader buffers its input, so reader.BaseStream is in the wrong position. Is there a way to unbuffer StreamReader? Or can I tell StreamReader to reset the stream back to where StreamReader is?</p>