views:

173

answers:

1

I have a block of binary data defined as:

void* address, size_t binarySize;

that I want to store to a MySQL database using MySQL C++ Connector.

The function setBlob() takes istream.

The question:

How can I convert from a raw void* address, size_t binarySize to either an istream object or istringstream? Is it possible to do this without "copying" the data? i.e. tell istream the pointer and the size so that it could point to it.

+4  A: 

You have to subclass streambuf e.g. like this:

class DataBuf : public streambuf
{
public:
   DataBuf(char * d, size_t s) {
      setg(d, d, d + s);
   }
};

Then you can instantiate an istream object which uses a DataBuf as buffer, which itself uses your block of binary data. Supposing that binarySize specifies the size of your binary data in bytes (sizeof(char) should be one byte), you could do this like so:

DataBuf buffer((char*)address, binarySize);
istream stream(&buffer);

That istream object you can now pass to setBlob().

Regards, Elrohir

Elrohir
awesome answer! thank you!
ShaChris23