• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STREAM_FS_STREAM_H
17 #define INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STREAM_FS_STREAM_H
18 
19 #include "stream_entity.h"
20 
21 #include <memory>
22 #include <mutex>
23 #include <optional>
24 
25 #include "filemgmt_libfs.h"
26 #include "fs_utils.h"
27 
28 namespace OHOS {
29 namespace FileManagement {
30 namespace ModuleFileIO {
31 using namespace std;
32 
33 struct WriteOptions {
34     optional<size_t> length = nullopt;
35     optional<int64_t> offset = nullopt;
36     optional<std::string> encoding = nullopt;
37 };
38 
39 struct ReadOptions {
40     optional<int64_t> length = nullopt;
41     optional<int64_t> offset = nullopt;
42 };
43 
44 class FsStream final {
45 public:
GetStreamEntity()46     StreamEntity *GetStreamEntity() const
47     {
48         return streamEntity.get();
49     }
50 
51     FsStream(const FsStream &) = delete;
52     FsStream &operator=(const FsStream &) = delete;
53 
54     std::shared_ptr<FILE> GetFilePtr();
55     FsResult<size_t> Write(const ArrayBuffer &buf, const optional<WriteOptions> &options = nullopt);
56     FsResult<size_t> Write(const string &buf, const optional<WriteOptions> &options = nullopt);
57     FsResult<size_t> Read(ArrayBuffer &buf, const optional<ReadOptions> &options = nullopt);
58     FsResult<void> Close();
59     FsResult<void> Flush();
60     FsResult<int64_t> Seek(const int64_t &offset, const optional<int32_t> &typeOpt = nullopt);
61 
62     ~FsStream() = default;
63     static FsResult<FsStream *> Constructor();
64 
65 private:
66     std::mutex mtx;
67     unique_ptr<StreamEntity> streamEntity;
FsStream(unique_ptr<StreamEntity> entity)68     explicit FsStream(unique_ptr<StreamEntity> entity) : streamEntity(move(entity)) {}
69 };
70 
71 } // namespace ModuleFileIO
72 } // namespace FileManagement
73 } // namespace OHOS
74 #endif // INTERFACES_KITS_JS_SRC_MOD_FS_CLASS_STREAM_FS_STREAM_H