Possible Duplicate:
Copy a streambuf's contents to a string
Recently I am working around boost::asio::streambuf. Because of my poor English I cannot express what I mean. So first, please look at the code below.
class bar {
public:
int length() const
{
return sizeof buffer;
}
char* get()
{
return &buffer[0];
}
private:
char buffer[100];
};
template<typename Elem, typename Traits>
std::basic_istream<Elem, Traits>& operator>>(std::basic_istream<Elem, Traits> &is, bar& data)
{
return is.read(data.get(), data.length());
}
boost::asio::streambuf buf;
buf.prepare(65535);
std::ostream os(&buf);
// some operations writing data to buf
std::istream is(&buf);
bar bdata
is >> bdata; // #1
At #1, data will be read from buf
and stored to bdata
.
This causes that the data that is stored to bdata
is deleted from buf
.
I want to get data from streambuf, but don't want to change the streambuf's contents at all.
streambuf has a lot of data and the data I want are located in the beginning of streambuf.
Is it possible? Thank you.