3

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.

Community
  • 1
  • 1
xps_l502
  • 107
  • 1
  • 9
  • 1
    Really? I think the link above cannot be applied to this. – xps_l502 Oct 07 '12 at 14:08
  • It can. Use `const char* header=boost::asio::buffer_cast(readbuffer.data());` to gain direct access to the data, but don't `consume`. – Igor R. Oct 08 '12 at 09:19
  • Oh, I overlooked the second answer of "Copy a streambuf's contents to a string". And buffer_cast is what I want. Thank you! – xps_l502 Oct 10 '12 at 07:52

0 Answers0