Shaka Packager SDK
Public Member Functions | Static Public Member Functions | Protected Member Functions | Friends | List of all members
shaka::File Class Referenceabstract

Define an abstract file interface. More...

#include <file.h>

Inheritance diagram for shaka::File:
shaka::CallbackFile shaka::HttpFile shaka::LocalFile shaka::MemoryFile shaka::ThreadedIoFile shaka::UdpFile

Public Member Functions

virtual bool Close ()=0
 
virtual int64_t Read (void *buffer, uint64_t length)=0
 
virtual int64_t Write (const void *buffer, uint64_t length)=0
 
virtual int64_t Size ()=0
 
virtual bool Flush ()=0
 
virtual bool Seek (uint64_t position)=0
 
virtual bool Tell (uint64_t *position)=0
 
const std::string & file_name () const
 

Static Public Member Functions

static FileOpen (const char *file_name, const char *mode)
 
static FileOpenWithNoBuffering (const char *file_name, const char *mode)
 
static bool Delete (const char *file_name)
 
static int64_t GetFileSize (const char *file_name)
 
static bool ReadFileToString (const char *file_name, std::string *contents)
 
static bool WriteStringToFile (const char *file_name, const std::string &contents)
 
static bool WriteFileAtomically (const char *file_name, const std::string &contents)
 
static bool Copy (const char *from_file_name, const char *to_file_name)
 
static int64_t CopyFile (File *source, File *destination)
 
static int64_t CopyFile (File *source, File *destination, int64_t max_copy)
 
static bool IsLocalRegularFile (const char *file_name)
 
static std::string MakeCallbackFileName (const BufferCallbackParams &callback_params, const std::string &name)
 
static bool ParseCallbackFileName (const std::string &callback_file_name, const BufferCallbackParams **callback_params, std::string *name)
 

Protected Member Functions

 File (const std::string &file_name)
 
virtual ~File ()
 
virtual bool Open ()=0
 Internal open. Should not be used directly.
 

Friends

class ThreadedIoFile
 

Detailed Description

Define an abstract file interface.

Definition at line 27 of file file.h.

Constructor & Destructor Documentation

◆ ~File()

virtual shaka::File::~File ( )
inlineprotectedvirtual

Do not call the destructor directly (with the "delete" keyword) nor use std::unique_ptr; instead use Close().

Definition at line 175 of file file.h.

Member Function Documentation

◆ Close()

virtual bool shaka::File::Close ( )
pure virtual

Flush() and de-allocate resources associated with this file, and delete this File object. THIS IS THE ONE TRUE WAY TO DEALLOCATE THIS OBJECT.

Returns
true on success. For writable files, returning false MAY INDICATE DATA LOSS.

Implemented in shaka::UdpFile, shaka::ThreadedIoFile, shaka::MemoryFile, shaka::LocalFile, shaka::HttpFile, and shaka::CallbackFile.

◆ Copy()

bool shaka::File::Copy ( const char *  from_file_name,
const char *  to_file_name 
)
static

Copies files. This is not good for copying huge files. Although not recommended, it is safe to have source file and destination file name be the same.

Parameters
from_file_nameis the source file name.
to_file_nameis the destination file name.
Returns
true on success, false otherwise.

Definition at line 300 of file file.cc.

◆ CopyFile() [1/2]

int64_t shaka::File::CopyFile ( File source,
File destination 
)
static

Copies the contents from source to destination.

Parameters
sourceThe file to copy from.
destinationThe file to copy to.
Returns
Number of bytes written, or a value < 0 on error.

Definition at line 336 of file file.cc.

◆ CopyFile() [2/2]

int64_t shaka::File::CopyFile ( File source,
File destination,
int64_t  max_copy 
)
static

Copies the contents from source to destination.

Parameters
sourceThe file to copy from.
destinationThe file to copy to.
max_copyThe maximum number of bytes to copy; < 0 to copy to EOF.
Returns
Number of bytes written, or a value < 0 on error.

Definition at line 340 of file file.cc.

◆ Delete()

bool shaka::File::Delete ( const char *  file_name)
static

Delete the specified file.

Parameters
file_namecontains the path of the file to be deleted.
Returns
true if successful, false otherwise.

Definition at line 212 of file file.cc.

◆ file_name()

const std::string& shaka::File::file_name ( ) const
inline
Returns
The file name. Note that the file type prefix has been stripped off.

Definition at line 95 of file file.h.

◆ Flush()

virtual bool shaka::File::Flush ( )
pure virtual

Flush the file so that recently written data will survive an application crash (but not necessarily an OS crash). For instance, in LocalFile the data is flushed into the OS but not necessarily to disk.

Returns
true on success, false otherwise.

Implemented in shaka::UdpFile, shaka::ThreadedIoFile, shaka::MemoryFile, shaka::LocalFile, shaka::HttpFile, and shaka::CallbackFile.

◆ GetFileSize()

int64_t shaka::File::GetFileSize ( const char *  file_name)
static
Returns
The size of a file in bytes on success, a value < 0 otherwise. The file will be opened and closed in the process.

Definition at line 221 of file file.cc.

◆ IsLocalRegularFile()

bool shaka::File::IsLocalRegularFile ( const char *  file_name)
static
Parameters
file_nameis the name of the file to be checked.
Returns
true if file_name is a local and regular file.

Definition at line 377 of file file.cc.

◆ MakeCallbackFileName()

std::string shaka::File::MakeCallbackFileName ( const BufferCallbackParams callback_params,
const std::string &  name 
)
static

Generate callback file name. NOTE: THE GENERATED NAME IS ONLY VAID WHILE callback_params IS VALID.

Parameters
callback_paramsreferences BufferCallbackParams, which will be embedded in the generated callback file name.
nameis the name of the buffer, which will be embedded in the generated callback file name.

Definition at line 402 of file file.cc.

◆ Open()

File * shaka::File::Open ( const char *  file_name,
const char *  mode 
)
static

Open the specified file. This is a file factory method, it opens a proper file automatically based on prefix, e.g. "file://" for LocalFile.

Parameters
file_namecontains the name of the file to be accessed.
modecontains file access mode. Implementation dependent.
Returns
A File pointer on success, false otherwise.

Definition at line 190 of file file.cc.

◆ OpenWithNoBuffering()

File * shaka::File::OpenWithNoBuffering ( const char *  file_name,
const char *  mode 
)
static

Open the specified file in direct-access mode (no buffering). This is a file factory method, it opens a proper file automatically based on prefix, e.g. "file://" for LocalFile.

Parameters
file_namecontains the name of the file to be accessed.
modecontains file access mode. Implementation dependent.
Returns
A File pointer on success, false otherwise.

Definition at line 201 of file file.cc.

◆ ParseCallbackFileName()

bool shaka::File::ParseCallbackFileName ( const std::string &  callback_file_name,
const BufferCallbackParams **  callback_params,
std::string *  name 
)
static

Parse and extract callback params.

Parameters
callback_file_nameis the name of the callback file which contains callback_params and name.
callback_paramspoints to the parsed BufferCallbackParams pointer.
namepoints to the parsed name.
Returns
true on success, false otherwise.

Definition at line 412 of file file.cc.

◆ Read()

virtual int64_t shaka::File::Read ( void *  buffer,
uint64_t  length 
)
pure virtual

Read data and return it in buffer.

Parameters
[out]bufferpoints to a block of memory with a size of at least length bytes.
lengthindicates number of bytes to be read.
Returns
Number of bytes read, or a value < 0 on error. Zero on end-of-file, or if 'length' is zero.

Implemented in shaka::UdpFile, shaka::ThreadedIoFile, shaka::MemoryFile, shaka::LocalFile, shaka::HttpFile, and shaka::CallbackFile.

◆ ReadFileToString()

bool shaka::File::ReadFileToString ( const char *  file_name,
std::string *  contents 
)
static

Read the contents of a file into string.

Parameters
file_nameis the file to be read. It should be a valid file.
contents[out]points to the output string. Should not be NULL.
Returns
true on success, false otherwise.

Definition at line 230 of file file.cc.

◆ Seek()

virtual bool shaka::File::Seek ( uint64_t  position)
pure virtual

Seek to the specifield position in the file.

Parameters
positionis the position to seek to.
Returns
true on success, false otherwise.

Implemented in shaka::UdpFile, shaka::ThreadedIoFile, shaka::MemoryFile, shaka::LocalFile, shaka::HttpFile, and shaka::CallbackFile.

◆ Size()

virtual int64_t shaka::File::Size ( )
pure virtual
Returns
Size of the file in bytes. A return value less than zero indicates a problem getting the size.

Implemented in shaka::UdpFile, shaka::ThreadedIoFile, shaka::MemoryFile, shaka::LocalFile, shaka::HttpFile, and shaka::CallbackFile.

◆ Tell()

virtual bool shaka::File::Tell ( uint64_t *  position)
pure virtual

Get the current file position.

Parameters
positionis a pointer to contain the current file position upon successful return.
Returns
true on succcess, false otherwise.

Implemented in shaka::UdpFile, shaka::ThreadedIoFile, shaka::MemoryFile, shaka::LocalFile, shaka::HttpFile, and shaka::CallbackFile.

◆ Write()

virtual int64_t shaka::File::Write ( const void *  buffer,
uint64_t  length 
)
pure virtual

Write block of data.

Parameters
bufferpoints to a block of memory with at least length bytes.
lengthindicates number of bytes to write.
Returns
Number of bytes written, or a value < 0 on error.

Implemented in shaka::UdpFile, shaka::ThreadedIoFile, shaka::MemoryFile, shaka::LocalFile, shaka::HttpFile, and shaka::CallbackFile.

◆ WriteFileAtomically()

bool shaka::File::WriteFileAtomically ( const char *  file_name,
const std::string &  contents 
)
static

Save contents to file_name in an atomic manner.

Parameters
file_nameis the destination file name.
contentsis the data to be saved.
Returns
true on success, false otherwise.

Definition at line 277 of file file.cc.

◆ WriteStringToFile()

bool shaka::File::WriteStringToFile ( const char *  file_name,
const std::string &  contents 
)
static

Writes the data to file.

Parameters
file_nameis the file to write to.
contentsis the data to write.
Returns
true on success, false otherwise.

Definition at line 248 of file file.cc.


The documentation for this class was generated from the following files: