CMS 3D CMS Logo

Public Types | Public Member Functions | Private Attributes

lhef::StorageInputStream Class Reference

#include <XMLUtils.h>

List of all members.

Public Types

typedef StorageWrap Stream_t

Public Member Functions

virtual unsigned int curPos () const
virtual unsigned int readBytes (XMLByte *const buf, const unsigned int size)
 StorageInputStream (StorageWrap &in)
virtual ~StorageInputStream ()

Private Attributes

bool compression_
StorageWrapin
unsigned int lasttotal_
lzma_stream lstr
unsigned int pos

Detailed Description

Definition at line 167 of file XMLUtils.h.


Member Typedef Documentation

Definition at line 170 of file XMLUtils.h.


Constructor & Destructor Documentation

lhef::StorageInputStream::StorageInputStream ( StorageWrap in)

Definition at line 206 of file XMLUtils.cc.

References compression_, Exception, lstr, NULL, run_regression::ret, and Storage::SET.

                                                      :
        in(in),
        lstr(LZMA_STREAM_INIT),
        compression_(false),
        lasttotal_(0)
{
  // Check the kind of file.
  char header[6];
  /*unsigned int s = */ in->read(header, 6);
  in->position(0, Storage::SET);
  // Let's use lzma to start with.
  if (header[1] == '7'
      && header[2] == 'z'
      && header[3] == 'X'
      && header[4] == 'Z')
  {
    compression_ = true;
    lstr = LZMA_STREAM_INIT;
    // We store the beginning of the outBuffer to make sure
    // we can always update previous results.

#if LZMA_VERSION <= UINT32_C(49990030)
    int ret = lzma_auto_decoder(&lstr, NULL, NULL);
#else
    int ret = lzma_auto_decoder(&lstr, -1, 0);
#endif

    if (ret != LZMA_OK)
    {
      lzma_end(&lstr);
      throw cms::Exception("IO") << "Error while reading compressed LHE file";
    }
  }
}
lhef::StorageInputStream::~StorageInputStream ( ) [virtual]

Definition at line 241 of file XMLUtils.cc.

References lstr.

{
  lzma_end(&(lstr));
}

Member Function Documentation

virtual unsigned int lhef::StorageInputStream::curPos ( ) const [inline, virtual]

Definition at line 175 of file XMLUtils.h.

References pos.

{ return pos; }
unsigned int lhef::StorageInputStream::readBytes ( XMLByte *const  buf,
const unsigned int  size 
) [virtual]

Definition at line 246 of file XMLUtils.cc.

References BUF_SIZE, compression_, gather_cfg::cout, Storage::CURRENT, Exception, in, lasttotal_, lstr, pos, SiPixelLorentzAngle_cfi::read, and run_regression::ret.

{
  // Simple read-in write-out in case
  if (!compression_)
  {
    void *rawBuf = reinterpret_cast<void*>(buf);
    unsigned int bytes = size * sizeof(XMLByte);
    unsigned int readBytes = in->read(rawBuf, bytes);

    unsigned int read = (unsigned int)(readBytes / sizeof(XMLByte));
    unsigned int rest = (unsigned int)(readBytes % sizeof(XMLByte));
    if (rest)
      in->position(-(IOOffset)rest, Storage::CURRENT);

    /*for (unsigned int i = 0; i < read; ++i){
      std::cout << buf[i] ;
    }*/
 
    pos += read;
    return read;
  }
  // Compressed case. 
  // We simply read as many bytes as we can and we 
  // uncompress them in the output buffer.
  // We never decompress more bytes we were asked by
  // xerces. 
  // In case we read from file more bytes than needed 
  // we simply rollback by the (hopefully) correct amount.
  // If we don't read enough bytes, we simply return
  // the amount of bytes read and we wait for being called
  // again by xerces.
  unsigned int bytes = size * sizeof(XMLByte);
//  std::cout << "bites " << bytes << std::endl;
  uint8_t inBuf[BUF_SIZE];
  unsigned int rd = in->read((void*)inBuf, BUF_SIZE);
  lstr.next_in = inBuf;
  lstr.avail_in = rd;
  lstr.next_out = buf;
  lstr.avail_out = bytes;
/*  for (unsigned int i = 0; i < size; ++i){
    std::cout << buf[i] ;
  }  
  std::cout << std::endl;*/

  int ret = lzma_code(&lstr, LZMA_RUN);
  if(ret != LZMA_OK && ret != LZMA_STREAM_END) {  /* decompression error */
    lzma_end(&lstr);
    throw cms::Exception("IO") << "Error while reading compressed LHE file";
  }
  // If we did not consume everything we put it back.
//  std::cout << "lstr.avail_in " << lstr.avail_in << " lstr.total_in " << lstr.total_in << std::endl;
//  std::cout << "lstr.avail_out " << lstr.avail_out << " lstr.total_out " << lstr.total_out << std::endl;
  if (lstr.avail_in){
    std::cout << "rolling back" << std::endl;
    in->position(-(IOOffset)(lstr.avail_in), Storage::CURRENT);    
  }  
  pos = lstr.total_out;
  unsigned int read = lstr.total_out - lasttotal_;
  lasttotal_ = lstr.total_out;
  return read;
}

Member Data Documentation

Definition at line 184 of file XMLUtils.h.

Referenced by readBytes(), and StorageInputStream().

Definition at line 181 of file XMLUtils.h.

Referenced by readBytes().

unsigned int lhef::StorageInputStream::lasttotal_ [private]

Definition at line 185 of file XMLUtils.h.

Referenced by readBytes().

lzma_stream lhef::StorageInputStream::lstr [private]

Definition at line 183 of file XMLUtils.h.

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

unsigned int lhef::StorageInputStream::pos [private]

Definition at line 182 of file XMLUtils.h.

Referenced by curPos(), and readBytes().