title: C++ Best approach to filtering bytes in a stream
body: <p>I'm learning c++ (coming from a C and Java university coursework) and today I want to write a class that filters the bytes taken from a generic stream and writes its output to another stream.</p>
  
  <p>To be coincise, let's say I want to make a class that base64-encodes the input and writes the output to stdout.</p>
  
  <p>In bash I would write:</p>
  
  <pre class="lang-bash prettyprint-override"><code>echo "some input data" | base64 
  </code></pre>
  
  <p>In C++ i want to implement a class <code>MyB64Encoder</code> that would behave like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>std::cout &lt;&lt; myB64EncoderObject &lt;&lt; "some input data";
  
  //Alternatively, is it possible to make it like this?
  std::cout &lt;&lt; MyB64Encoder &lt;&lt; "some input data";
  </code></pre>
  
  <p>The thing is, the <code>myB64EncoderObject</code> has, of course, to maintain an internal state and an internal buffer. To prevent blocking and excessive memory usage, it must be able to read and process small chunks of data and output each one of them immediately after it has been processed.</p>
  
  <p>There are a few more things to take care of:</p>
  
  <ul>
  <li>The object must wait for the output stream to be able to receive data</li>
  <li>The object must throw an error if there is no stream reading from it (kinda like a broken pipe?)</li>
  </ul>
  
  <p>What would be the best approach to a problem like this, in terms of efficiency? How would I implement it in modern C++1x?</p>