7 #include "packager/media/file/memory_file.h"
15 #include "packager/base/logging.h"
26 static FileSystem* Instance() {
28 g_file_system_.reset(
new FileSystem());
30 return g_file_system_.get();
33 bool Exists(
const std::string& file_name)
const {
34 return files_.find(file_name) != files_.end();
37 std::vector<uint8_t>* GetFile(
const std::string& file_name) {
38 return &files_[file_name];
41 void Delete(
const std::string& file_name) { files_.erase(file_name); }
43 void DeleteAll() { files_.clear(); }
48 static std::unique_ptr<FileSystem> g_file_system_;
50 std::map<std::string, std::vector<uint8_t> > files_;
51 DISALLOW_COPY_AND_ASSIGN(FileSystem);
54 std::unique_ptr<FileSystem> FileSystem::g_file_system_;
58 MemoryFile::MemoryFile(
const std::string& file_name,
const std::string& mode)
59 : File(file_name), mode_(mode), file_(NULL), position_(0) {}
61 MemoryFile::~MemoryFile() {}
69 const uint64_t size =
Size();
70 DCHECK_LE(position_, size);
71 if (position_ >= size)
74 const uint64_t bytes_to_read = std::min(length, size - position_);
75 memcpy(buffer, &(*file_)[position_], bytes_to_read);
76 position_ += bytes_to_read;
81 const uint64_t size =
Size();
82 if (size < position_ + length) {
83 file_->resize(position_ + length);
86 memcpy(&(*file_)[position_], buffer, length);
101 if (
Size() < static_cast<int64_t>(position))
104 position_ = position;
109 *position = position_;
114 FileSystem* file_system = FileSystem::Instance();
116 if (!file_system->Exists(file_name()))
118 }
else if (mode_ ==
"w") {
121 NOTIMPLEMENTED() <<
"File mode " << mode_ <<
" not supported by MemoryFile";
125 file_ = file_system->GetFile(
file_name());
132 FileSystem::Instance()->DeleteAll();
136 FileSystem::Instance()->Delete(file_name);