10 #ifndef MSGPACK_V1_ZBUFFER_HPP
11 #define MSGPACK_V1_ZBUFFER_HPP
18 #include <boost/assert.hpp>
28 zbuffer(
int level = Z_DEFAULT_COMPRESSION,
32 m_stream.zalloc = Z_NULL;
33 m_stream.zfree = Z_NULL;
34 m_stream.opaque = Z_NULL;
35 m_stream.next_out = Z_NULL;
36 m_stream.avail_out = 0;
37 if(deflateInit(&m_stream, level) != Z_OK) {
38 throw std::bad_alloc();
44 deflateEnd(&m_stream);
49 void write(
const char* buf,
size_t len)
51 BOOST_ASSERT(buf || len == 0);
54 m_stream.next_in =
reinterpret_cast<Bytef*
>(
const_cast<char*
>(buf));
55 m_stream.avail_in =
static_cast<uInt
>(len);
57 while(m_stream.avail_in > 0) {
60 throw std::bad_alloc();
64 if(deflate(&m_stream, Z_NO_FLUSH) != Z_OK) {
65 throw std::bad_alloc();
73 switch(deflate(&m_stream, Z_FINISH)) {
79 throw std::bad_alloc();
83 throw std::bad_alloc();
100 return static_cast<size_t>(
reinterpret_cast<char*
>(m_stream.next_out) - m_data);
105 if(deflateReset(&m_stream) != Z_OK) {
106 throw std::bad_alloc();
113 m_stream.avail_out +=
static_cast<uInt
>(
reinterpret_cast<char*
>(m_stream.next_out) - m_data);
114 m_stream.next_out =
reinterpret_cast<Bytef*
>(m_data);
122 m_stream.avail_out = 0;
129 size_t used =
static_cast<size_t>(
reinterpret_cast<char*
>(m_stream.next_out) - m_data);
130 size_t csize = used + m_stream.avail_out;
131 size_t nsize = (csize == 0) ? m_init_size : csize * 2;
133 char* tmp =
static_cast<char*
>(::realloc(m_data, nsize));
139 m_stream.next_out =
reinterpret_cast<Bytef*
>(tmp + used);
140 m_stream.avail_out =
static_cast<uInt
>(nsize - used);
144 #if defined(MSGPACK_USE_CPP03)
Definition: zbuffer.hpp:26
size_t size() const
Definition: zbuffer.hpp:98
char * flush()
Definition: zbuffer.hpp:70
zbuffer(int level=Z_DEFAULT_COMPRESSION, size_t init_size=MSGPACK_ZBUFFER_INIT_SIZE)
Definition: zbuffer.hpp:28
char * data()
Definition: zbuffer.hpp:88
const char * data() const
Definition: zbuffer.hpp:93
~zbuffer()
Definition: zbuffer.hpp:42
void write(const char *buf, size_t len)
Definition: zbuffer.hpp:49
char * release_buffer()
Definition: zbuffer.hpp:117
void reset_buffer()
Definition: zbuffer.hpp:111
void reset()
Definition: zbuffer.hpp:103
Definition: adaptor_base.hpp:15
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66
#define MSGPACK_ZBUFFER_RESERVE_SIZE
Definition: zbuffer_decl.hpp:16
#define MSGPACK_ZBUFFER_INIT_SIZE
Definition: zbuffer_decl.hpp:20