CMS 3D CMS Logo

StreamerFileIO.cc
Go to the documentation of this file.
2 #include <fstream>
3 #include <iostream>
4 #include <cstring>
8 
9 namespace edm::streamer {
11  : current_offset_(1),
12  do_adler_(false),
13  adlera_(1),
14  adlerb_(0),
15  padding_(padding),
16  ost_(new std::ofstream(name.c_str(), std::ios_base::binary | std::ios_base::out)),
17  filename_(name) {
18  if (!ost_->is_open()) {
19  throw cms::Exception("OutputFile", "OutputFile") << "Error Opening Output File: " << name << "\n";
20  }
21  ost_->rdbuf()->pubsetbuf(nullptr, 0);
22  if (padding_) {
23  paddingBuf_ = std::make_unique<char[]>(padding_);
24  memset(paddingBuf_.get(), Header::PADDING, padding_);
25  }
26  }
27 
28  OutputFile::~OutputFile() { ost_->close(); }
29 
30  bool OutputFile::write(const char* ptr, size_t n, bool doPadding) {
31  ost_->write(ptr, n);
32  if (!ost_->fail()) {
33  current_offset_ += (uint64)(n);
34  if (do_adler_)
35  cms::Adler32(ptr, n, adlera_, adlerb_);
36  if (doPadding && padding_) {
37  return writePadding();
38  }
39  return false;
40  }
41  return true;
42  }
43 
45  uint64 mod = ost_->tellp() % padding_;
46  if (mod) {
47  uint32 rem = padding_ - (uint32)(mod % padding_);
48  bool ret = write(paddingBuf_.get(), rem, false);
49  return ret;
50  }
51  return false;
52  }
53 
55  if (padding_)
56  if (writePadding())
57  throw cms::Exception("OutputFile", "OutputFile")
58  << "Error writing padding to the output file: " << filename_ << ": " << std::strerror(errno);
59  ost_->close();
60  }
61 } // namespace edm::streamer
ret
prodAgent to be discontinued
OutputFile(const std::string &name, uint32 padding=0)
edm::propagate_const< std::shared_ptr< std::ofstream > > ost_
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
bool write(const char *ptr, size_t n, bool doPadding=false)
unsigned long long uint64
Definition: MsgTools.h:15
std::unique_ptr< char[]> paddingBuf_
unsigned int uint32
Definition: MsgTools.h:14
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4