CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/CondFormats/Common/src/MultiFileBlob.cc

Go to the documentation of this file.
00001 #include "CondFormats/Common/interface/MultiFileBlob.h"
00002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00003 
00004 #include <iostream>
00005 #include <zlib.h>
00006 
00007 MultiFileBlob::MultiFileBlob() : 
00008   compressed(false), isize(0), expanded(false) {}
00009 
00010 MultiFileBlob::~MultiFileBlob() {}
00011 
00012 void MultiFileBlob::finalized(bool compress) {
00013   if (!compress) return;
00014   if (0==isize) return;
00015   compressed=true;
00016   expanded=false;
00017   std::vector<unsigned char> out(isize);
00018   uLongf destLen = compressBound(isize);
00019   int zerr =  compress2(&out.front(), &destLen,
00020                         &blob.front(), isize,
00021                         9);
00022     if (zerr!=0) edm::LogError("MultiFileBlob")<< "Compression error " << zerr;
00023     out.resize(destLen);
00024     blob.swap(out);
00025 }
00026 
00027  
00028 void MultiFileBlob::read(const std::string& name, std::istream & is) {
00029   Positions::const_iterator pos = positions.find(name);
00030   if (pos!=positions.end()) {
00031     edm::LogError("MultiFileBlob:")<< name << "already in this object";
00032     return;
00033   }
00034   positions[name]=isize;
00035   char c;
00036   while (is.get(c))
00037     blob.push_back( (unsigned char)c);
00038   isize=blob.size();
00039 }
00040 
00041 void MultiFileBlob::write(const std::string& name, std::ostream & os) const {
00042   Range r = rawBlob(name);
00043   os.write((const char *)(r.first),r.second-r.first);
00044 
00045 }
00046 
00047 MultiFileBlob::Range MultiFileBlob::rawBlob(const std::string& name) const {
00048   const_cast<MultiFileBlob*>(this)->expand();
00049   Positions::const_iterator pos = positions.find(name);
00050   if (pos==positions.end()) {
00051     edm::LogError("MultiFileBlob:")<< name << "not in this object";
00052     return Range(0,0);
00053   }
00054   unsigned long long b = (*pos).second;
00055   unsigned long long e = isize;
00056   pos++;
00057   if (pos!=positions.end()) e= (*pos).second;
00058   
00059   return Range(&blob[b],&blob[e]);
00060 }
00061 
00062 
00063 unsigned long long MultiFileBlob::size(const std::string& name) const {
00064   Range r = rawBlob(name);
00065   return r.second-r.first;
00066 }
00067 
00068 void MultiFileBlob::expand() {
00069   if (expanded) return;
00070   if (!compressed) {
00071     expanded=true;
00072     return;
00073   }
00074   std::vector<unsigned char> out(isize);
00075   uLongf destLen = out.size();
00076   int zerr =  uncompress(&out.front(),  &destLen,
00077                          &blob.front(), blob.size());
00078   if (zerr!=0 || out.size()!=destLen) 
00079     edm::LogError("FileBlob")<< "uncompressing error " << zerr
00080                              << " original size was " << isize
00081                              << " new size is " << destLen;
00082   blob.swap(out);
00083   expanded=true;
00084 }
00085