From efb1dca068a5ff989b8919505005fdebad4fa97b Mon Sep 17 00:00:00 2001 From: liujk Date: Wed, 21 Aug 2024 20:56:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8NAPI=E5=81=9AArrayBuffer?= =?UTF-8?q?=E5=92=8Cstring=E7=9A=84=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liujk --- .../flutter/src/main/ets/util/StringUtils.ets | 11 ++-- shell/platform/ohos/library_loader.cpp | 6 +++ .../ohos/napi/platform_view_ohos_napi.cpp | 50 +++++++++++++++++++ .../ohos/napi/platform_view_ohos_napi.h | 6 +++ 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/StringUtils.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/StringUtils.ets index a934478207..2f7f577102 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/StringUtils.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/util/StringUtils.ets @@ -13,7 +13,7 @@ * limitations under the License. */ -import util from '@ohos.util' +import flutter from 'libflutter.so' /** * 默认字符串工具 @@ -23,24 +23,21 @@ export default class StringUtils { if(str.length == 0){ return new ArrayBuffer(0); } - let textEncoder = new util.TextEncoder("utf-8"); - return textEncoder.encodeInto(str).buffer; + return flutter.encodeUtf8(str).buffer; } static arrayBufferToString(buffer: ArrayBuffer): string { if (buffer.byteLength <= 0) { return ""; } - let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM : true }) - return textDecoder.decode(new Uint8Array(buffer)); + return flutter.decodeUtf8(new Uint8Array(buffer)); } static uint8ArrayToString(buffer: Uint8Array): string { if (buffer.length <= 0) { return ""; } - let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM : true }) - return textDecoder.decodeWithStream(buffer); + return flutter.decodeUtf8(buffer); } static isNotEmpty(str: string): boolean { diff --git a/shell/platform/ohos/library_loader.cpp b/shell/platform/ohos/library_loader.cpp index 231723d337..26466b89fd 100644 --- a/shell/platform/ohos/library_loader.cpp +++ b/shell/platform/ohos/library_loader.cpp @@ -125,6 +125,12 @@ static napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_FUNCTION( "nativeRegisterPixelMap", flutter::PlatformViewOHOSNapi::nativeRegisterPixelMap), + DECLARE_NAPI_FUNCTION( + "encodeUtf8", + flutter::PlatformViewOHOSNapi::nativeEncodeUtf8), + DECLARE_NAPI_FUNCTION( + "decodeUtf8", + flutter::PlatformViewOHOSNapi::nativeDecodeUtf8), }; diff --git a/shell/platform/ohos/napi/platform_view_ohos_napi.cpp b/shell/platform/ohos/napi/platform_view_ohos_napi.cpp index 52fa7d7b52..6280e0082b 100644 --- a/shell/platform/ohos/napi/platform_view_ohos_napi.cpp +++ b/shell/platform/ohos/napi/platform_view_ohos_napi.cpp @@ -1709,4 +1709,54 @@ napi_value PlatformViewOHOSNapi::nativeXComponentDispatchMouseWheel(napi_env env return nullptr; } +/** + * @brief flutterEngine convert string to ArrayBuffer + * @note + * @param str: string + * @return napi_value + */ +napi_value PlatformViewOHOSNapi::nativeEncodeUtf8(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + size_t length = 0; + napi_get_value_string_utf8(env, args[0], nullptr, 0, &length); + + auto null_terminated_length = length + 1; + auto char_array = std::make_unique(null_terminated_length); + napi_get_value_string_utf8(env, args[0], char_array.get(), null_terminated_length, nullptr); + + void *data; + napi_value arraybuffer; + napi_create_arraybuffer(env, length, &data, &arraybuffer); + std::memcpy(data, char_array.get(), length); + + napi_value uint8_array; + napi_create_typedarray(env, napi_uint8_array, length, arraybuffer, 0, &uint8_array); + return uint8_array; +} + +/** + * @brief flutterEngine convert Uint8Array to string + * @note + * @param array: Uint8Array + * @return napi_value + */ +napi_value PlatformViewOHOSNapi::nativeDecodeUtf8(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + size_t size = 0; + void *data = nullptr; + napi_get_typedarray_info(env, args[0], nullptr, &size, &data, nullptr, nullptr); + + napi_value result; + napi_create_string_utf8(env, static_cast(data), size, &result); + return result; +} + } // namespace flutter \ No newline at end of file diff --git a/shell/platform/ohos/napi/platform_view_ohos_napi.h b/shell/platform/ohos/napi/platform_view_ohos_napi.h index b91367ae88..81caa2f54f 100644 --- a/shell/platform/ohos/napi/platform_view_ohos_napi.h +++ b/shell/platform/ohos/napi/platform_view_ohos_napi.h @@ -194,6 +194,12 @@ class PlatformViewOHOSNapi { static napi_value nativeXComponentDispatchMouseWheel( napi_env env, napi_callback_info info); + static napi_value nativeEncodeUtf8( + napi_env env, + napi_callback_info info); + static napi_value nativeDecodeUtf8( + napi_env env, + napi_callback_info info); private: static napi_env env_; -- Gitee