title: Creating a std::iostream adapter
body: <p>I'd like to create an <code>iostream</code> adapter class which lets me modify the data written to or read from a stream on-the-fly.
  The adapter itself should be a <code>iostream</code> to allow true transparency towards third-party code.</p>
  
  <p>Example for a <code>StreamEncoder</code> class derived from <code>std::ostream</code>:</p>
  
  <pre><code>// External algorithm, creates large amounts of log data
  int foo(int bar, std::ostream&amp; logOutput);
  
  int main()
  {
      // The target file
      std::ofstream file("logfile.lzma");
      // A StreamEncoder compressing the output via LZMA
      StreamEncoder lzmaEncoder(file, &amp;encodeLzma);
      // A StreamEncoder converting the UTF-8 log data to UTF-16
      StreamEncoder utf16Encoder(lzmaEncoder, &amp;utf8ToUtf16);
  
      // Call foo(), but write the log data to an LZMA-compressed UTF-16 file
      cout &lt;&lt; foo(42, utf16Encoder);
  }
  </code></pre>
  
  <p>As far as I know, I need to create a new <code>basic_streambuf</code> derivate and embed it in a <code>basic_ostream</code> subclass, but that seems to be pretty complex.</p>
  
  <p>Is there any easier way to accomplish this? </p>