restinio::transforms::zlib::zlib_t Class Reference

Zlib transformator. More...

`#include <zlib.hpp>`

## Public Member Functions

| zlib_t (const params_t &transformation_params)
| zlib_t (const zlib_t &)=delete
| zlib_t (zlib_t &&)=delete
zlib_t & | operator= (const zlib_t &)=delete
zlib_t & | operator= (zlib_t &&)=delete
| ~zlib_t ()
const params_t & | params () const
| Get parameters of current transformation. More...
void | write (string_view_t input)
| Append input data. More...
void | flush ()
| Flush the zlib stream. More...
void | complete ()
| Complete the stream. More...
std::string | giveaway_output ()
| Get current accumulated output data. More...
auto | output_size () const
| Get current output size. More...
bool | is_completed () const
| Is operation complete? More...

## Private Member Functions

bool | is_identity () const
const char * | get_error_msg () const
| Get zlib error message if it exists. More...
void | ensure_operation_in_not_completed () const
| Checks completion flag and throws if operation is is already completed. More...
void | inc_buffer ()
| Increment internal buffer for receiving output. More...
auto | prepare_out_buffer ()
| Prepare out buffer for receiving data. More...
void | write_compress_impl (int flush)
| Handle incoming data for compression operation. More...
void | write_decompress_impl (int flush)
| Handle incoming data for decompression operation. More...

## Private Attributes

const params_t | m_params
| Parameters for zlib. More...
bool | m_zlib_stream_initialized { false }
| Flag: was m_zlib_stream initialized properly. More...
z_stream | m_zlib_stream
| zlib stream. More...
std::string | m_out_buffer
| Output buffer. More...
std::size_t | m_write_pos { 0 }
| Next write pos in out buffer. More...
bool | m_operation_is_complete { false }

## Detailed Description

Zlib transformator.

