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 a9344782075002c1968da4b8b23c982b9553b944..2f7f577102369d05002d9cfff1b9b8e5123b09e5 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 231723d337abab42a477ee2cedee1e0113ba67f8..26466b89fd0855a6a196c3b39f15481d3e1fb262 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 52fa7d7b52cbdc6b63ac15b12535ee833e078a1b..6280e0082bf8e33a7b6768e8d58f101d555754dc 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 b91367ae88f7442984bbe5db6e3fedfbb91401e6..81caa2f54fa2ba354efeb3d403ca8ef8567e1247 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_;