CMS 3D CMS Logo

FileBlob.cc
Go to the documentation of this file.
3 
4 #include <fstream>
5 #include <iostream>
6 #include <memory>
7 
8 #include <string>
9 #include <zlib.h>
10 
11 FileBlob::FileBlob(const std::string& fname, bool zip) : isize(0) {
12  compressed = zip;
13  /*
14  std::cout << "isize = " << isize
15  << " zip = " << (zip? "true" : "false")
16  << std::endl;
17  */
18  if (isize == 0)
20  // std::cout << "isize = " << isize << std::endl;
21  blob.reserve(isize);
22  read(fname);
23 }
24 FileBlob::FileBlob(std::istream& is, bool zip) : isize(0) {
25  compressed = zip;
26  if (isize == 0)
28  blob.reserve(isize);
29  read(is);
30 }
31 
32 void FileBlob::read(std::istream& is) {
33  if (compressed) {
34  std::vector<unsigned char> in;
35  in.reserve(isize);
36  char c;
37  while (is.get(c))
38  in.push_back((unsigned char)c);
39  /*
40  for(int i=0;i<in.size();i++){
41  std::cout<<in[i];
42  }
43  std::cout<<std::endl;
44  */
45  blob.resize(isize);
46  uLongf destLen = compressBound(in.size());
47  int zerr = compress2(&*blob.begin(), &destLen, &*in.begin(), in.size(), 9);
48  if (zerr != 0)
49  edm::LogError("FileBlob") << "Compression error " << zerr;
50  blob.resize(destLen);
51  } else {
52  //std::cout << "reading uncompressed" << std::endl;
53  char c;
54  while (is.get(c))
55  blob.push_back((unsigned char)c);
56  blob.resize(blob.size());
57  isize = blob.size();
58  }
59 }
60 
61 void FileBlob::write(std::ostream& os) const {
62  if (compressed) {
63  std::vector<unsigned char> out(isize);
64  uLongf destLen = out.size();
65  int zerr = uncompress(&*out.begin(), &destLen, &*blob.begin(), blob.size());
66  if (zerr != 0 || out.size() != destLen)
67  edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
68  << destLen;
69  os.write(reinterpret_cast<const char*>(&*out.begin()), out.size());
70  } else {
71  os.write(reinterpret_cast<const char*>(&*blob.begin()), blob.size());
72  }
73 }
74 
75 std::unique_ptr<std::vector<unsigned char>> FileBlob::getUncompressedBlob() const {
76  std::unique_ptr<std::vector<unsigned char>> newblob;
77  if (compressed) {
78  newblob = std::make_unique<std::vector<unsigned char>>(isize);
79  uLongf destLen = newblob->size();
80  // std::cout<<"Store isize = "<<isize<<"; newblob->size() = "<<newblob->size()<<"; destLen = "<<destLen<<std::endl;
81  int zerr = uncompress(&*(newblob->begin()), &destLen, &*blob.begin(), blob.size());
82  if (zerr != 0 || newblob->size() != destLen)
83  edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
84  << destLen;
85  } else {
86  newblob = std::make_unique<std::vector<unsigned char>>(blob);
87  }
88  return newblob;
89 }
90 
91 void FileBlob::getUncompressedBlob(std::vector<unsigned char>& myblobcopy) const {
92  if (compressed) {
93  myblobcopy.reserve(isize);
94  uLongf destLen = isize;
95  int zerr = uncompress(&*myblobcopy.begin(), &destLen, &*blob.begin(), blob.size());
96  if (zerr != 0 || myblobcopy.size() != destLen)
97  edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
98  << destLen;
99  } else {
100  myblobcopy = blob;
101  }
102 }
103 
105  std::ifstream ifile(fname.c_str());
106  if (!ifile) {
107  edm::LogError("FileBlob") << "file " << fname << " does not exist...";
108  } else
109  read(ifile);
110  ifile.close();
111 }
112 
113 void FileBlob::write(const std::string& fname) const {
114  std::ofstream ofile(fname.c_str());
115  write(ofile);
116  ofile.close();
117 }
118 
120  unsigned int is = 0;
121  std::ifstream ifile(fname.c_str());
122  if (!ifile) {
123  edm::LogError("FileBlob") << "file " << fname << " does not exist...";
124  } else
125  is = computeStreamSize(ifile);
126  ifile.close();
127  return is;
128 }
129 
130 unsigned int FileBlob::computeStreamSize(std::istream& is) {
131  unsigned int rs = 0;
132  char c;
133  while (is.get(c))
134  rs++;
135  is.clear();
136  is.seekg(0);
137  return rs;
138 }
FileBlob::isize
unsigned int isize
Definition: FileBlob.h:49
MessageLogger.h
FileBlob::computeFileSize
static unsigned int computeFileSize(const std::string &)
Definition: FileBlob.cc:119
FileBlob::getUncompressedBlob
std::unique_ptr< std::vector< unsigned char > > getUncompressedBlob() const
i didn't want to do two copies ... hope this works.
Definition: FileBlob.cc:75
FileBlob.h
compare_using_db.ifile
ifile
Definition: compare_using_db.py:251
FileBlob::FileBlob
FileBlob()
Definition: FileBlob.h:13
FileBlob::blob
std::vector< unsigned char > blob
Definition: FileBlob.h:47
FileBlob::read
void read(const std::string &)
read from real file
Definition: FileBlob.cc:104
recoMuon::in
Definition: RecoMuonEnumerators.h:6
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ComparisonHelper::zip
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Definition: L1TStage2CaloLayer1.h:41
alignmentValidation.fname
string fname
main script
Definition: alignmentValidation.py:959
FileBlob::computeStreamSize
static unsigned int computeStreamSize(std::istream &)
Definition: FileBlob.cc:130
FileBlob::write
void write(const std::string &) const
write to real file
Definition: FileBlob.cc:113
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
c
auto & c
Definition: CAHitNtupletGeneratorKernelsImpl.h:56
spu::zerr
void zerr(int)
Definition: SherpackUtilities.cc:129
edm::Log
Definition: MessageLogger.h:70
FileBlob::compressed
bool compressed
Definition: FileBlob.h:48