• 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 "stream_wrapper.h"
17 
18 #include <string>
19 
20 #include "ani_helper.h"
21 #include "ani_signature.h"
22 #include "filemgmt_libhilog.h"
23 #include "type_converter.h"
24 
25 namespace OHOS {
26 namespace FileManagement {
27 namespace ModuleFileIO {
28 namespace ANI {
29 using namespace std;
30 using namespace OHOS::FileManagement::ModuleFileIO;
31 using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature;
32 
Unwrap(ani_env * env,ani_object object)33 FsStream *StreamWrapper::Unwrap(ani_env *env, ani_object object)
34 {
35     ani_long nativePtr;
36     auto ret = env->Object_GetFieldByName_Long(object, "nativePtr", &nativePtr);
37     if (ret != ANI_OK) {
38         HILOGE("Unwrap fsFile err: %{private}d", ret);
39         return nullptr;
40     }
41     uintptr_t ptrValue = static_cast<uintptr_t>(nativePtr);
42     FsStream *stream = reinterpret_cast<FsStream *>(ptrValue);
43     return stream;
44 }
45 
Wrap(ani_env * env,const FsStream * stream)46 ani_object StreamWrapper::Wrap(ani_env *env, const FsStream *stream)
47 {
48     if (stream == nullptr) {
49         HILOGE("FsStream pointer is null!");
50         return nullptr;
51     }
52     auto classDesc = FS::StreamInner::classDesc.c_str();
53     ani_class cls;
54     if (ANI_OK != env->FindClass(classDesc, &cls)) {
55         HILOGE("Cannot find class %s", classDesc);
56         return nullptr;
57     }
58     auto ctorDesc = FS::StreamInner::ctorDesc.c_str();
59     auto ctorSig = FS::StreamInner::ctorSig.c_str();
60     ani_method ctor;
61     if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) {
62         HILOGE("Cannot find constructor method for class %s", classDesc);
63         return nullptr;
64     }
65     ani_long ptr = static_cast<ani_long>(reinterpret_cast<std::uintptr_t>(stream));
66     ani_object obj;
67     if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) {
68         HILOGE("New %s obj Failed!", classDesc);
69         return nullptr;
70     }
71 
72     return obj;
73 }
74 
75 } // namespace ANI
76 } // namespace ModuleFileIO
77 } // namespace FileManagement
78 } // namespace OHOS