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  std::vector<uint8_t>* GetFile(const std::string& file_name) {
33  return &files_[file_name];
34  }
35 
36  void Delete(const std::string& file_name) { files_.erase(file_name); }
37 
38  void DeleteAll() { files_.clear(); }
39 
40  private:
41  FileSystem() {}
42 
43  static scoped_ptr<FileSystem> g_file_system_;
44 
45  std::map<std::string, std::vector<uint8_t> > files_;
46  DISALLOW_COPY_AND_ASSIGN(FileSystem);
47 };
48 
49 scoped_ptr<FileSystem> FileSystem::g_file_system_;
50 
51 } // namespace
52 
53 MemoryFile::MemoryFile(const std::string& file_name)
54  : File(file_name),
55  file_(FileSystem::Instance()->GetFile(file_name)),
56  position_(0) {}
57 
58 MemoryFile::~MemoryFile() {}
59 
61  delete this;
62  return true;
63 }
64 
65 int64_t MemoryFile::Read(void* buffer, uint64_t length) {
66  const uint64_t size = Size();
67  DCHECK_LE(position_, size);
68  if (position_ >= size)
69  return 0;
70 
71  const uint64_t bytes_to_read = std::min(length, size - position_);
72  memcpy(buffer, &(*file_)[position_], bytes_to_read);
73  position_ += bytes_to_read;
74  return bytes_to_read;
75 }
76 
77 int64_t MemoryFile::Write(const void* buffer, uint64_t length) {
78  const uint64_t size = Size();
79  if (size < position_ + length) {
80  file_->resize(position_ + length);
81  }
82 
83  memcpy(&(*file_)[position_], buffer, length);
84  position_ += length;
85  return length;
86 }
87 
88 int64_t MemoryFile::Size() {
89  return file_->size();
90 }
91 
93  return true;
94 }
95 
96 bool MemoryFile::Seek(uint64_t position) {
97  if (Size() < static_cast<int64_t>(position))
98  return false;
99 
100  position_ = position;
101  return true;
102 }
103 
104 bool MemoryFile::Tell(uint64_t* position) {
105  *position = position_;
106  return true;
107 }
108 
110  position_ = 0;
111  return true;
112 }
113 
115  FileSystem::Instance()->DeleteAll();
116 }
117 
118 void MemoryFile::Delete(const std::string& file_name) {
119  FileSystem::Instance()->Delete(file_name);
120 }
121 
122 } // namespace media
123 } // namespace edash_packager
124 
bool Seek(uint64_t position) override
Definition: memory_file.cc:96
bool Open() override
Internal open. Should not be used directly.
Definition: memory_file.cc:109
int64_t Read(void *buffer, uint64_t length) override
Definition: memory_file.cc:65
bool Tell(uint64_t *position) override
Definition: memory_file.cc:104
int64_t Write(const void *buffer, uint64_t length) override
Definition: memory_file.cc:77
static void Delete(const std::string &file_name)
Definition: memory_file.cc:118