DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
memory_file.cc
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/file/memory_file.h"
8 
9 #include <string.h> // for memcpy
10 
11 #include <map>
12 
13 #include "packager/base/logging.h"
14 #include "packager/base/memory/scoped_ptr.h"
15 
16 namespace edash_packager {
17 namespace media {
18 namespace {
19 
20 // A helper filesystem object. This holds the data for the memory files.
21 class FileSystem {
22  public:
23  ~FileSystem() {}
24 
25  static FileSystem* Instance() {
26  if (!g_file_system_)
27  g_file_system_.reset(new FileSystem());
28 
29  return g_file_system_.get();
30  }
31 
32  bool Exists(const std::string& file_name) const {
33  return files_.find(file_name) != files_.end();
34  }
35 
36  std::vector<uint8_t>* GetFile(const std::string& file_name) {
37  return &files_[file_name];
38  }
39 
40  void Delete(const std::string& file_name) { files_.erase(file_name); }
41 
42  void DeleteAll() { files_.clear(); }
43 
44  private:
45  FileSystem() {}
46 
47  static scoped_ptr<FileSystem> g_file_system_;
48 
49  std::map<std::string, std::vector<uint8_t> > files_;
50  DISALLOW_COPY_AND_ASSIGN(FileSystem);
51 };
52 
53 scoped_ptr<FileSystem> FileSystem::g_file_system_;
54 
55 } // namespace
56 
57 MemoryFile::MemoryFile(const std::string& file_name, const std::string& mode)
58  : File(file_name), mode_(mode), file_(NULL), position_(0) {}
59 
60 MemoryFile::~MemoryFile() {}
61 
63  delete this;
64  return true;
65 }
66 
67 int64_t MemoryFile::Read(void* buffer, uint64_t length) {
68  const uint64_t size = Size();
69  DCHECK_LE(position_, size);
70  if (position_ >= size)
71  return 0;
72 
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;
76  return bytes_to_read;
77 }
78 
79 int64_t MemoryFile::Write(const void* buffer, uint64_t length) {
80  const uint64_t size = Size();
81  if (size < position_ + length) {
82  file_->resize(position_ + length);
83  }
84 
85  memcpy(&(*file_)[position_], buffer, length);
86  position_ += length;
87  return length;
88 }
89 
90 int64_t MemoryFile::Size() {
91  DCHECK(file_);
92  return file_->size();
93 }
94 
96  return true;
97 }
98 
99 bool MemoryFile::Seek(uint64_t position) {
100  if (Size() < static_cast<int64_t>(position))
101  return false;
102 
103  position_ = position;
104  return true;
105 }
106 
107 bool MemoryFile::Tell(uint64_t* position) {
108  *position = position_;
109  return true;
110 }
111 
113  FileSystem* file_system = FileSystem::Instance();
114  if (mode_ == "r") {
115  if (!file_system->Exists(file_name()))
116  return false;
117  } else if (mode_ == "w") {
118  file_system->Delete(file_name());
119  } else {
120  NOTIMPLEMENTED() << "File mode " << mode_ << " not supported by MemoryFile";
121  return false;
122  }
123 
124  file_ = file_system->GetFile(file_name());
125  DCHECK(file_);
126  position_ = 0;
127  return true;
128 }
129 
131  FileSystem::Instance()->DeleteAll();
132 }
133 
134 void MemoryFile::Delete(const std::string& file_name) {
135  FileSystem::Instance()->Delete(file_name);
136 }
137 
138 } // namespace media
139 } // namespace edash_packager
140 
bool Seek(uint64_t position) override
Definition: memory_file.cc:99
bool Open() override
Internal open. Should not be used directly.
Definition: memory_file.cc:112
int64_t Read(void *buffer, uint64_t length) override
Definition: memory_file.cc:67
bool Tell(uint64_t *position) override
Definition: memory_file.cc:107
const std::string & file_name() const
Definition: file.h:90
int64_t Write(const void *buffer, uint64_t length) override
Definition: memory_file.cc:79
static void Delete(const std::string &file_name)
Definition: memory_file.cc:134