File indexing completed on 2024-04-06 12:01:58
0001 #include "CondFormats/Common/interface/MultiFileBlob.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003
0004 #include <iostream>
0005 #include <zlib.h>
0006
0007 MultiFileBlob::MultiFileBlob() : compressed(false), isize(0), expanded(false) {}
0008
0009 MultiFileBlob::~MultiFileBlob() {}
0010
0011 void MultiFileBlob::finalized(bool compress) {
0012 if (!compress)
0013 return;
0014 if (0 == isize)
0015 return;
0016 compressed = true;
0017 expanded = false;
0018 std::vector<unsigned char> out(isize);
0019 uLongf destLen = compressBound(isize);
0020 int zerr = compress2(&out.front(), &destLen, &blob.front(), isize, 9);
0021 if (zerr != 0)
0022 edm::LogError("MultiFileBlob") << "Compression error " << zerr;
0023 out.resize(destLen);
0024 blob.swap(out);
0025 }
0026
0027 void MultiFileBlob::read(const std::string& name, std::istream& is) {
0028 Positions::const_iterator pos = positions.find(name);
0029 if (pos != positions.end()) {
0030 edm::LogError("MultiFileBlob:") << name << "already in this object";
0031 return;
0032 }
0033 positions[name] = isize;
0034 char c;
0035 while (is.get(c))
0036 blob.push_back((unsigned char)c);
0037 isize = blob.size();
0038 }
0039
0040 void MultiFileBlob::write(const std::string& name, std::ostream& os) const {
0041 Range r = rawBlob(name);
0042 os.write((const char*)(r.first), r.second - r.first);
0043 }
0044
0045 MultiFileBlob::Range MultiFileBlob::rawBlob(const std::string& name) const {
0046 const_cast<MultiFileBlob*>(this)->expand();
0047 Positions::const_iterator pos = positions.find(name);
0048 if (pos == positions.end()) {
0049 edm::LogError("MultiFileBlob:") << name << "not in this object";
0050 return Range(nullptr, nullptr);
0051 }
0052 unsigned long long b = (*pos).second;
0053 unsigned long long e = isize;
0054 pos++;
0055 if (pos != positions.end())
0056 e = (*pos).second;
0057
0058 return Range(&blob[b], &blob[e]);
0059 }
0060
0061 unsigned long long MultiFileBlob::size(const std::string& name) const {
0062 Range r = rawBlob(name);
0063 return r.second - r.first;
0064 }
0065
0066 void MultiFileBlob::expand() {
0067 if (expanded)
0068 return;
0069 if (!compressed) {
0070 expanded = true;
0071 return;
0072 }
0073 std::vector<unsigned char> out(isize);
0074 uLongf destLen = out.size();
0075 int zerr = uncompress(&out.front(), &destLen, &blob.front(), blob.size());
0076 if (zerr != 0 || out.size() != destLen)
0077 edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
0078 << destLen;
0079 blob.swap(out);
0080 expanded = true;
0081 }