CMS 3D CMS Logo

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