25 #include <ibrcommon/data/BLOB.h>
59 stream << _algorithm << _origin_size;
60 len -= _algorithm.
getLength() + _origin_size.getLength();
67 stream >> _origin_size;
94 if (p_it == b.
end())
throw ibrcommon::Exception(
"Payload block missing.");
98 ibrcommon::BLOB::Reference ref = ibrcommon::BLOB::create();
103 ibrcommon::BLOB::iostream is = p.
getBLOB().iostream();
104 ibrcommon::BLOB::iostream os = ref.iostream();
131 if (p_it == b.
end())
throw ibrcommon::Exception(
"Payload block missing.");
135 ibrcommon::BLOB::Reference ref = ibrcommon::BLOB::create();
140 ibrcommon::BLOB::iostream is = p.
getBLOB().iostream();
141 ibrcommon::BLOB::iostream os = ref.iostream();
164 const uInt CHUNK_SIZE = 16384;
168 unsigned char in[CHUNK_SIZE];
169 unsigned char out[CHUNK_SIZE];
173 strm.zalloc = Z_NULL;
175 strm.opaque = Z_NULL;
176 ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
179 if (ret != Z_OK)
throw ibrcommon::Exception(
"initialization of zlib failed");
182 is.read((
char*)&in, CHUNK_SIZE);
183 strm.avail_in =
static_cast<uInt
>(is.gcount());
185 flush = is.eof() ? Z_FINISH : Z_NO_FLUSH;
189 strm.avail_out = CHUNK_SIZE;
192 ret = deflate(&strm, flush);
193 assert(ret != Z_STREAM_ERROR);
196 have = CHUNK_SIZE - strm.avail_out;
199 os.write((
char*)&out, have);
203 (void)deflateEnd(&strm);
204 throw ibrcommon::Exception(
"decompression failed. output stream went wrong.");
207 }
while (strm.avail_out == 0);
208 assert(strm.avail_in == 0);
209 }
while (flush != Z_FINISH);
210 assert(ret == Z_STREAM_END);
212 (void)deflateEnd(&strm);
214 throw ibrcommon::Exception(
"zlib is not supported");
220 throw ibrcommon::Exception(
"compression mode is not supported");
231 const uInt CHUNK_SIZE = 16384;
235 unsigned char in[CHUNK_SIZE];
236 unsigned char out[CHUNK_SIZE];
239 strm.zalloc = Z_NULL;
241 strm.opaque = Z_NULL;
243 strm.next_in = Z_NULL;
244 ret = inflateInit(&strm);
247 if (ret != Z_OK)
throw ibrcommon::Exception(
"initialization of zlib failed");
250 is.read((
char*)&in, CHUNK_SIZE);
251 strm.avail_in =
static_cast<uInt
>(is.gcount());
254 if ((strm.avail_in == 0) && (ret != Z_STREAM_END))
256 (void)inflateEnd(&strm);
257 throw ibrcommon::Exception(
"decompression failed. no enough data available.");
262 strm.avail_out = CHUNK_SIZE;
265 ret = inflate(&strm, Z_NO_FLUSH);
266 assert(ret != Z_STREAM_ERROR);
274 (void)inflateEnd(&strm);
275 throw ibrcommon::Exception(
"decompression failed. memory error.");
279 have = CHUNK_SIZE - strm.avail_out;
282 os.write((
char*)&out, have);
287 (void)inflateEnd(&strm);
288 throw ibrcommon::Exception(
"decompression failed. output stream went wrong.");
291 }
while (strm.avail_out == 0);
292 assert(strm.avail_in == 0);
293 }
while (ret != Z_STREAM_END);
295 (void)inflateEnd(&strm);
297 throw ibrcommon::Exception(
"zlib is not supported");
303 throw ibrcommon::Exception(
"compression mode is not supported");
virtual ~CompressedPayloadBlock()
static void extract(dtn::data::Bundle &b)
static const dtn::data::block_t BLOCK_TYPE
void remove(const Block &block)
T & insert(iterator before)
static void compress(dtn::data::Bundle &b, COMPRESS_ALGS alg)
const Number & getOriginSize() const
virtual Length getLength() const
virtual dtn::data::Block * create()
COMPRESS_ALGS getAlgorithm() const
block_list::iterator iterator
virtual Length getLength() const
static const dtn::data::block_t BLOCK_TYPE
virtual std::ostream & serialize(std::ostream &stream, Length &length) const
iterator find(block_t blocktype)
ibrcommon::BLOB::Reference getBLOB() const
void setOriginSize(const Number &s)
void setAlgorithm(COMPRESS_ALGS alg)
virtual std::istream & deserialize(std::istream &stream, const Length &length)