Uses zlib (https://zlib.net) for gzip/deflate compresion/decompression. All higher level functionality that minimizes boilerplate code and makes compression and decompression logic less verbose is based on using zlib_t.

Simple usage:

namespace rtz = restinio::transforms::zlib;
rtz::zlib_t z{ rtz::make_gzip_compress_params( compression_level ).level( 9 ) };
z.write( input_data );
z.complete();
auto gziped_data = z.giveaway_output();
restinio::transforms::zlib::params_t::level
int level() const
Get compression level.
Definition: zlib.hpp:127
restinio::transforms::zlib
Definition: zlib.hpp:34
restinio::transforms::zlib::make_gzip_compress_params
params_t make_gzip_compress_params(int compression_level=-1)
Definition: zlib.hpp:397

Advanced usage:

namespace rtz = restinio::transforms::zlib;
rtz::zlib_t z{ rtz::make_gzip_compress_params( compression_level ).level( 9 ) };
std::size_t processed_data = 0;
for( const auto d : data_pieces )
{
z.write( d );
// Track how much data is already processed:
processed_data += d.size();
if( processed_data > 1024 * 1024 )
{
// If more than 1Mb is processed do flush.
z.flush();
}
if( z.output_size() > 100 * 1024 )
{
// If more than 100Kb of data is ready, then append it to something.
append_output( z.giveaway_output() );
}
}
// Complete the stream and append remeining putput data.
z.complete();
append_output( z.giveaway_output() );

Since
  v.0.4.4

Definition at line 474 of file zlib.hpp.

## Constructor & Destructor Documentation

## zlib_t() [1/3]

restinio::transforms::zlib::zlib_t::zlib_t | ( | const params_t & | transformation_params | ) |

inline

Definition at line 477 of file zlib.hpp.

## zlib_t() [2/3]

restinio::transforms::zlib::zlib_t::zlib_t | ( | const zlib_t & |  | ) |

delete

## zlib_t() [3/3]

restinio::transforms::zlib::zlib_t::zlib_t | ( | zlib_t && |  | ) |

delete

## ~zlib_t()

restinio::transforms::zlib::zlib_t::~zlib_t | ( |  | ) |

inline

Definition at line 541 of file zlib.hpp.

## Member Function Documentation

## complete()

void restinio::transforms::zlib::zlib_t::complete | ( |  | ) |

inline

Complete the stream.

Definition at line 633 of file zlib.hpp.

## ensure_operation_in_not_completed()

void restinio::transforms::zlib::zlib_t::ensure_operation_in_not_completed | ( |  | ) | const

inline private

Checks completion flag and throws if operation is is already completed.

Definition at line 713 of file zlib.hpp.

## flush()

void restinio::transforms::zlib::zlib_t::flush | ( |  | ) |

inline

Flush the zlib stream.

Flushes underlying zlib stream. All pending output is flushed to the output buffer.

Definition at line 611 of file zlib.hpp.

## get_error_msg()

const char * restinio::transforms::zlib::zlib_t::get_error_msg | ( |  | ) | const

inline private

Get zlib error message if it exists.

Definition at line 702 of file zlib.hpp.

## giveaway_output()

std::string restinio::transforms::zlib::zlib_t::giveaway_output | ( |  | ) |

inline

Get current accumulated output data.

On this request a current accumulated output data is reterned. Move semantics is applied. Once current output is fetched zlib_t object resets its internal out buffer.

In the following code:

restinio::transformator::zlib_t z{ restinio::transformator::gzip_compress() };
z.write( A );
consume_out( z.giveaway_output() ); // (1)
z.write( B );
z.write( C );
consume_out( z.giveaway_output() ); // (2)
restinio::transforms::zlib::gzip_compress
std::string gzip_compress(string_view_t input, int compression_level=-1)
Definition: zlib.hpp:900

At the point (2) `consume_out()` function receives a string that is not an appended version of a string received in point (1).

Definition at line 678 of file zlib.hpp.

## inc_buffer()

void restinio::transforms::zlib::zlib_t::inc_buffer | ( |  | ) |

inline private

Increment internal buffer for receiving output.

Definition at line 721 of file zlib.hpp.

## is_completed()

bool restinio::transforms::zlib::zlib_t::is_completed | ( |  | ) | const

inline

Is operation complete?

Definition at line 692 of file zlib.hpp.

## is_identity()

bool restinio::transforms::zlib::zlib_t::is_identity | ( |  | ) | const

inline private

Definition at line 695 of file zlib.hpp.

## operator=() [1/2]

zlib_t & restinio::transforms::zlib::zlib_t::operator= | ( | const zlib_t & |  | ) |

delete

## operator=() [2/2]

zlib_t & restinio::transforms::zlib::zlib_t::operator= | ( | zlib_t && |  | ) |

delete

## output_size()

auto restinio::transforms::zlib::zlib_t::output_size | ( |  | ) | const

inline

Get current output size.

Definition at line 689 of file zlib.hpp.

## params()

const params_t & restinio::transforms::zlib::zlib_t::params | ( |  | ) | const

inline

Get parameters of current transformation.

Definition at line 557 of file zlib.hpp.

## prepare_out_buffer()

auto restinio::transforms::zlib::zlib_t::prepare_out_buffer | ( |  | ) |

inline private

Prepare out buffer for receiving data.

Definition at line 729 of file zlib.hpp.

## write()

void restinio::transforms::zlib::zlib_t::write | ( | string_view_t | input | ) |

inline

Append input data.

Pushes a given data to zlib transform. input is the data to be compressed or decompressed.

Definition at line 565 of file zlib.hpp.

## write_compress_impl()

void restinio::transforms::zlib::zlib_t::write_compress_impl | ( | int | flush | ) |

inline private

Handle incoming data for compression operation.

Definition at line 749 of file zlib.hpp.

## write_decompress_impl()

void restinio::transforms::zlib::zlib_t::write_decompress_impl | ( | int | flush | ) |

inline private

Handle incoming data for decompression operation.

Definition at line 798 of file zlib.hpp.

## Member Data Documentation

## m_operation_is_complete

bool restinio::transforms::zlib::zlib_t::m_operation_is_complete { false }

private

Definition at line 854 of file zlib.hpp.

## m_out_buffer

std::string restinio::transforms::zlib::zlib_t::m_out_buffer

private

Output buffer.

Definition at line 850 of file zlib.hpp.

## m_params

const params_t restinio::transforms::zlib::zlib_t::m_params

private

Parameters for zlib.

Definition at line 837 of file zlib.hpp.

## m_write_pos

std::size_t restinio::transforms::zlib::zlib_t::m_write_pos { 0 }

private

Next write pos in out buffer.

Definition at line 852 of file zlib.hpp.

## m_zlib_stream

z_stream restinio::transforms::zlib::zlib_t::m_zlib_stream

private

zlib stream.

Definition at line 847 of file zlib.hpp.

## m_zlib_stream_initialized

bool restinio::transforms::zlib::zlib_t::m_zlib_stream_initialized { false }

private

Flag: was m_zlib_stream initialized properly.

If deflateInit2()/inflateInit2() was completed successfully it is needed to call deflateEnd()/inflateEnd().

Definition at line 844 of file zlib.hpp.

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

- restinio/transforms/zlib.hpp