00001 #include "IOPool/Streamer/interface/StreamerFileIO.h"
00002 #include <fstream>
00003 #include <iostream>
00004 #include "FWCore/Utilities/interface/Exception.h"
00005
00006 OutputFile::OutputFile(const std::string& name):
00007 current_offset_(1),
00008 first_event_offset_(0),
00009 last_event_offset_(0),
00010 events_(0),
00011 run_(0),
00012 do_adler_(0),
00013 adlera_(1),
00014 adlerb_(0),
00015 ost_(new std::ofstream(name.c_str(), std::ios_base::binary | std::ios_base::out)),
00016 filename_(name)
00017 {
00018 if(!ost_->is_open()) {
00019 throw cms::Exception("OutputFile","OutputFile")
00020 << "Error Opening Output File: "<<name<<"\n";
00021 }
00022 ost_->rdbuf()->pubsetbuf(0,0);
00023 }
00024
00025 OutputFile::~OutputFile()
00026 {
00027 ost_->close();
00028 delete ost_;
00029 }
00030
00031 bool OutputFile::write(const char *ptr, size_t n)
00032 {
00033 ost_->write(ptr,n);
00034 if(!ost_->fail()) {
00035 current_offset_ += (uint64)(n);
00036 if (do_adler_)
00037 adler32(ptr,n,adlera_,adlerb_);
00038 return 0;
00039 }
00040 return 1;
00041 }
00042
00043
00044
00045
00046
00047
00048 void OutputFile::adler32(const char *data, size_t len, uint32 &a, uint32 &b)
00049 {
00050
00051
00052 #define MOD_ADLER 65521
00053
00054 const unsigned char *ptr = (const unsigned char *)data;
00055 while (len > 0)
00056 {
00057 size_t tlen = len > 5552 ? 5552 : len;
00058 len -= tlen;
00059 do
00060 {
00061 a += *ptr++;
00062 b += a;
00063 } while (--tlen);
00064
00065 a %= MOD_ADLER;
00066 b %= MOD_ADLER;
00067 }
00068
00069 #undef MOD_ADLER
00070 }
00071
00072 uint32 OutputFile::adler32(const char *data, size_t len)
00073 {
00074
00075
00076 uint32_t a = 1, b = 0;
00077 adler32(data,len,a,b);
00078 return (b << 16) | a;
00079 }