#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/core/demangle.hpp>
+#include <boost/iostreams/device/array.hpp>
+#include <boost/iostreams/device/back_inserter.hpp>
+#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/serialization/array.hpp>
#include <boost/serialization/complex.hpp>
#include <boost/serialization/vector.hpp>
#ifdef DEAL_II_WITH_ZLIB
-# include <boost/iostreams/device/back_inserter.hpp>
# include <boost/iostreams/filter/gzip.hpp>
-# include <boost/iostreams/filtering_stream.hpp>
-# include <boost/iostreams/stream.hpp>
#endif
DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
std::vector<char> &dest_buffer,
const bool allow_compression)
{
- // the data is never compressed when we can't use zlib.
- (void)allow_compression;
-
std::size_t size = 0;
// see if the object is small and copyable via memcpy. if so, use
// use buffer as the target of a compressing
// stream into which we serialize the current object
const std::size_t previous_size = dest_buffer.size();
+ {
+ boost::iostreams::filtering_ostreambuf fosb;
#ifdef DEAL_II_WITH_ZLIB
- if (allow_compression)
- {
- boost::iostreams::filtering_ostream out;
- out.push(
- boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(
- boost::iostreams::gzip::default_compression)));
- out.push(boost::iostreams::back_inserter(dest_buffer));
-
- boost::archive::binary_oarchive archive(out);
- archive << object;
- out.flush();
- }
- else
+ if (allow_compression)
+ fosb.push(boost::iostreams::gzip_compressor());
+#else
+ (void)allow_compression;
#endif
- {
- std::ostringstream out;
- boost::archive::binary_oarchive archive(out);
- archive << object;
-
- const std::string s = out.str();
- dest_buffer.reserve(dest_buffer.size() + s.size());
- std::move(s.begin(), s.end(), std::back_inserter(dest_buffer));
- }
+ fosb.push(boost::iostreams::back_inserter(dest_buffer));
+ boost::archive::binary_oarchive boa(fosb);
+ boa << object;
+ // the stream object has to be destroyed before the return statement
+ // to ensure that all data has been written in the buffer
+ }
size = dest_buffer.size() - previous_size;
}
{
T object;
- // the data is never compressed when we can't use zlib.
- (void)allow_compression;
-
// see if the object is small and copyable via memcpy. if so, use
// this fast path. otherwise, we have to go through the BOOST
// serialization machinery
}
else
{
- std::string decompressed_buffer;
-
- // first decompress the buffer
+ // decompress the buffer section into the object
+ boost::iostreams::filtering_istreambuf fisb;
#ifdef DEAL_II_WITH_ZLIB
if (allow_compression)
- {
- boost::iostreams::filtering_ostream decompressing_stream;
- decompressing_stream.push(boost::iostreams::gzip_decompressor());
- decompressing_stream.push(
- boost::iostreams::back_inserter(decompressed_buffer));
- decompressing_stream.write(&*cbegin, std::distance(cbegin, cend));
- }
- else
+ fisb.push(boost::iostreams::gzip_decompressor());
+#else
+ (void)allow_compression;
#endif
- {
- decompressed_buffer.assign(cbegin, cend);
- }
-
- // then restore the object from the buffer
- std::istringstream in(decompressed_buffer);
- boost::archive::binary_iarchive archive(in);
+ fisb.push(boost::iostreams::array_source(&*cbegin, &*cend));
- archive >> object;
+ boost::archive::binary_iarchive bia(fisb);
+ bia >> object;
}
return object;
}
else
{
- std::string decompressed_buffer;
-
- // first decompress the buffer
- (void)allow_compression;
+ // decompress the buffer section into the object
+ boost::iostreams::filtering_istreambuf fisb;
#ifdef DEAL_II_WITH_ZLIB
if (allow_compression)
- {
- boost::iostreams::filtering_ostream decompressing_stream;
- decompressing_stream.push(boost::iostreams::gzip_decompressor());
- decompressing_stream.push(
- boost::iostreams::back_inserter(decompressed_buffer));
- decompressing_stream.write(&*cbegin, std::distance(cbegin, cend));
- }
- else
+ fisb.push(boost::iostreams::gzip_decompressor());
+#else
+ (void)allow_compression;
#endif
- {
- decompressed_buffer.assign(cbegin, cend);
- }
-
- // then restore the object from the buffer
- std::istringstream in(decompressed_buffer);
- boost::archive::binary_iarchive archive(in);
+ fisb.push(boost::iostreams::array_source(&*cbegin, &*cend));
- archive >> unpacked_object;
+ boost::archive::binary_iarchive bia(fisb);
+ bia >> unpacked_object;
}
}