CMS 3D CMS Logo

LHEXMLStringProduct.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <algorithm>
3 #include <lzma.h>
4 
6 
8 
9 using namespace edm;
10 using namespace std;
11 
13 
14 LHEXMLStringProduct::LHEXMLStringProduct(const string &onelheoutput) : content_() { content_.push_back(onelheoutput); }
15 
17 
18 void LHEXMLStringProduct::fillCompressedContent(std::istream &input, unsigned int initialSize) {
19  //create blob with desired size
20  compressedContent_.emplace_back(initialSize);
21  std::vector<uint8_t> &output = compressedContent_.back();
22 
23  //read buffer
24  constexpr unsigned int bufsize = 4096;
25  char inbuf[bufsize];
26 
27  const unsigned int threshsize = 32 * 1024 * 1024;
28 
29  //initialize lzma
30  uint32_t preset = 9;
31  lzma_stream strm = LZMA_STREAM_INIT;
32  lzma_ret ret = lzma_easy_encoder(&strm, preset, LZMA_CHECK_CRC64);
33 
34  lzma_action action = LZMA_RUN;
35 
36  strm.next_in = reinterpret_cast<uint8_t *>(&inbuf[0]);
37  strm.avail_in = 0;
38  strm.next_out = output.data();
39  strm.avail_out = output.size();
40 
41  unsigned int compressedSize = 0;
42 
43  while (ret == LZMA_OK) {
44  //read input to buffer if necessary
45  if (strm.avail_in == 0 && !input.eof()) {
46  input.read(inbuf, bufsize);
47  strm.next_in = reinterpret_cast<uint8_t *>(&inbuf[0]);
48  strm.avail_in = input.gcount();
49  if (input.eof()) {
50  //signal to lzma that there is no more input
51  action = LZMA_FINISH;
52  }
53  }
54 
55  //actual compression
56  ret = lzma_code(&strm, action);
57 
58  //update compressed size
59  compressedSize = output.size() - strm.avail_out;
60 
61  //if output blob is full and compression is still going, allocate more memory
62  if (strm.avail_out == 0 && ret == LZMA_OK) {
63  unsigned int oldsize = output.size();
64  if (oldsize < threshsize) {
65  output.resize(2 * oldsize);
66  } else {
67  output.resize(oldsize + threshsize);
68  }
69  strm.next_out = &output[oldsize];
70  strm.avail_out = output.size() - oldsize;
71  }
72  }
73 
74  lzma_end(&strm);
75 
76  if (ret != LZMA_STREAM_END) {
77  throw cms::Exception("CompressionError") << "There was a failure in LZMA compression in LHEXMLStringProduct.";
78  }
79 
80  //trim output blob
81  output.resize(compressedSize);
82 }
83 
84 void LHEXMLStringProduct::writeCompressedContent(std::ostream &output, unsigned int i) const {
85  //initialize lzma
86  lzma_stream strm = LZMA_STREAM_INIT;
87  lzma_ret ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
88  //all output available from the start, so start "close out" immediately
89  lzma_action action = LZMA_FINISH;
90 
91  //write buffer
92  constexpr unsigned int bufsize = 4096;
93  char outbuf[bufsize];
94 
95  const std::vector<uint8_t> &input = compressedContent_[i];
96 
97  strm.next_in = input.data();
98  strm.avail_in = input.size();
99  strm.next_out = reinterpret_cast<uint8_t *>(&outbuf[0]);
100  strm.avail_out = bufsize;
101 
102  while (ret == LZMA_OK) {
103  ret = lzma_code(&strm, action);
104 
105  //write to stream
106  output.write(outbuf, bufsize - strm.avail_out);
107 
108  //output buffer full, recycle
109  if (strm.avail_out == 0 && ret == LZMA_OK) {
110  strm.next_out = reinterpret_cast<uint8_t *>(&outbuf[0]);
111  strm.avail_out = bufsize;
112  }
113  }
114 
115  lzma_end(&strm);
116 
117  if (ret != LZMA_STREAM_END) {
118  throw cms::Exception("DecompressionError") << "There was a failure in LZMA decompression in LHEXMLStringProduct.";
119  }
120 }
121 
123  content_.insert(content_.end(), other.getStrings().begin(), other.getStrings().end());
124  compressedContent_.insert(compressedContent_.end(), other.getCompressed().begin(), other.getCompressed().end());
125  return true;
126 }
127 
129  content_.swap(other.content_);
130  compressedContent_.swap(other.compressedContent_);
131 }
void swap(LHEXMLStringProduct &other)
ret
prodAgent to be discontinued
static std::string const input
Definition: EdmProvDump.cc:50
void writeCompressedContent(std::ostream &output, unsigned int i) const
std::vector< std::vector< uint8_t > > compressedContent_
HLT enums.
std::vector< std::string > content_
Definition: output.py:1
bool mergeProduct(LHEXMLStringProduct const &other)
void fillCompressedContent(std::istream &input, unsigned int initialSize=4 *1024 *1024)