Menu

[r2222]: / framework / trunk / src / gzip_filter.cpp  Maximize  Restore  History

Download this file

170 lines (149 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#include <zlib.h>
#include <booster/streambuf.h>
#include <booster/backtrace.h>
#include <ostream>
namespace cppcms {
namespace impl {
class gzip_device : public booster::io_device {
public:
gzip_device() :
opened_(false),
z_stream_(z_stream()),
out_(0),
level_(-1),
buffer_(4096)
{
}
void level(int v)
{
level_ = v;
}
void buffer(size_t size)
{
if(size < 256)
size = 256;
buffer_ = size;
}
void open()
{
if(deflateInit2(&z_stream_,
level_,
Z_DEFLATED,
15 + 16, // 15 window bits+gzip = 16,
8, // memuse
Z_DEFAULT_STRATEGY) != Z_OK)
{
std::string error = "ZLib init failed";
if(z_stream_.msg) {
error+=":";
error+=z_stream_.msg;
}
throw booster::runtime_error(error);
}
opened_ = true;
chunk_.resize(buffer_,0);
}
void output(std::ostream &out)
{
out_ = &out;
}
size_t write(char const *src,size_t n)
{
if(!out_ || !opened_) {
return 0;
}
if(n==0) {
return 0;
}
z_stream_.avail_in = n;
z_stream_.next_in = (Bytef*)(src);
z_stream_.avail_out = chunk_.size();
z_stream_.next_out = (Bytef*)(&chunk_[0]);
do {
deflate(&z_stream_,Z_NO_FLUSH);
size_t have = chunk_.size() - z_stream_.avail_out;
if(!out_->write(&chunk_[0],have)) {
close();
return 0;
}
} while(z_stream_.avail_out == 0);
return n;
}
void close()
{
if(!opened_)
return;
if(out_) {
z_stream_.avail_in = 0;
z_stream_.next_in = 0;
z_stream_.avail_out = chunk_.size();
z_stream_.next_out = (Bytef*)(&chunk_[0]);
do {
deflate(&z_stream_,Z_FINISH);
size_t have = chunk_.size() - z_stream_.avail_out;
if(!out_->write(&chunk_[0],have)) {
break;
}
} while(z_stream_.avail_out == 0);
out_->flush();
}
deflateEnd(&z_stream_);
opened_ = false;
z_stream_ = z_stream();
chunk_.clear();
}
~gzip_device()
{
if(opened_)
deflateEnd(&z_stream_);
}
private:
bool opened_;
std::vector<char> chunk_;
z_stream z_stream_;
std::ostream *out_;
int level_;
size_t buffer_;
};
class gzip_filter : public std::ostream, public gzip_device {
public:
gzip_filter() :
std::ios(&buf_),
std::ostream(&buf_)
{
init(&buf_);
buf_.device(*this);
}
void close()
{
flush();
gzip_device::close();
}
private:
gzip_device device_;
booster::streambuf buf_;
};
} // impl
} // cppcms
#include <iostream>
#include <fstream>
int main()
{
cppcms::impl::gzip_filter filter;
std::ifstream in("gzip_filter.cpp");
std::ofstream out("test.cpp.gz");
filter.output(out);
filter.open();
std::cerr << "A" << std::endl;
filter << in.rdbuf();
std::cerr << "B" << std::endl;
filter.close();
std::cerr << "C" << std::endl;
}
MongoDB Logo MongoDB