File indexing completed on 2024-04-06 12:19:11
0001 #include "IOPool/Streamer/interface/StreamerFileIO.h"
0002 #include <fstream>
0003 #include <iostream>
0004 #include <cstring>
0005 #include "FWCore/Utilities/interface/Adler32Calculator.h"
0006 #include "FWCore/Utilities/interface/Exception.h"
0007 #include "IOPool/Streamer/interface/MsgHeader.h"
0008
0009 namespace edm::streamer {
0010 OutputFile::OutputFile(const std::string& name, uint32 padding)
0011 : current_offset_(1),
0012 do_adler_(false),
0013 adlera_(1),
0014 adlerb_(0),
0015 padding_(padding),
0016 ost_(new std::ofstream(name.c_str(), std::ios_base::binary | std::ios_base::out)),
0017 filename_(name) {
0018 if (!ost_->is_open()) {
0019 throw cms::Exception("OutputFile", "OutputFile") << "Error Opening Output File: " << name << "\n";
0020 }
0021 ost_->rdbuf()->pubsetbuf(nullptr, 0);
0022 if (padding_) {
0023 paddingBuf_ = std::make_unique<char[]>(padding_);
0024 memset(paddingBuf_.get(), Header::PADDING, padding_);
0025 }
0026 }
0027
0028 OutputFile::~OutputFile() { ost_->close(); }
0029
0030 bool OutputFile::write(const char* ptr, size_t n, bool doPadding) {
0031 ost_->write(ptr, n);
0032 if (!ost_->fail()) {
0033 current_offset_ += (uint64)(n);
0034 if (do_adler_)
0035 cms::Adler32(ptr, n, adlera_, adlerb_);
0036 if (doPadding && padding_) {
0037 return writePadding();
0038 }
0039 return false;
0040 }
0041 return true;
0042 }
0043
0044 bool OutputFile::writePadding() {
0045 uint64 mod = ost_->tellp() % padding_;
0046 if (mod) {
0047 uint32 rem = padding_ - (uint32)(mod % padding_);
0048 bool ret = write(paddingBuf_.get(), rem, false);
0049 return ret;
0050 }
0051 return false;
0052 }
0053
0054 void OutputFile::close() {
0055 if (padding_)
0056 if (writePadding())
0057 throw cms::Exception("OutputFile", "OutputFile")
0058 << "Error writing padding to the output file: " << filename_ << ": " << std::strerror(errno);
0059 ost_->close();
0060 }
0061 }