Shaka Packager SDK
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 #include "packager/status.h"
17 
18 namespace shaka {
19 
20 extern const char* kCallbackFilePrefix;
21 extern const char* kLocalFilePrefix;
22 extern const char* kMemoryFilePrefix;
23 extern const char* kUdpFilePrefix;
24 extern const char* kHttpFilePrefix;
25 const int64_t kWholeFile = -1;
26 
28 class SHAKA_EXPORT File {
29  public:
36  static File* Open(const char* file_name, const char* mode);
37 
44  static File* OpenWithNoBuffering(const char* file_name, const char* mode);
45 
49  static bool Delete(const char* file_name);
50 
56  virtual bool Close() = 0;
57 
64  virtual int64_t Read(void* buffer, uint64_t length) = 0;
65 
70  virtual int64_t Write(const void* buffer, uint64_t length) = 0;
71 
74  virtual int64_t Size() = 0;
75 
81  virtual bool Flush() = 0;
82 
86  virtual bool Seek(uint64_t position) = 0;
87 
92  virtual bool Tell(uint64_t* position) = 0;
93 
96  const std::string& file_name() const { return file_name_; }
97 
98  // ************************************************************
99  // * Static Methods: File-on-the-filesystem status
100  // ************************************************************
101 
104  static int64_t GetFileSize(const char* file_name);
105 
110  static bool ReadFileToString(const char* file_name, std::string* contents);
111 
116  static bool WriteStringToFile(const char* file_name,
117  const std::string& contents);
118 
123  static bool WriteFileAtomically(const char* file_name,
124  const std::string& contents);
125 
132  static bool Copy(const char* from_file_name, const char* to_file_name);
133 
138  static int64_t CopyFile(File* source, File* destination);
139 
145  static int64_t CopyFile(File* source, File* destination, int64_t max_copy);
146 
149  static bool IsLocalRegularFile(const char* file_name);
150 
157  static std::string MakeCallbackFileName(
158  const BufferCallbackParams& callback_params,
159  const std::string& name);
160 
167  static bool ParseCallbackFileName(
168  const std::string& callback_file_name,
169  const BufferCallbackParams** callback_params,
170  std::string* name);
171 
172  protected:
173  explicit File(const std::string& file_name) : file_name_(file_name) {}
176  virtual ~File() {}
177 
179  virtual bool Open() = 0;
180 
181  private:
182  friend class ThreadedIoFile;
183 
184  // This is a file factory method, it creates a proper file, e.g.
185  // LocalFile, MemFile based on prefix.
186  static File* Create(const char* file_name, const char* mode);
187 
188  static File* CreateInternalFile(const char* file_name, const char* mode);
189 
190  // Note that the file type prefix has been stripped off.
191  std::string file_name_;
192 
193  DISALLOW_COPY_AND_ASSIGN(File);
194 };
195 
196 } // namespace shaka
197 
198 #endif // PACKAGER_FILE_FILE_H_
shaka::File::file_name
const std::string & file_name() const
Definition: file.h:96
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::BufferCallbackParams
Buffer callback params.
Definition: buffer_callback_params.h:15
shaka::ThreadedIoFile
Declaration of class which implements a thread-safe circular buffer.
Definition: threaded_io_file.h:20
shaka::File
Define an abstract file interface.
Definition: file.h:28
shaka::File::~File
virtual ~File()
Definition: file.h:176