7 #include "packager/file/memory_file.h"
15 #include "packager/base/logging.h"
25 static FileSystem* Instance() {
27 g_file_system_.reset(
new FileSystem());
29 return g_file_system_.get();
32 bool Exists(
const std::string& file_name)
const {
33 return files_.find(file_name) != files_.end();
36 std::vector<uint8_t>* GetFile(
const std::string& file_name) {
37 return &files_[file_name];
40 void Delete(
const std::string& file_name) { files_.erase(file_name); }
42 void DeleteAll() { files_.clear(); }
47 static std::unique_ptr<FileSystem> g_file_system_;
49 std::map<std::string, std::vector<uint8_t> > files_;
50 DISALLOW_COPY_AND_ASSIGN(FileSystem);
53 std::unique_ptr<FileSystem> FileSystem::g_file_system_;
57 MemoryFile::MemoryFile(
const std::string& file_name,
const std::string& mode)
58 : File(file_name), mode_(mode), file_(NULL), position_(0) {}
60 MemoryFile::~MemoryFile() {}
68 const uint64_t size =
Size();
69 DCHECK_LE(position_, size);
70 if (position_ >= size)
73 const uint64_t bytes_to_read = std::min(length, size - position_);
74 memcpy(buffer, &(*file_)[position_], bytes_to_read);
75 position_ += bytes_to_read;
80 const uint64_t size =
Size();
81 if (size < position_ + length) {
82 file_->resize(position_ + length);
85 memcpy(&(*file_)[position_], buffer, length);
100 if (
Size() < static_cast<int64_t>(position))
103 position_ = position;
108 *position = position_;
113 FileSystem* file_system = FileSystem::Instance();
115 if (!file_system->Exists(file_name()))
117 }
else if (mode_ ==
"w") {
120 NOTIMPLEMENTED() <<
"File mode " << mode_ <<
" not supported by MemoryFile";
124 file_ = file_system->GetFile(
file_name());
131 FileSystem::Instance()->DeleteAll();
135 FileSystem::Instance()->Delete(file_name);
static void Delete(const std::string &file_name)
const std::string & file_name() const
bool Open() override
Internal open. Should not be used directly.
bool Seek(uint64_t position) override
int64_t Write(const void *buffer, uint64_t length) override
bool Tell(uint64_t *position) override
int64_t Read(void *buffer, uint64_t length) override