CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MultiFileBlob.cc
Go to the documentation of this file.
3 
4 #include <iostream>
5 #include <zlib.h>
6 
7 MultiFileBlob::MultiFileBlob() : compressed(false), isize(0), expanded(false) {}
8 
10 
11 void MultiFileBlob::finalized(bool compress) {
12  if (!compress)
13  return;
14  if (0 == isize)
15  return;
16  compressed = true;
17  expanded = false;
18  std::vector<unsigned char> out(isize);
19  uLongf destLen = compressBound(isize);
20  int zerr = compress2(&out.front(), &destLen, &blob.front(), isize, 9);
21  if (zerr != 0)
22  edm::LogError("MultiFileBlob") << "Compression error " << zerr;
23  out.resize(destLen);
24  blob.swap(out);
25 }
26 
27 void MultiFileBlob::read(const std::string& name, std::istream& is) {
28  Positions::const_iterator pos = positions.find(name);
29  if (pos != positions.end()) {
30  edm::LogError("MultiFileBlob:") << name << "already in this object";
31  return;
32  }
33  positions[name] = isize;
34  char c;
35  while (is.get(c))
36  blob.push_back((unsigned char)c);
37  isize = blob.size();
38 }
39 
40 void MultiFileBlob::write(const std::string& name, std::ostream& os) const {
41  Range r = rawBlob(name);
42  os.write((const char*)(r.first), r.second - r.first);
43 }
44 
46  const_cast<MultiFileBlob*>(this)->expand();
47  Positions::const_iterator pos = positions.find(name);
48  if (pos == positions.end()) {
49  edm::LogError("MultiFileBlob:") << name << "not in this object";
50  return Range(nullptr, nullptr);
51  }
52  unsigned long long b = (*pos).second;
53  unsigned long long e = isize;
54  pos++;
55  if (pos != positions.end())
56  e = (*pos).second;
57 
58  return Range(&blob[b], &blob[e]);
59 }
60 
61 unsigned long long MultiFileBlob::size(const std::string& name) const {
62  Range r = rawBlob(name);
63  return r.second - r.first;
64 }
65 
67  if (expanded)
68  return;
69  if (!compressed) {
70  expanded = true;
71  return;
72  }
73  std::vector<unsigned char> out(isize);
74  uLongf destLen = out.size();
75  int zerr = uncompress(&out.front(), &destLen, &blob.front(), blob.size());
76  if (zerr != 0 || out.size() != destLen)
77  edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
78  << destLen;
79  blob.swap(out);
80  expanded = true;
81 }
void write(const std::string &name, std::ostream &os) const
write to ostream
const edm::EventSetup & c
void zerr(int)
std::pair< unsigned char const *, unsigned char const * > Range
Definition: MultiFileBlob.h:13
std::vector< unsigned char > blob
Definition: MultiFileBlob.h:50
void finalized(bool compress)
void read(const std::string &name, std::istream &is)
read from real file give it name name
Log< level::Error, false > LogError
unsigned long long size(const std::string &name) const
unsigned long long isize
Definition: MultiFileBlob.h:54
Positions positions
Definition: MultiFileBlob.h:52
double b
Definition: hdecay.h:118
Range rawBlob(const std::string &name) const