DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
file.h
1 // Copyright 2014 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 #ifndef PACKAGER_FILE_FILE_H_
8 #define PACKAGER_FILE_FILE_H_
9 
10 #include <stdint.h>
11 
12 #include <string>
13 
14 #include "packager/base/macros.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 extern const char* kLocalFilePrefix;
20 extern const char* kMemoryFilePrefix;
21 const int64_t kWholeFile = -1;
22 
24 class File {
25  public:
32  static File* Open(const char* file_name, const char* mode);
33 
40  static File* OpenWithNoBuffering(const char* file_name, const char* mode);
41 
45  static bool Delete(const char* file_name);
46 
52  virtual bool Close() = 0;
53 
60  virtual int64_t Read(void* buffer, uint64_t length) = 0;
61 
66  virtual int64_t Write(const void* buffer, uint64_t length) = 0;
67 
70  virtual int64_t Size() = 0;
71 
77  virtual bool Flush() = 0;
78 
82  virtual bool Seek(uint64_t position) = 0;
83 
88  virtual bool Tell(uint64_t* position) = 0;
89 
91  const std::string& file_name() const { return file_name_; }
92 
93  // ************************************************************
94  // * Static Methods: File-on-the-filesystem status
95  // ************************************************************
96 
99  static int64_t GetFileSize(const char* file_name);
100 
105  static bool ReadFileToString(const char* file_name, std::string* contents);
106 
111  static bool WriteFileAtomically(const char* file_name,
112  const std::string& contents);
113 
120  static bool Copy(const char* from_file_name, const char* to_file_name);
121 
126  static int64_t CopyFile(File* source, File* destination);
127 
133  static int64_t CopyFile(File* source, File* destination, int64_t max_copy);
134 
135  protected:
136  explicit File(const std::string& file_name) : file_name_(file_name) {}
139  virtual ~File() {}
140 
142  virtual bool Open() = 0;
143 
144  private:
145  friend class ThreadedIoFile;
146 
147  // This is a file factory method, it creates a proper file, e.g.
148  // LocalFile, MemFile based on prefix.
149  static File* Create(const char* file_name, const char* mode);
150 
151  static File* CreateInternalFile(const char* file_name, const char* mode);
152 
153  std::string file_name_;
154  DISALLOW_COPY_AND_ASSIGN(File);
155 };
156 
157 } // namespace media
158 } // namespace shaka
159 
160 #endif // PACKAGER_FILE_FILE_H_
virtual bool Open()=0
Internal open. Should not be used directly.
static File * OpenWithNoBuffering(const char *file_name, const char *mode)
Definition: file.cc:170
static bool Copy(const char *from_file_name, const char *to_file_name)
Definition: file.cc:253
virtual int64_t Size()=0
virtual ~File()
Definition: file.h:139
virtual bool Close()=0
virtual int64_t Write(const void *buffer, uint64_t length)=0
Define an abstract file interface.
Definition: file.h:24
virtual int64_t Read(void *buffer, uint64_t length)=0
static int64_t CopyFile(File *source, File *destination)
Definition: file.cc:282
virtual bool Tell(uint64_t *position)=0
static bool WriteFileAtomically(const char *file_name, const std::string &contents)
Definition: file.cc:217
const std::string & file_name() const
Definition: file.h:91
static int64_t GetFileSize(const char *file_name)
Definition: file.cc:190
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:199
virtual bool Flush()=0
virtual bool Seek(uint64_t position)=0
Declaration of class which implements a thread-safe circular buffer.
static bool Delete(const char *file_name)
Definition: file.cc:181