CMS 3D CMS Logo

CStringStream.cc
Go to the documentation of this file.
1 #include <cassert>
2 #include <cstring>
3 #include <memory>
4 
5 #include "zlib.h"
6 
7 #include "Alignment/Geners/interface/BZ2Handle.hh"
8 #include "Alignment/Geners/interface/CStringStream.hh"
9 #include "Alignment/Geners/interface/IOException.hh"
10 
11 static void doZlibCompression(const char *data,
12  const unsigned long long len,
13  const bool defl,
14  z_stream_s &strm,
15  char *buffer,
16  const unsigned long long bufLen,
17  std::ostream &sink) {
18  assert(buffer);
19  assert(bufLen);
20 
21  int status = Z_OK;
22  strm.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(data));
23  strm.avail_in = len;
24  do {
25  strm.next_out = reinterpret_cast<Bytef *>(buffer);
26  strm.avail_out = bufLen;
27  status = defl ? deflate(&strm, Z_FINISH) : inflate(&strm, Z_NO_FLUSH);
28  assert(status == Z_OK || status == Z_STREAM_END);
29  const unsigned have = bufLen - strm.avail_out;
30  sink.write(buffer, have);
31  if (sink.fail())
32  throw gs::IOWriteFailure("In gs::doZlibCompression: sink stream failure");
33  } while (strm.avail_out == 0);
34 
35  if (defl) {
36  assert(strm.avail_in == 0);
37  assert(status == Z_STREAM_END);
38  assert(deflateReset(&strm) == Z_OK);
39  } else
40  assert(inflateReset(&strm) == Z_OK);
41 }
42 
43 static void doBZ2Compression(const char *data,
44  const unsigned long long len,
45  const bool defl,
46  bz_stream &strm,
47  char *buffer,
48  const unsigned long long bufLen,
49  std::ostream &sink) {
50  assert(buffer);
51  assert(bufLen);
52 
53  int status = BZ_OK;
54  strm.next_in = const_cast<char *>(data);
55  strm.avail_in = len;
56  do {
57  strm.next_out = buffer;
58  strm.avail_out = bufLen;
59  status = defl ? BZ2_bzCompress(&strm, BZ_FINISH) : BZ2_bzDecompress(&strm);
60  assert(status == BZ_OK || status == BZ_STREAM_END);
61  const unsigned have = bufLen - strm.avail_out;
62  sink.write(buffer, have);
63  if (sink.fail())
64  throw gs::IOWriteFailure("In gs::doBZ2Compression: sink stream failure");
65  } while (status != BZ_STREAM_END);
66 }
67 
68 namespace gs {
69  CStringStream::CStringStream(const CompressionMode m,
70  const int compressionLevel,
71  const unsigned minSizeToCompress,
72  const unsigned bufSize)
73  : mode_(m),
74  compressionLevel_(compressionLevel),
75  minSizeToCompress_(minSizeToCompress),
76  // Have a reasonable minimum buffer size to maintain
77  // performance even if the user wants to shoot himself
78  // in a foot
79  comprBuf_(bufSize > 1024U ? bufSize : 1024U),
80  sink_(nullptr) {
81  this->init(&buf_);
82  }
83 
84  void CStringStream::setCompressionMode(const CompressionMode newmode) {
85  reset();
86  mode_ = newmode;
87  }
88 
89  void CStringStream::reset() {
90  clear();
91  seekp(0);
92  seekg(0);
93  }
94 
95  void CStringStream::readCompressed(std::istream &in, const unsigned compressionCode, const unsigned long long len) {
96  reset();
97  if (!len)
98  return;
99 
100  // Decompress and dump to this string stream
101  if (len > readBuf_.size())
102  readBuf_.resize(len);
103  in.read(&readBuf_[0], len);
104 
105  switch (static_cast<CompressionMode>(compressionCode)) {
106  case NOT_COMPRESSED:
107  this->write(&readBuf_[0], len);
108  return;
109 
110  case ZLIB: {
111  if (!inflator_.get())
112  inflator_ = std::make_unique<ZlibInflateHandle>();
113  doZlibCompression(&readBuf_[0], len, false, inflator_->strm(), &comprBuf_[0], comprBuf_.size(), *this);
114  } break;
115 
116  case BZIP2: {
117  // bzlib2 can not be reset, so we have to make
118  // a new inflator every time
119  bz_stream strm;
120  BZ2InflateHandle h(strm);
121  doBZ2Compression(&readBuf_[0], len, false, strm, &comprBuf_[0], comprBuf_.size(), *this);
122  } break;
123 
124  default:
125  assert(!"Unhandled switch case in "
126  "CStringStream::readCompressed. "
127  "This is a bug. Please report.");
128  }
129  }
130 
131  CStringStream::CompressionMode CStringStream::writeCompressed() {
132  // Compress and dump to sink
133  assert(sink_);
134 
135  unsigned long long len = 0;
136  const char *data = buf_.getPutBuffer(&len);
137  if (len == 0)
138  return NOT_COMPRESSED;
139 
140  if (mode_ == NOT_COMPRESSED || len < minSizeToCompress_) {
141  sink_->write(data, len);
142  return NOT_COMPRESSED;
143  }
144 
145  switch (mode_) {
146  case ZLIB: {
147  if (!deflator_.get())
148  deflator_ = std::make_unique<ZlibDeflateHandle>(compressionLevel_);
149  doZlibCompression(data, len, true, deflator_->strm(), &comprBuf_[0], comprBuf_.size(), *sink_);
150  } break;
151 
152  case BZIP2: {
153  // bzlib2 can not be reset, so we have to make
154  // a new deflator every time
155  bz_stream strm;
156  BZ2DeflateHandle h(strm);
157  doBZ2Compression(data, len, true, strm, &comprBuf_[0], comprBuf_.size(), *sink_);
158  } break;
159 
160  default:
161  assert(!"Unhandled switch case in "
162  "CStringStream::writeCompressed. "
163  "This is a bug. Please report.");
164  }
165 
166  seekp(0);
167  return mode_;
168  }
169 
170  bool CStringStream::getCompressionModeByName(const char *name, CompressionMode *m) {
171  static const char *names[] = {"n", "z", "b"};
172  if (!name || !m)
173  return false;
174  for (unsigned i = 0; i < sizeof(names) / sizeof(names[0]); ++i)
175  if (strcasecmp(name, names[i]) == 0) {
176  *m = static_cast<CompressionMode>(i);
177  return true;
178  }
179  return false;
180  }
181 
182  std::string CStringStream::compressionModeName(const CompressionMode m, const bool useShortName) {
184  switch (m) {
185  case NOT_COMPRESSED:
186  mode = useShortName ? "n" : "not compressed";
187  break;
188  case ZLIB:
189  mode = useShortName ? "z" : "zlib";
190  break;
191  case BZIP2:
192  mode = useShortName ? "b" : "bzip2";
193  break;
194  default:
195  assert(!"Unhandled switch case in "
196  "CStringStream::compressionModeName. "
197  "This is a bug. Please report.");
198  }
199  return mode;
200  }
201 } // namespace gs
static void doBZ2Compression(const char *data, const unsigned long long len, const bool defl, bz_stream &strm, char *buffer, const unsigned long long bufLen, std::ostream &sink)
int init
Definition: HydjetWrapper.h:66
assert(be >=bs)
const std::string names[nVars_]
static void doZlibCompression(const char *data, const unsigned long long len, const bool defl, z_stream_s &strm, char *buffer, const unsigned long long bufLen, std::ostream &sink)
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
Definition: AbsArchive.cc:46
void clear(EGIsoObj &c)
Definition: egamma.h:82
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void reset(double vett[256])
Definition: TPedValues.cc:11