• 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 #include "read_ani.h"
17 
18 #include <optional>
19 #include "ani_helper.h"
20 #include "error_handler.h"
21 #include "filemgmt_libhilog.h"
22 #include "read_core.h"
23 #include "type_converter.h"
24 
25 namespace OHOS::FileManagement::ModuleFileIO::ANI {
26 
27 using namespace std;
28 using namespace OHOS::FileManagement::ModuleFileIO;
29 
ToReadOptions(ani_env * env,ani_object obj)30 static tuple<bool, optional<ReadOptions>> ToReadOptions(ani_env *env, ani_object obj)
31 {
32     ReadOptions options;
33     ani_boolean isUndefined;
34     env->Reference_IsUndefined(obj, &isUndefined);
35     if (isUndefined) {
36         return { true, nullopt };
37     }
38 
39     auto [succOffset, offset] = AniHelper::ParseInt64Option(env, obj, "offset");
40     if (!succOffset) {
41         HILOGE("Illegal option.offset parameter");
42         return { false, nullopt };
43     }
44     options.offset = offset;
45 
46     auto [succLength, length] = AniHelper::ParseInt64Option(env, obj, "length");
47     if (!succLength) {
48         HILOGE("Illegal option.length parameter");
49         return { false, nullopt };
50     }
51     options.length = length;
52 
53     return { true, make_optional<ReadOptions>(move(options)) };
54 }
55 
ReadSync(ani_env * env,ani_class clazz,ani_double fd,ani_arraybuffer buffer,ani_object options)56 ani_double ReadAni::ReadSync(
57     ani_env *env, [[maybe_unused]] ani_class clazz, ani_double fd, ani_arraybuffer buffer, ani_object options)
58 {
59     auto [succBuf, arrayBuffer] = TypeConverter::ToArrayBuffer(env, buffer);
60     if (!succBuf) {
61         HILOGE("Failed to resolve arrayBuffer!");
62         ErrorHandler::Throw(env, EINVAL);
63         return -1;
64     }
65 
66     auto [succOp, op] = ToReadOptions(env, options);
67     if (!succOp) {
68         HILOGE("Failed to resolve options!");
69         ErrorHandler::Throw(env, EINVAL);
70         return -1;
71     }
72 
73     auto ret = ReadCore::DoRead(static_cast<int32_t>(fd), arrayBuffer, op);
74     if (!ret.IsSuccess()) {
75         HILOGE("Read file content failed!");
76         const auto &err = ret.GetError();
77         ErrorHandler::Throw(env, err);
78         return -1;
79     }
80     return static_cast<double>(ret.GetData().value());
81 }
82 } // namespace OHOS::FileManagement::ModuleFileIO::ANI