1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_FS_RANDOMACCESSFILE_H 17 #define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_FS_RANDOMACCESSFILE_H 18 19 #include "randomaccessfile_entity.h" 20 21 #include "filemgmt_libfs.h" 22 23 namespace OHOS { 24 namespace FileManagement { 25 namespace ModuleFileIO { 26 using namespace std; 27 28 struct WriteOptions { 29 optional<size_t> length = nullopt; 30 optional<int64_t> offset = nullopt; 31 optional<string> encoding = nullopt; 32 }; 33 34 struct ReadOptions { 35 optional<int64_t> offset = nullopt; 36 optional<int64_t> length = nullopt; 37 }; 38 39 class FsRandomAccessFile { 40 public: 41 inline static const string className_ = "RandomAccessFile"; 42 GetRAFEntity()43 RandomAccessFileEntity *GetRAFEntity() const 44 { 45 return rafEntity.get(); 46 } 47 48 FsRandomAccessFile(const FsRandomAccessFile &other) = delete; 49 FsRandomAccessFile &operator=(const FsRandomAccessFile &other) = delete; 50 FsRandomAccessFile(FsRandomAccessFile && other)51 FsRandomAccessFile(FsRandomAccessFile &&other) noexcept : rafEntity(move(other.rafEntity)) 52 { 53 other.rafEntity = nullptr; 54 } 55 56 FsRandomAccessFile &operator=(FsRandomAccessFile &&other) noexcept 57 { 58 if (this != &other) { 59 rafEntity = move(other.rafEntity); 60 other.rafEntity = nullptr; 61 } 62 return *this; 63 } 64 65 ~FsRandomAccessFile() = default; 66 67 FsResult<void> SetFilePointerSync(const int64_t &fp) const; 68 FsResult<int64_t> WriteSync(const string &buffer, const optional<WriteOptions> &options = nullopt) const; 69 FsResult<int64_t> WriteSync(const ArrayBuffer &buffer, const optional<WriteOptions> &options = nullopt) const; 70 FsResult<int64_t> ReadSync(ArrayBuffer &buffer, const optional<ReadOptions> &options = nullopt) const; 71 FsResult<void> CloseSync() const; 72 FsResult<int32_t> GetFD() const; 73 FsResult<int64_t> GetFPointer() const; 74 75 static FsResult<FsRandomAccessFile *> Constructor(); 76 77 private: 78 unique_ptr<RandomAccessFileEntity> rafEntity; FsRandomAccessFile(unique_ptr<RandomAccessFileEntity> entity)79 explicit FsRandomAccessFile(unique_ptr<RandomAccessFileEntity> entity) : rafEntity(move(entity)) {} 80 }; 81 } // namespace ModuleFileIO 82 } // namespace FileManagement 83 } // namespace OHOS 84 #endif //INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_RANDOMACCESSFILE_FS_RANDOMACCESSFILE_H