File indexing completed on 2024-04-06 12:01:58
0001 #include "CondFormats/Common/interface/FileBlob.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003
0004 #include <fstream>
0005 #include <iostream>
0006 #include <memory>
0007
0008 #include <string>
0009 #include <zlib.h>
0010
0011 FileBlob::FileBlob(const std::string& fname, bool zip) : isize(0) {
0012 compressed = zip;
0013
0014
0015
0016
0017
0018 if (isize == 0)
0019 isize = computeFileSize(fname);
0020
0021 blob.reserve(isize);
0022 read(fname);
0023 }
0024 FileBlob::FileBlob(std::istream& is, bool zip) : isize(0) {
0025 compressed = zip;
0026 if (isize == 0)
0027 isize = computeStreamSize(is);
0028 blob.reserve(isize);
0029 read(is);
0030 }
0031
0032 void FileBlob::read(std::istream& is) {
0033 if (compressed) {
0034 std::vector<unsigned char> in;
0035 in.reserve(isize);
0036 char c;
0037 while (is.get(c))
0038 in.push_back((unsigned char)c);
0039
0040
0041
0042
0043
0044
0045 uLongf destLen = compressBound(in.size());
0046 blob.resize(destLen);
0047 int zerr = compress2(&*blob.begin(), &destLen, &*in.begin(), in.size(), 9);
0048 if (zerr != 0)
0049 edm::LogError("FileBlob") << "Compression error " << zerr;
0050 blob.resize(destLen);
0051 } else {
0052
0053 char c;
0054 while (is.get(c))
0055 blob.push_back((unsigned char)c);
0056 blob.resize(blob.size());
0057 isize = blob.size();
0058 }
0059 }
0060
0061 void FileBlob::write(std::ostream& os) const {
0062 if (compressed) {
0063 std::vector<unsigned char> out(isize);
0064 uLongf destLen = out.size();
0065 int zerr = uncompress(&*out.begin(), &destLen, &*blob.begin(), blob.size());
0066 if (zerr != 0 || out.size() != destLen)
0067 edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
0068 << destLen;
0069 os.write(reinterpret_cast<const char*>(&*out.begin()), out.size());
0070 } else {
0071 os.write(reinterpret_cast<const char*>(&*blob.begin()), blob.size());
0072 }
0073 }
0074
0075 std::unique_ptr<std::vector<unsigned char>> FileBlob::getUncompressedBlob() const {
0076 std::unique_ptr<std::vector<unsigned char>> newblob;
0077 if (compressed) {
0078 newblob = std::make_unique<std::vector<unsigned char>>(isize);
0079 uLongf destLen = newblob->size();
0080
0081 int zerr = uncompress(&*(newblob->begin()), &destLen, &*blob.begin(), blob.size());
0082 if (zerr != 0 || newblob->size() != destLen)
0083 edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
0084 << destLen;
0085 } else {
0086 newblob = std::make_unique<std::vector<unsigned char>>(blob);
0087 }
0088 return newblob;
0089 }
0090
0091 void FileBlob::getUncompressedBlob(std::vector<unsigned char>& myblobcopy) const {
0092 if (compressed) {
0093 myblobcopy.reserve(isize);
0094 uLongf destLen = isize;
0095 int zerr = uncompress(&*myblobcopy.begin(), &destLen, &*blob.begin(), blob.size());
0096 if (zerr != 0 || myblobcopy.size() != destLen)
0097 edm::LogError("FileBlob") << "uncompressing error " << zerr << " original size was " << isize << " new size is "
0098 << destLen;
0099 } else {
0100 myblobcopy = blob;
0101 }
0102 }
0103
0104 void FileBlob::read(const std::string& fname) {
0105 std::ifstream ifile(fname.c_str());
0106 if (!ifile) {
0107 edm::LogError("FileBlob") << "file " << fname << " does not exist...";
0108 } else
0109 read(ifile);
0110 ifile.close();
0111 }
0112
0113 void FileBlob::write(const std::string& fname) const {
0114 std::ofstream ofile(fname.c_str());
0115 write(ofile);
0116 ofile.close();
0117 }
0118
0119 unsigned int FileBlob::computeFileSize(const std::string& fname) {
0120 unsigned int is = 0;
0121 std::ifstream ifile(fname.c_str());
0122 if (!ifile) {
0123 edm::LogError("FileBlob") << "file " << fname << " does not exist...";
0124 } else
0125 is = computeStreamSize(ifile);
0126 ifile.close();
0127 return is;
0128 }
0129
0130 unsigned int FileBlob::computeStreamSize(std::istream& is) {
0131 unsigned int rs = 0;
0132 char c;
0133 while (is.get(c))
0134 rs++;
0135 is.clear();
0136 is.seekg(0);
0137 return rs;
0138 }