我正在尝试使用filtering_streams将某些对象的序列化压缩到array_sink
或类似设备中,然后我可以确定压缩输出的长度并将其复制到另一个流,例如文件。但是,在 filtering_ostream
上使用 ostream::tellp
会导致 boost 引发运行时异常。我真的不知道我做错了什么。
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...
最佳答案
问题在于 tellp
是根据底层流缓冲区 pubseekoff
实现的,与当前写入头位置的偏移量为 0(基本上,这只是糟糕的设计)。现在,这里的关键是 zlib_compressor
不能与 output_seekable
接收器一起使用(如文档中所示)。这是相当自然的,因为更改写入头几乎肯定会导致数据损坏。如果您尝试解压缩,也会遇到同样的问题。
关于c++ - boost filtering_stream和tellp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10533170/