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