title: How can I connect an object that requires a writable stream to an object that wants a Readable stream?
body: <p>A DataSet knows how to serialize to a stream via WriteXml(). </p>
  
  <p>I have an object that wants a readable stream.  How can I connect the DataSet to this other object? </p>
  
  <p>The plumbing analogy: suppose a 1/2" copper pipe has a "male" outlet, and a valve also has a "male" fitting.  To connect the two I need an adapter - female on both ends.  </p>
  
  <p>I am imagining an AdapterStream class that is multi-thread-aware.  The stream internally is implemented with a fixed buffer, say 8kbytes.  On one  thread I call WriteXml() using an instance of that AdaterStream. The implementation of WriteXml repeatedly calls Write() to the stream until it's done.  But AdapterStream blocks when the buffer is full, and waits until the buffer is drained via a call to Read().  In a separate thread, the app calls Read() on that AdapterStream until it returns zero.  </p>
  
  <p>If the buffer is empty that doesn't mean Read() can return zero. It can only return zero when an EOF is signaled by the writing side. (maybe with a Close() or WriteEof() method). </p>
  
  <p>You'd need an ManualResetEvent to be used to signal between the Write() and Read().  </p>
  
  <p>It wouldn't be too hard to write.  But, does such a thing exist? </p>
  
  <p>Something like <a href="http://msdn.microsoft.com/en-us/magazine/cc163290.aspx" rel="nofollow noreferrer">the BlockingStream Stephen Taub provided in MSDN Magazine</a>, except without the unlimited buffer allocation. </p>