Question

I'm trying to using filtering_streams to compress the serialization of some object into an array_sink or similar device where I can then determine the length of the compressed output and copy it to another stream, say a file. However, using ostream::tellp on the filtering_ostream causes boost to throw a runtime exception. I can't really figure out what I'm doing wrong.

using namespace boost::iostreams;

char *buffer = new char[4096*255];
array_sink zipStream(buffer, 4096*255);

filtering_ostream tempOut;
tempOut.push(zlib_compressor());
tempOut.push(zipStream);

column->Serialize(tempOut); // Object::Serialize(ostream&)
tempOut.flush(); // ?
int zipSize = tempOut.tellp();

// Do stuff with zipStream...
Was it helpful?

Solution

The problem is that tellp is implemented in terms of the underlying stream buffers pubseekoff with 0 offset from current write head position ( basically, it's just poor design ). Now, the kicker here is that zlib_compressor does not work with output_seekable sinks ( as can be seen in the docs ). This is fairly natural since changing the write head would almost definitively lead to corrupted data. You have the very same problem if you try to decompress.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top