10 #ifndef MSGPACK_V1_ZBUFFER_HPP 11 #define MSGPACK_V1_ZBUFFER_HPP 26 zbuffer(
int level = Z_DEFAULT_COMPRESSION,
28 : m_data(
nullptr), m_init_size(init_size)
30 m_stream.zalloc = Z_NULL;
31 m_stream.zfree = Z_NULL;
32 m_stream.opaque = Z_NULL;
33 m_stream.next_out = Z_NULL;
34 m_stream.avail_out = 0;
35 if(deflateInit(&m_stream, level) != Z_OK) {
36 throw std::bad_alloc();
42 deflateEnd(&m_stream);
47 void write(
const char* buf,
size_t len)
49 m_stream.next_in =
reinterpret_cast<Bytef*
>(
const_cast<char*
>(buf));
50 m_stream.avail_in = len;
52 while(m_stream.avail_in > 0) {
55 throw std::bad_alloc();
59 if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) {
60 throw std::bad_alloc();
68 switch(deflate(&m_stream, Z_FINISH)) {
73 throw std::bad_alloc();
77 throw std::bad_alloc();
94 return reinterpret_cast<char*
>(m_stream.next_out) - m_data;
99 if(deflateReset(&m_stream) != Z_OK) {
100 throw std::bad_alloc();
107 m_stream.avail_out +=
reinterpret_cast<char*
>(m_stream.next_out) - m_data;
108 m_stream.next_out =
reinterpret_cast<Bytef*
>(m_data);
115 m_stream.next_out =
nullptr;
116 m_stream.avail_out = 0;
123 size_t used =
reinterpret_cast<char*
>(m_stream.next_out) - m_data;
124 size_t csize = used + m_stream.avail_out;
125 size_t nsize = (csize == 0) ? m_init_size : csize * 2;
127 char* tmp =
static_cast<char*
>(::realloc(m_data, nsize));
133 m_stream.next_out =
reinterpret_cast<Bytef*
>(tmp + used);
134 m_stream.avail_out = nsize - used;
138 #if defined(MSGPACK_USE_CPP03) 142 #else // defined(MSGPACK_USE_CPP03) 145 #endif // defined(MSGPACK_USE_CPP03) 159 #endif // MSGPACK_V1_ZBUFFER_HPP const char * data() const
Definition: zbuffer.hpp:87
char * flush()
Definition: zbuffer.hpp:65
Definition: zbuffer.hpp:24
char * data()
Definition: zbuffer.hpp:82
Definition: adaptor_base.hpp:15
#define nullptr
Definition: cpp_config_decl.hpp:30
size_t size() const
Definition: zbuffer.hpp:92
#define MSGPACK_ZBUFFER_RESERVE_SIZE
Definition: zbuffer_decl.hpp:16
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
zbuffer(int level=Z_DEFAULT_COMPRESSION, size_t init_size=MSGPACK_ZBUFFER_INIT_SIZE)
Definition: zbuffer.hpp:26
~zbuffer()
Definition: zbuffer.hpp:40
char * release_buffer()
Definition: zbuffer.hpp:111
void write(const char *buf, size_t len)
Definition: zbuffer.hpp:47
#define MSGPACK_ZBUFFER_INIT_SIZE
Definition: zbuffer_decl.hpp:20
void reset()
Definition: zbuffer.hpp:97
void reset_buffer()
Definition: zbuffer.hpp:105