Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Tools Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

5.7 Compressed IOStreams

The class RWCompressedStreamBuffer derives from std::streambuf, and provides a mechanism to compress data that is serialized to an std::ostream or deserialized from an std::istream. RWCompressedStreamBuffer is a class template with a single template parameter that specifies the compressor type to be used. Rogue Wave provides the class RWSnappyCompressor based on the Google® snappy compression algorithm. You can use your own compressor class, whose only requirement is that it provide the same API as RWSnappyCompressor.

The classes RWCompressedIOStream, RWCompressedIStream, and RWCompressedOStream provide standard stream interfaces to RWCompressedStreamBuffer.

The RWCompressedStreamBuffer-based streams are used to compress and serialize object representations to an associated stream buffer. Objects can be serialized directly to or from these stream classes just as you would with any standard stream:

The RWCompressedStreamBuffer-based streams can also be used with the Essential Tools Module virtual streams classes. For example, here is a method to write an RWCollectable to an std::ofstream in a portable format:

The above function writes the provided object to a std::ofstream by way of an RWeostream. To add compression, you would simply do this:

The RWCompressedOStream simply takes the portable object representation generated by the RWeostream, compresses it using an RWSnappyCompressor, and then forwards the compressed data along to the std::ofstream for storage on disk, just as in the previous example.

Here is another example for writing and reading compressed data from an RWSocketPortal:

After creating a portal, and a stream for reading from and writing to the portal, the above code creates an RWCompressedIOStream. The final lines stream the data to and from the portal.

5.7.1 Buffer Size

The constructors for each of the compressed stream classes take a buffer size parameter. If not provided, the default size is 2048U. Data sent to the stream buffer is buffered in a block of the provided size until there is no room for additional data. Once the buffer is filled, it is compressed and forwarded along to the underlying stream buffer. A larger buffer tends to improve the compression ratio, but makes large blocks of contiguous memory unavailable to the rest of your application. A smaller value mitigates the memory problem, but the compression ratio suffers. This is something you need to adjust in the context of your application to decide on the value that produces the best performance.

5.7.2 Typedefs

The following convenience typedefs are provided:

Use these to create IOStream objects using the default Snappy compressor. For example:

5.7.3 Custom Compressor Interface

In general, the interface for a custom compressor must match that of RWSnappyCompressor. More specifically:



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.