CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Private Attributes | Static Private Attributes
lhef::StorageInputStream Class Reference

#include <XMLUtils.h>

Inheritance diagram for lhef::StorageInputStream:

Public Types

typedef StorageWrap Stream_t
 

Public Member Functions

virtual XMLFilePos curPos () const override
 
virtual const XMLCh * getContentType () const override
 
virtual XMLSize_t readBytes (XMLByte *const buf, const XMLSize_t size) override
 
 StorageInputStream (StorageWrap &in)
 
virtual ~StorageInputStream ()
 

Private Attributes

std::vector< uint8_t > buffer_
 
unsigned int buffLoc_ = 0
 
unsigned int buffTotal_ = 0
 
bool compression_
 
StorageWrapin
 
unsigned int lasttotal_
 
lzma_stream lstr
 
unsigned int pos
 

Static Private Attributes

static constexpr unsigned bufferSize_ = 16*1024*1024
 

Detailed Description

Definition at line 173 of file XMLUtils.h.

Member Typedef Documentation

Definition at line 176 of file XMLUtils.h.

Constructor & Destructor Documentation

lhef::StorageInputStream::StorageInputStream ( StorageWrap in)

Definition at line 207 of file XMLUtils.cc.

References buffer_, bufferSize_, compression_, Exception, RecoTauValidation_cfi::header, lstr, NULL, Storage::position(), Storage::read(), and Storage::SET.

207  :
208  in(in),
209  lstr(LZMA_STREAM_INIT),
210  compression_(false),
211  lasttotal_(0)
212 {
213  buffer_.reserve(bufferSize_);
214  // Check the kind of file.
215  char header[6];
216  /*unsigned int s = */ in->read(header, 6);
217  in->position(0, Storage::SET);
218  // Let's use lzma to start with.
219  if (header[1] == '7'
220  && header[2] == 'z'
221  && header[3] == 'X'
222  && header[4] == 'Z')
223  {
224  compression_ = true;
225  lstr = LZMA_STREAM_INIT;
226  // We store the beginning of the outBuffer to make sure
227  // we can always update previous results.
228 
229 #if LZMA_VERSION <= UINT32_C(49990030)
230  int ret = lzma_auto_decoder(&lstr, NULL, NULL);
231 #else
232  int ret = lzma_auto_decoder(&lstr, -1, 0);
233 #endif
234 
235  if (ret != LZMA_OK)
236  {
237  lzma_end(&lstr);
238  throw cms::Exception("IO") << "Error while reading compressed LHE file";
239  }
240  }
241 }
virtual IOSize read(void *into, IOSize n, IOOffset pos)
Definition: Storage.cc:17
#define NULL
Definition: scimark2.h:8
unsigned int lasttotal_
Definition: XMLUtils.h:193
virtual IOOffset position(void) const
Definition: Storage.cc:95
static constexpr unsigned bufferSize_
Definition: XMLUtils.h:197
StorageWrap & in
Definition: XMLUtils.h:189
std::vector< uint8_t > buffer_
Definition: XMLUtils.h:196
lhef::StorageInputStream::~StorageInputStream ( )
virtual

Definition at line 243 of file XMLUtils.cc.

References lstr.

244 {
245  lzma_end(&(lstr));
246 }

Member Function Documentation

virtual XMLFilePos lhef::StorageInputStream::curPos ( ) const
inlineoverridevirtual

Definition at line 181 of file XMLUtils.h.

References findQualityFiles::size.

181 { return pos; }
virtual const XMLCh* lhef::StorageInputStream::getContentType ( ) const
inlineoverridevirtual

Definition at line 186 of file XMLUtils.h.

186 { return 0; }
XMLSize_t lhef::StorageInputStream::readBytes ( XMLByte *const  buf,
const XMLSize_t  size 
)
overridevirtual

Definition at line 249 of file XMLUtils.cc.

References buffer_, buffLoc_, buffTotal_, compression_, Storage::CURRENT, Exception, edm::errors::FileReadError, in, lstr, min(), Storage::position(), Storage::read(), and findQualityFiles::size.

