Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 #include "packager/file/public/buffer_callback_params.h"
16 
17 namespace shaka {
18 
19 extern const char* kCallbackFilePrefix;
20 extern const char* kLocalFilePrefix;
21 extern const char* kMemoryFilePrefix;
22 extern const char* kUdpFilePrefix;
23 const int64_t kWholeFile = -1;
24 
26 class File {
27  public:
34  static File* Open(const char* file_name, const char* mode);
35 
42  static File* OpenWithNoBuffering(const char* file_name, const char* mode);
43 
47  static bool Delete(const char* file_name);
48 
54  virtual bool Close() = 0;
55 
62  virtual int64_t Read(void* buffer, uint64_t length) = 0;
63 
68  virtual int64_t Write(const void* buffer, uint64_t length) = 0;
69 
72  virtual int64_t Size() = 0;
73 
79  virtual bool Flush() = 0;
80 
84  virtual bool Seek(uint64_t position) = 0;
85 
90  virtual bool Tell(uint64_t* position) = 0;
91 
94  const std::string& file_name() const { return file_name_; }
95 
96  // ************************************************************
97  // * Static Methods: File-on-the-filesystem status
98  // ************************************************************
99 
102  static int64_t GetFileSize(const char* file_name);
103 
108  static bool ReadFileToString(const char* file_name, std::string* contents);
109 
114  static bool WriteStringToFile(const char* file_name,
115  const std::string& contents);
116 
121  static bool WriteFileAtomically(const char* file_name,
122  const std::string& contents);
123 
130  static bool Copy(const char* from_file_name, const char* to_file_name);
131 
136  static int64_t CopyFile(File* source, File* destination);
137 
143  static int64_t CopyFile(File* source, File* destination, int64_t max_copy);
144 
151  static std::string MakeCallbackFileName(
152  const BufferCallbackParams& callback_params,
153  const std::string& name);
154 
161  static bool ParseCallbackFileName(
162  const std::string& callback_file_name,
163  const BufferCallbackParams** callback_params,
164  std::string* name);
165 
166  protected:
167  explicit File(const std::string& file_name) : file_name_(file_name) {}
170  virtual ~File() {}
171 
173  virtual bool Open() = 0;
174 
175  private:
176  friend class ThreadedIoFile;
177 
178  // This is a file factory method, it creates a proper file, e.g.
179  // LocalFile, MemFile based on prefix.
180  static File* Create(const char* file_name, const char* mode);
181 
182  static File* CreateInternalFile(const char* file_name, const char* mode);
183 
184  // Note that the file type prefix has been stripped off.
185  std::string file_name_;
186 
187  DISALLOW_COPY_AND_ASSIGN(File);
188 };
189 
190 } // namespace shaka
191 
192 #endif // PACKAGER_FILE_FILE_H_
virtual int64_t Write(const void *buffer, uint64_t length)=0
const std::string & file_name() const
Definition: file.h:94
static bool Delete(const char *file_name)
Definition: file.cc:198
Define an abstract file interface.
Definition: file.h:26
static bool Copy(const char *from_file_name, const char *to_file_name)
Definition: file.cc:281
static int64_t CopyFile(File *source, File *destination)
Definition: file.cc:316
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:216
virtual int64_t Read(void *buffer, uint64_t length)=0
Declaration of class which implements a thread-safe circular buffer.
static int64_t GetFileSize(const char *file_name)
Definition: file.cc:207
virtual bool Tell(uint64_t *position)=0
virtual bool Close()=0
virtual bool Flush()=0
virtual int64_t Size()=0
static bool WriteStringToFile(const char *file_name, const std::string &contents)
Definition: file.cc:234
virtual bool Seek(uint64_t position)=0
static std::string MakeCallbackFileName(const BufferCallbackParams &callback_params, const std::string &name)
Definition: file.cc:354
static File * OpenWithNoBuffering(const char *file_name, const char *mode)
Definition: file.cc:187
virtual ~File()
Definition: file.h:170
static bool WriteFileAtomically(const char *file_name, const std::string &contents)
Definition: file.cc:262
static bool ParseCallbackFileName(const std::string &callback_file_name, const BufferCallbackParams **callback_params, std::string *name)
Definition: file.cc:364
virtual bool Open()=0
Internal open. Should not be used directly.
Buffer callback params.