title: Boost streambuf to const char* with buffer_cast vs std::string vs ostringstream
body: <p>I have a client/server application using boost::read_until with a boost::streambuf. I'm reading an XML message from a socket and want to parse this with tinyXML2 like so:</p>
  
  <pre><code>XMLDocument doc;
  doc.parse(strPtr); // strPtr is const char* 
  </code></pre>
  
  <p>I need to somehow extract const char *strPtr from the streambuf. So far I found three methods here on stack exchange:</p>
  
  <pre><code>// method 1, string constructor
  const char * streambufToPtr(boost::asio::streambuf &amp;message) {
      boost::asio::streambuf::const_buffers_type bufs = message.data();
      std::string astr(boost::asio::buffers_begin(bufs), boost::asio::buffers_begin(bufs) + message.size());
      return astr.c_str();
  }
  
  // method 2, stringstream
  const char * streambufToPtr(boost::asio::streambuf &amp;message) {
      std::ostringstream ss;
      ss &lt;&lt; &amp;message;
      std::string astr = ss.str();
      return astr.c_str();
  }
  
  // method 3, buffer_cast
  const char * streambufToPtr(boost::asio::streambuf &amp;message) {
      const char* bufPtr=boost::asio::buffer_cast&lt;const char*&gt;(message.data());
      return bufPtr;
  }
  </code></pre>
  
  <p>All three methods work in my code (so far). Method 1 and 2 probably copies the data at least once (or more), while method 3 does something else. </p>
  
  <p>What goes on in each of the methods?
  How fast are they in comparison?
  Is any of these unsafer in terms of buffer overflows, threading or other traps I might run into later with a network client/server application?</p>