Add Data::StoreToFile

This commit is contained in:
Sebastian Messmer 2014-12-05 07:42:39 +01:00
parent e585d12511
commit e32a84eb8e
4 changed files with 23 additions and 10 deletions

View File

@ -1,12 +1,16 @@
#include <blobstore/implementations/ondisk/Data.h>
#include <stdexcept>
#include <fstream>
using std::ofstream;
using std::ios;
namespace blobstore {
namespace ondisk {
Data::Data(size_t size)
: _data(std::malloc(size)) {
: _size(size), _data(std::malloc(size)) {
if (nullptr == _data) {
throw std::bad_alloc();
}
@ -25,5 +29,14 @@ const void *Data::data() const {
return _data;
}
size_t Data::size() const {
return _size;
}
void Data::StoreToFile(const boost::filesystem::path &filepath) const {
ofstream file(filepath.c_str(), ios::binary | ios::trunc);
file.write((const char*)_data, _size);
}
} /* namespace ondisk */
} /* namespace blobstore */

View File

@ -6,6 +6,8 @@
//TODO Move this to a more generic utils
#include "fspp/utils/macros.h"
#include <boost/filesystem/path.hpp>
namespace blobstore {
namespace ondisk {
@ -17,13 +19,17 @@ public:
void *data();
const void *data() const;
size_t size() const;
void StoreToFile(const boost::filesystem::path &filepath) const;
private:
size_t _size;
void *_data;
DISALLOW_COPY_AND_ASSIGN(Data);
};
} /* namespace ondisk */
} /* namespace blobstore */

View File

@ -84,12 +84,7 @@ void OnDiskBlob::_fillDataWithZeroes() {
}
void OnDiskBlob::_storeToDisk() const {
ofstream file(_filepath.c_str(), ios::binary | ios::trunc);
_storeDataToStream(file);
}
void OnDiskBlob::_storeDataToStream(ostream &stream) const {
stream.write((const char*)_data.data(), _size);
_data.StoreToFile(_filepath);
}
} /* namespace ondisk */

View File

@ -25,8 +25,7 @@ public:
void SetFileSize(size_t size) {
Data data(size);
ofstream writer(file.path().c_str(), ios::trunc | ios::binary);
writer.write((char*)data.data(), size);
data.StoreToFile(file.path());
}
};