250 {
251  // Compression code is not able to handle sizeof(XMLByte) > 1.
252  assert(sizeof(XMLByte) == sizeof(unsigned char));
253 
254  if (! (buffLoc_ < buffTotal_) )
255  {
256  int rd = in->read((void*)&buffer_[0], buffer_.capacity());
257  // Storage layer is supposed to throw exceptions instead of returning errors; just-in-case
258  if (rd < 0)
259  {
261  ex << "Error while reading buffered LHE file";
262  throw ex;
263  }
264  buffLoc_=0;
265  buffTotal_=rd;
266  if (buffTotal_ == 0)
267  {
268  return 0;
269  }
270  }
271  unsigned int dataRead;
272  if (!compression_)
273  {
274  dataRead = std::min(buffTotal_-buffLoc_, static_cast<unsigned int>(size));
275  memcpy(buf, &buffer_[buffLoc_], dataRead);
276  buffLoc_ += dataRead;
277  }
278  else
279  {
280  dataRead = buffTotal_-buffLoc_;
281  lstr.next_in = &buffer_[buffLoc_];
282  lstr.avail_in = dataRead;
283  lstr.next_out = buf;
284  lstr.avail_out = size;
285  int ret = lzma_code(&lstr, LZMA_RUN);
286  if(ret != LZMA_OK && ret != LZMA_STREAM_END)
287  { /* decompression error */
288  lzma_end(&lstr);
289  throw cms::Exception("IO") << "Error while reading compressed LHE file (error code " << ret << ")";
290  }
291  dataRead -= lstr.avail_in;
292  buffLoc_ += dataRead;
293  // Decoder was unable to make progress; reset stream and try again.
294  // If this becomes problematic, we can make the buffer circular.
295  if (!dataRead)
296  {
297  // NOTE: lstr.avail_in == buffTotal-buffLoc_
298  in->position(-(IOOffset)(lstr.avail_in), Storage::CURRENT);
299  buffLoc_ = 0;
300  buffTotal_ = 0;
301  return readBytes(buf, size);
302  }
303  dataRead = (size - lstr.avail_out);
304  }
305  return dataRead;
306 }
size
Write out results.
virtual IOSize read(void *into, IOSize n, IOOffset pos)
Definition: Storage.cc:17
virtual IOOffset position(void) const
Definition: Storage.cc:95
T min(T a, T b)
Definition: MathUtil.h:58
virtual XMLSize_t readBytes(XMLByte *const buf, const XMLSize_t size) override
Definition: XMLUtils.cc:249
StorageWrap & in
Definition: XMLUtils.h:189
unsigned int buffLoc_
Definition: XMLUtils.h:195
int64_t IOOffset
Definition: IOTypes.h:19
unsigned int buffTotal_
Definition: XMLUtils.h:195
std::vector< uint8_t > buffer_
Definition: XMLUtils.h:196

Member Data Documentation

std::vector<uint8_t> lhef::StorageInputStream::buffer_
private

Definition at line 196 of file XMLUtils.h.

Referenced by readBytes(), and StorageInputStream().

constexpr unsigned lhef::StorageInputStream::bufferSize_ = 16*1024*1024
staticprivate

Definition at line 197 of file XMLUtils.h.

Referenced by StorageInputStream().

unsigned int lhef::StorageInputStream::buffLoc_ = 0
private

Definition at line 195 of file XMLUtils.h.

Referenced by readBytes().

unsigned int lhef::StorageInputStream::buffTotal_ = 0
private

Definition at line 195 of file XMLUtils.h.

Referenced by readBytes().

bool lhef::StorageInputStream::compression_
private

Definition at line 192 of file XMLUtils.h.

Referenced by readBytes(), and StorageInputStream().

StorageWrap& lhef::StorageInputStream::in
private

Definition at line 189 of file XMLUtils.h.

Referenced by readBytes().

unsigned int lhef::StorageInputStream::lasttotal_
private

Definition at line 193 of file XMLUtils.h.

lzma_stream lhef::StorageInputStream::lstr
private

Definition at line 191 of file XMLUtils.h.

Referenced by readBytes(), StorageInputStream(), and ~StorageInputStream().

unsigned int lhef::StorageInputStream::pos
private