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 "reader_iterator_ani.h"
17
18 #include "ani_signature.h"
19 #include "error_handler.h"
20 #include "filemgmt_libhilog.h"
21 #include "reader_iterator_result_ani.h"
22 #include "type_converter.h"
23
24 namespace OHOS {
25 namespace FileManagement {
26 namespace ModuleFileIO {
27 namespace ANI {
28 using namespace std;
29 using namespace OHOS::FileManagement::ModuleFileIO;
30 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
31
Wrap(ani_env * env,const FsReaderIterator * it)32 ani_object ReaderIteratorAni::Wrap(ani_env *env, const FsReaderIterator *it)
33 {
34 if (it == nullptr) {
35 HILOGE("FsReaderIterator pointer is null!");
36 return nullptr;
37 }
38
39 auto classDesc = FS::ReaderIteratorInner::classDesc.c_str();
40 ani_class cls;
41 if (ANI_OK != env->FindClass(classDesc, &cls)) {
42 HILOGE("Cannot find class %s", classDesc);
43 return nullptr;
44 }
45
46 auto ctorDesc = FS::ReaderIteratorInner::ctorDesc.c_str();
47 auto ctorSig = FS::ReaderIteratorInner::ctorSig.c_str();
48 ani_method ctor;
49 if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) {
50 HILOGE("Cannot find constructor method for class %s", classDesc);
51 return nullptr;
52 }
53
54 ani_long ptr = static_cast<ani_long>(reinterpret_cast<std::uintptr_t>(it));
55 ani_object obj;
56 if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) {
57 HILOGE("New %s obj Failed!", classDesc);
58 return nullptr;
59 }
60
61 return obj;
62 }
63
Unwrap(ani_env * env,ani_object object)64 FsReaderIterator *ReaderIteratorAni::Unwrap(ani_env *env, ani_object object)
65 {
66 ani_long nativePtr;
67 auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr);
68 if (ret != ANI_OK) {
69 HILOGE("Unwrap fsReaderIterator err: %{private}d", ret);
70 return nullptr;
71 }
72 uintptr_t ptrValue = static_cast<uintptr_t>(nativePtr);
73 FsReaderIterator *readeriterator = reinterpret_cast<FsReaderIterator *>(ptrValue);
74 return readeriterator;
75 }
76
Next(ani_env * env,ani_object object)77 ani_object ReaderIteratorAni::Next(ani_env *env, [[maybe_unused]] ani_object object)
78 {
79 auto fsReaderIterator = Unwrap(env, object);
80 if (fsReaderIterator == nullptr) {
81 ErrorHandler::Throw(env, UNKNOWN_ERR);
82 return nullptr;
83 }
84
85 auto ret = fsReaderIterator->Next();
86 if (!ret.IsSuccess()) {
87 HILOGE("Cannot get readeriterator next!");
88 const auto &err = ret.GetError();
89 ErrorHandler::Throw(env, err);
90 return nullptr;
91 }
92
93 auto nextRet = ret.GetData().value();
94 auto result = ReaderIteratorResultAni::Wrap(env, &nextRet);
95 if (result == nullptr) {
96 ErrorHandler::Throw(env, UNKNOWN_ERR);
97 return nullptr;
98 }
99 return result;
100 }
101
102 } // namespace ANI
103 } // namespace ModuleFileIO
104 } // namespace FileManagement
105 } // namespace OHOS