cx::Compressor Class Reference

Compression filter using zlib. More...

#include <Compressor.hpp>

Inheritance diagram for cx::Compressor:
cx::Filter cx::ConcreteNode cx::NonCopyable

List of all members.

Public Member Functions

 Compressor (int level=-1, std::size_t bufferSize=16384)
 Constructor.
 ~Compressor ()
 Destructor.
void pass (Chunk &chunk)
 Do a compression step.
void finish (Chunk &out)
 Finish the compression operation.
unsigned int getChecksum () const
 Get checksum value after compression.
void apply (Chunk &chunk)
 Completelly filter the provided data.

Protected Member Functions

void wait ()
 Wait for the pipeline to terminate.
void beforeDestruction ()
 Handy cleanup.
void setStream (Stream *stream)
 Change the owning stream.

Detailed Description

Compression filter using zlib.

This filter applies ZLib compression to each chunk of data passed. It keeps it's internal compression state between chunks.

Compressor computes the adler32 checksum of the decompressed data. You can obtain it by calling the function getChecksum(). You can store the checksum in some place (a database, a file, etc), and use it later when decompressing the same data, to check if integrity was maintained (if it wasn't corrupted from the outside).

Example of compressing a chunk of data in one step.

 cx::Chunk myData;
 // fill myData
 ...
 // compress myData
 cx::Compressor compressor;
 compressor.apply(myData);
 unsigned int checksum = compressor.getChecksum();

Example of using the Compressor in a stream:

 cx::File inputFile("original.txt");
 cx::File outputFile("compressed.dat");
 cx::Compressor compressor;
 cx::Stream stream(inputFile, compressor, outputFile);
 unsigned int checksum = compressor.getChecksum();

Note: Remember to catch eventual exceptions when using the Compressor or Stream classes.

See also:
cx::Decompressor
cx::Filter

Definition at line 42 of file Compressor.hpp.


Constructor & Destructor Documentation

cx::Compressor::Compressor ( int  level = -1,
std::size_t  bufferSize = 16384 
) [explicit]

Constructor.

Initialize the zlib state.

The first argument is the compression rate, ranging from 0 to 9. Level 0 means no compression, level 9 means best (but slower) compression.

The constructor may throw a cx::DataException if the zlib fails it's initialization (due to invalid compression level for instance).

Parameters:
takeChecksum Function pointer that will receive the checksum.
level Compression level (0-9).
bufferSize Size of the internal zlib buffer. The bigger, the better (but don't put all your available memory on it).
cx::Compressor::~Compressor (  ) 

Destructor.

Clean up the zlib state.


Member Function Documentation

void cx::Filter::apply ( Chunk chunk  )  [inherited]

Completelly filter the provided data.

Parameters:
[in,out] chunk The data to be filtered. It is directly modified by the filter.
void cx::ConcreteNode::beforeDestruction (  )  [protected, inherited]

Handy cleanup.

This function safelly wait for the stream conclusion (if assigned), so that classes can finally clean up their resources. This function should be called on derived classes destructors before cleaning up their critical structures. Otherwise, a stream may try to access strucutures during their destruction.

void cx::Compressor::finish ( Chunk out  )  [virtual]

Finish the compression operation.

Parameters:
[out] chunk Chunk where to write the remaining data cached on the zlib internal buffer.
See also:
Filter::finish

Reimplemented from cx::Filter.

unsigned int cx::Compressor::getChecksum (  )  const

Get checksum value after compression.

Returns:
The checksum of the original data.
void cx::Compressor::pass ( Chunk chunk  )  [virtual]

Do a compression step.

Parameters:
[in,out] chunk The chunk of data to be compressed.
See also:
Filter::pass

Implements cx::Filter.

void cx::ConcreteNode::setStream ( Stream stream  )  [protected, inherited]

Change the owning stream.

This function is called by Stream itself when being constructed.

void cx::ConcreteNode::wait (  )  [protected, inherited]

Wait for the pipeline to terminate.

If it's being used on a pipeline stream, it blocks execution until the stream finish.


The documentation for this class was generated from the following file: