Enrollment 28.2.0
Loading...
Searching...
No Matches
File

The library defines BinaryFileAccess interface to access files for reading and writing. More...

Collaboration diagram for File:

Classes

class  BinaryFileAccess
 BinaryFileAccess provides abstraction on underlying file system. More...
class  FilesystemAccess
 Standard filesystem implementation. More...
class  GlobalFileAccess
 Holds instance of BinaryFileAccess. More...
class  FileException
 Exception for file processing. More...

Detailed Description

The library defines BinaryFileAccess interface to access files for reading and writing.

The library defines BinaryFileAccess interface to access files for reading and writing. Usually default implementation (using filesystem) is sufficient. In case e.g. Android we need special access implementation due to Android restriction. The default GlobalFileAccess is filesystem implementation that is sufficient on most operating systems. The BinaryFileAccess can be implemented also in Java / CSharp bindings.

The library provide convenient static methods BinaryFile::ReadAll() and BinaryFile::WriteAll() that uses GlobalFileAccess to read, write files.

  • c++
    class UserFileAccess : public BinaryFileAccess
    {
    std::vector<uint8_t> ReadAll(const std::string& filePath) final
    {
    // do something useful, special
    return FilesystemAccess::Create()->ReadAll(filePath);
    }
    void WriteAll(const std::string& filePath,
    const std::vector<uint8_t>& content) final
    {
    // do something useful, special
    FilesystemAccess::Create()->WriteAll(filePath, content);
    }
    };
    static void UseUserManagedFilesystem()
    {
    GlobalFileAccess::Set(std::make_shared<UserFileAccess>());
    auto content = BinaryFile::ReadAll("assets/color.png");
    BinaryFile::WriteAll("output/color_copied_via_file_access.png", content);
    }
  • java
    class UserFileAccess extends BinaryFileAccess {
    public Bytes ReadAll(String filePath) {
    try {
    if (!"The Android Project".equals(
    System.getProperty("java.specification.vendor"))) {
    return new Bytes(Files.readAllBytes(Paths.get(filePath)));
    } else {
    // Use Android approach e.g.
    // IOUtils.toByteArray(getAssets().open(filePath));
    return new Bytes();
    }
    } catch (IOException e) {
    throw new FileException(e.getMessage());
    }
    }
    public void WriteAll(String filePath, Bytes content) {
    try {
    if (!"The Android Project".equals(
    System.getProperty("java.specification.vendor"))) {
    Path file = Paths.get(filePath);
    Files.write(file, content.toByteArray());
    } else {
    // Use Android approach
    }
    } catch (IOException e) {
    throw new FileException(e.getMessage());
    }
    }
    }
    public void UseUserManagedFilesystem() {
    BinaryFileAccess userFileAccess = new UserFileAccess();
    GlobalFileAccess.Set(userFileAccess);
    Bytes content = BinaryFile.ReadAll("assets/color.png");
    BinaryFile.WriteAll("output/color_copied_via_file_access.png", content);
    }
  • csharp
    class UserFileAccess : BinaryFileAccess
    {
    public override Bytes ReadAll(string filePath)
    {
    // E.g. use csharp approach to read files
    return new Bytes(File.ReadAllBytes(filePath));
    }
    public override void WriteAll(string filePath, Bytes content)
    {
    // E.g. use csharp approach to write files
    File.WriteAllBytes(filePath, content.ToArray());
    }
    };
    public void UseUserManagedFilesystem()
    {
    BinaryFileAccess userFileAccess = new UserFileAccess();
    GlobalFileAccess.Set(userFileAccess);
    Bytes content = BinaryFile.ReadAll("assets/color.png");
    BinaryFile.WriteAll("output/color_copied_via_file_access.png", content);
    }