title: Pass-Through Stream (not having to save to memory in the middle)
body: <p>In my c# program, I am working with an object (third-party, so I have no way of changing its source code) that takes a stream in in its constructor (<code>var myObject = new MyObject(stream);</code>).</p>
  
  <p>My challenge is that I need to make some changes to some of the lines of my file prior to it being ready to be given to this object.</p>
  
  <p>To do this, I wrote the following code (which does work):</p>
  
  <pre><code>using (var st = new MemoryStream())
  using (var reader = new StreamReader(path))
  {
      using (var writer = new StreamWriter(st))
      {
          while (!reader.EndOfStream)
          {
              var currentLine = reader.ReadLine().Replace(@"XXXX", @"YYYY");
              writer.Write(currentLine);
          }
          writer.Flush();
          st.Position = 0;
          var myObject = new MyObject(st);
      }
  }
  </code></pre>
  
  <p>So, this does work, but it seems so inefficient since it is no longer really streaming the information, but storing it all in the memory stream before giving it through to the object.</p>
  
  <p>Is there a way to create a transform / "pass-through stream" that will:</p>
  
  <ol>
  <li>Read in each small amount from the streamreader</li>
  <li>Make the adjustment on that small amount</li>
  <li>Stream that amount through</li>
  </ol>
  
  <p>So there won't be a large bit of memory storage in the middle?</p>
  
  <p>Thanks!!</p>