CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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, const unsigned long long len,
11  const bool defl, z_stream_s& strm,
12  char* buffer, const unsigned long long bufLen,
13  std::ostream& sink)
14 {
15  assert(buffer);
16  assert(bufLen);
17 
18  int status = Z_OK;
19  strm.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(data));
20  strm.avail_in = len;
21  do
22  {
23  strm.next_out = reinterpret_cast<Bytef*>(buffer);
24  strm.avail_out = bufLen;
25  status = defl ? deflate(&strm, Z_FINISH) :
26  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()) throw gs::IOWriteFailure(
31  "In gs::doZlibCompression: sink stream failure");
32  } while (strm.avail_out == 0);
33 
34  if (defl)
35  {
36  assert(strm.avail_in == 0);
37  assert(status == Z_STREAM_END);
38  assert(deflateReset(&strm) == Z_OK);
39  }
40  else
41  assert(inflateReset(&strm) == Z_OK);
42 }
43 
44 
45 static void doBZ2Compression(const char* data, const unsigned long long len,
46  const bool defl, bz_stream& strm,
47  char* buffer, const unsigned long long bufLen,
48  std::ostream& sink)
49 {
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  {
58  strm.next_out = buffer;
59  strm.avail_out = bufLen;
60  status = defl ? BZ2_bzCompress(&strm, BZ_FINISH) :
61  BZ2_bzDecompress(&strm);
62  assert(status == BZ_OK || status == BZ_STREAM_END);
63  const unsigned have = bufLen - strm.avail_out;
64  sink.write(buffer, have);
65  if (sink.fail()) throw gs::IOWriteFailure(
66  "In gs::doBZ2Compression: sink stream failure");
67  } while (status != BZ_STREAM_END);
68 }
69 
70 
71 namespace gs {
72  CStringStream::CStringStream(const CompressionMode m,
73  const int compressionLevel,
74  const unsigned minSizeToCompress,
75  const unsigned bufSize)
76  : mode_(m),
77  compressionLevel_(compressionLevel),
78  minSizeToCompress_(minSizeToCompress),
79  // Have a reasonable minimum buffer size to maintain
80  // performance even if the user wants to shoot himself
81  // in a foot
82  comprBuf_(bufSize > 1024U ? bufSize : 1024U),
83  sink_(0)
84  {
85  this->init(&buf_);
86  }
87 
88  void CStringStream::setCompressionMode(const CompressionMode newmode)
89  {
90  reset();
91  mode_ = newmode;
92  }
93 
95  {
96  clear();
97  seekp(0);
98  seekg(0);
99  }
100 
101  void CStringStream::readCompressed(std::istream& in,
102  const unsigned compressionCode,
103  const unsigned long long len)
104  {
105  reset();
106  if (!len)
107  return;
108 
109  // Decompress and dump to this string stream
110  if (len > readBuf_.size())
111  readBuf_.resize(len);
112  in.read(&readBuf_[0], len);
113 
114  switch (static_cast<CompressionMode>(compressionCode))
115  {
116  case NOT_COMPRESSED:
117  this->write(&readBuf_[0], len);
118  return;
119 
120  case ZLIB:
121  {
122  if (!inflator_.get())
123  inflator_ = CPP11_auto_ptr<ZlibInflateHandle>(
124  new ZlibInflateHandle());
125  doZlibCompression(&readBuf_[0], len, false, inflator_->strm(),
126  &comprBuf_[0], comprBuf_.size(), *this);
127  }
128  break;
129 
130  case BZIP2:
131  {
132  // bzlib2 can not be reset, so we have to make
133  // a new inflator every time
134  bz_stream strm;
135  BZ2InflateHandle h(strm);
136  doBZ2Compression(&readBuf_[0], len, false, strm,
137  &comprBuf_[0], comprBuf_.size(), *this);
138  }
139  break;
140 
141  default:
142  assert(!"Unhandled switch case in "
143  "CStringStream::readCompressed. "
144  "This is a bug. Please report.");
145  }
146  }
147 
148  CStringStream::CompressionMode CStringStream::writeCompressed()
149  {
150  // Compress and dump to sink
151  assert(sink_);
152 
153  unsigned long long len = 0;
154  const char* data = buf_.getPutBuffer(&len);
155  if (len == 0)
156  return NOT_COMPRESSED;
157 
158  if (mode_ == NOT_COMPRESSED || len < minSizeToCompress_)
159  {
160  sink_->write(data, len);
161  return NOT_COMPRESSED;
162  }
163 
164  switch (mode_)
165  {
166  case ZLIB:
167  {
168  if (!deflator_.get())
169  deflator_ = CPP11_auto_ptr<ZlibDeflateHandle>(
170  new ZlibDeflateHandle(compressionLevel_));
171  doZlibCompression(data, len, true, deflator_->strm(),
172  &comprBuf_[0], comprBuf_.size(), *sink_);
173  }
174  break;
175 
176  case BZIP2:
177  {
178  // bzlib2 can not be reset, so we have to make
179  // a new deflator every time
180  bz_stream strm;
181  BZ2DeflateHandle h(strm);
182  doBZ2Compression(data, len, true, strm,
183  &comprBuf_[0], comprBuf_.size(), *sink_);
184  }
185  break;
186 
187  default:
188  assert(!"Unhandled switch case in "
189  "CStringStream::writeCompressed. "
190  "This is a bug. Please report.");
191  }
192 
193  seekp(0);
194  return mode_;
195  }
196 
197  bool CStringStream::getCompressionModeByName(const char* name,
198  CompressionMode* m)
199  {
200  static const char* names[] = {
201  "n",
202  "z",
203  "b"
204  };
205  if (!name || !m)
206  return false;
207  for (unsigned i=0; i<sizeof(names)/sizeof(names[0]); ++i)
208  if (strcasecmp(name, names[i]) == 0)
209  {
210  *m = static_cast<CompressionMode>(i);
211  return true;
212  }
213  return false;
214  }
215 
216  std::string CStringStream::compressionModeName(const CompressionMode m,
217  const bool useShortName)
218  {
220  switch (m)
221  {
222  case NOT_COMPRESSED:
223  mode = useShortName ? "n" : "not compressed";
224  break;
225  case ZLIB:
226  mode = useShortName ? "z" : "zlib";
227  break;
228  case BZIP2:
229  mode = useShortName ? "b" : "bzip2";
230  break;
231  default:
232  assert(!"Unhandled switch case in "
233  "CStringStream::compressionModeName. "
234  "This is a bug. Please report.");
235  }
236  return mode;
237  }
238 }
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 i
Definition: DBlmapReader.cc:9
static const HistoName names[]
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
assert(m_qm.get())
int init
Definition: HydjetWrapper.h:67
static int const bufSize
Definition: Guid.cc:24
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)
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
void reset(double vett[256])
Definition: TPedValues.cc:11
tuple status
Definition: mps_update.py:57