void ZlibCompressionWriteFilter::resetCompressionState() { if (mCompressionStateInited) { mZerr = deflateEnd(&mCstream); RCF_VERIFY( mZerr == Z_OK || mZerr == Z_DATA_ERROR, FilterException( _RcfError_Zlib(), mZerr, RcfSubsystem_Zlib, "deflateEnd() failed"))(mZerr); mCompressionStateInited = false; } mCstream.zalloc = NULL; mCstream.zfree = NULL; mCstream.opaque = NULL; mZerr = deflateInit(&mCstream, Z_DEFAULT_COMPRESSION); RCF_VERIFY( mZerr == Z_OK, FilterException( _RcfError_Zlib(), mZerr, RcfSubsystem_Zlib, "deflateInit() failed"))(mZerr); mCompressionStateInited = true; }
dtn::data::Number FilterContext::getBlockLength() const throw (FilterException) { if (_block == NULL) throw FilterException("attribute not present in this context"); return _block_length; }
const dtn::data::Block& FilterContext::getBlock() const throw (FilterException) { if (_block == NULL) throw FilterException("attribute not present in this context"); return *_block; }
const dtn::data::BundleID& FilterContext::getBundleID() const throw (FilterException) { if (_metabundle != NULL) return *_metabundle; if (_primaryblock != NULL) return *_primaryblock; throw FilterException("attribute not present in this context"); }
const dtn::data::Bundle& FilterContext::getBundle() const throw (FilterException) { if (_bundle == NULL) throw FilterException("attribute not present in this context"); return *_bundle; }
const std::string FilterContext::getRoutingTag() const throw (FilterException) { if (_routing == NULL) throw FilterException("attribute not present in this context"); return _routing->getTag(); }
dtn::core::Node::Protocol FilterContext::getProtocol() const throw (FilterException) { if (_protocol == dtn::core::Node::CONN_UNDEFINED) throw FilterException("attribute not present in this context"); return _protocol; }
const dtn::data::EID& FilterContext::setPeer() const throw (FilterException) { if (_peer == NULL) throw FilterException("attribute not present in this context"); return *_peer; }
void ZlibCompressionWriteFilter::compress() { mPostBuffers.resize(0); // TODO: buffer size std::size_t bufferSize = 2*(lengthByteBuffers(mPreBuffers)+7+7); std::size_t leftMargin = mPreBuffers.front().getLeftMargin(); if (mVecPtr.get() == NULL || !mVecPtr.unique()) { mVecPtr.reset( new ReallocBuffer()); } mVecPtr->resize(leftMargin + bufferSize); if (leftMargin > 0) { mPostBuffers.push_back( ByteBuffer( &(*mVecPtr)[0] + leftMargin, mVecPtr->size() - leftMargin, leftMargin, mVecPtr)); } else { mPostBuffers.push_back( ByteBuffer( &(*mVecPtr)[0], mVecPtr->size(), mVecPtr)); } ByteBuffer &outBuffer = mPostBuffers.back(); std::size_t outPos = 0; std::size_t outRemaining = outBuffer.getLength() - outPos; mTotalBytesIn = 0; mTotalBytesOut = 0; for (std::size_t i=0; i<mPreBuffers.size(); ++i) { RCF_ASSERT_LT(outPos , outBuffer.getLength()); ByteBuffer &inBuffer = mPreBuffers[i]; mCstream.next_in = (Bytef*) inBuffer.getPtr(); mCstream.avail_in = static_cast<uInt>(inBuffer.getLength()); mCstream.next_out = (Bytef*) &outBuffer.getPtr()[outPos]; mCstream.avail_out = static_cast<uInt>(outRemaining); mZerr = (i<mPreBuffers.size()-1) ? deflate(&mCstream, Z_NO_FLUSH) : deflate(&mCstream, Z_SYNC_FLUSH); RCF_VERIFY( mZerr == Z_OK || mZerr == Z_BUF_ERROR, FilterException( _RcfError_Zlib(), mZerr, RcfSubsystem_Zlib, "deflate() failed")) (mZerr)(inBuffer.getLength())(outBuffer.getLength()); RCF_ASSERT_GTEQ(mCstream.avail_out , 0); std::size_t bytesIn = inBuffer.getLength() - mCstream.avail_in; std::size_t bytesOut = outRemaining - mCstream.avail_out; mTotalBytesIn += bytesIn; mTotalBytesOut += bytesOut; outPos += bytesOut; outRemaining -= bytesOut; } if (!mStateful) { mCstream.next_in = NULL; mCstream.avail_in = 0; mCstream.next_out = (Bytef*) &outBuffer.getPtr()[outPos]; mCstream.avail_out = static_cast<uInt>(outRemaining); mZerr = deflate(&mCstream, Z_FINISH); RCF_VERIFY( mZerr == Z_BUF_ERROR || mZerr == Z_STREAM_END, FilterException( _RcfError_Zlib(), mZerr, RcfSubsystem_Zlib, "deflate() failed")) (mZerr)(outPos)(outRemaining); RCF_ASSERT_GT(mCstream.avail_out , 0); std::size_t bytesOut = outRemaining - mCstream.avail_out; mTotalBytesOut += bytesOut; outPos += bytesOut; outRemaining -= bytesOut; } mPreBuffers.resize(0); outBuffer = ByteBuffer(outBuffer, 0, mTotalBytesOut); }