diff options
author | Sven Gothel <[email protected]> | 2020-07-04 01:18:04 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-07-04 01:18:04 +0200 |
commit | 7d028e850913f3ed495ce79a0350e76d5727222b (patch) | |
tree | 0cbabea3715b76bb68e629c5fe4f3b896cb87d27 /java | |
parent | fb7b8f699e8ab921af3f875ddb285bd502737818 (diff) |
Extract dfa_utf8_decode.[cpp/hpp] added to libtinyb.so, i.e. used for both native libs commonly exposed in Java via BluetoothUtils.cxx JNI
Diffstat (limited to 'java')
-rw-r--r-- | java/jni/BluetoothUtils.cxx | 28 | ||||
-rw-r--r-- | java/jni/direct_bt/CMakeLists.txt | 1 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTUtils.cxx | 57 |
3 files changed, 28 insertions, 58 deletions
diff --git a/java/jni/BluetoothUtils.cxx b/java/jni/BluetoothUtils.cxx index 83e71dd0..b05ec66f 100644 --- a/java/jni/BluetoothUtils.cxx +++ b/java/jni/BluetoothUtils.cxx @@ -30,6 +30,11 @@ #include <time.h> +#include "JNIMem.hpp" +#include "helper_base.hpp" + +#include "direct_bt/dfa_utf8_decode.hpp" + static const int64_t NanoPerMilli = 1000000L; static const int64_t MilliPerOne = 1000L; @@ -51,3 +56,26 @@ jlong Java_org_tinyb_BluetoothUtils_getCurrentMilliseconds(JNIEnv *env, jclass c return (jlong)res; } +jstring Java_org_tinyb_BluetoothUtils_decodeUTF8String(JNIEnv *env, jclass clazz, jbyteArray jbuffer, jint offset, jint size) { + (void)clazz; + + const int buffer_size = env->GetArrayLength(jbuffer); + if( 0 == buffer_size ) { + return env->NewStringUTF(""); + } + if( buffer_size < offset+size ) { + std::string msg("buffer.length "+std::to_string(buffer_size)+ + " < offset "+std::to_string(offset)+ + " + size "+std::to_string(size)); + throw std::invalid_argument(msg.c_str()); + } + + JNICriticalArray<uint8_t> criticalArray(env); // RAII - release + uint8_t * buffer_ptr = criticalArray.get(jbuffer, criticalArray.Mode::NO_UPDATE_AND_RELEASE); + if( NULL == buffer_ptr ) { + throw std::invalid_argument("GetPrimitiveArrayCritical(byte array) is null"); + } + std::string sres = dfa_utf8_decode(buffer_ptr+offset, size); + + return from_string_to_jstring(env, sres); +} diff --git a/java/jni/direct_bt/CMakeLists.txt b/java/jni/direct_bt/CMakeLists.txt index 60eacd6c..9fd40026 100644 --- a/java/jni/direct_bt/CMakeLists.txt +++ b/java/jni/direct_bt/CMakeLists.txt @@ -33,7 +33,6 @@ set (direct_bt_JNI_SRCS ${PROJECT_SOURCE_DIR}/java/jni/direct_bt/DBTGattService.cxx ${PROJECT_SOURCE_DIR}/java/jni/direct_bt/DBTManager.cxx ${PROJECT_SOURCE_DIR}/java/jni/direct_bt/DBTObject.cxx - ${PROJECT_SOURCE_DIR}/java/jni/direct_bt/DBTUtils.cxx ) set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed") diff --git a/java/jni/direct_bt/DBTUtils.cxx b/java/jni/direct_bt/DBTUtils.cxx deleted file mode 100644 index 876cd4ff..00000000 --- a/java/jni/direct_bt/DBTUtils.cxx +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Author: Sven Gothel <[email protected]> - * Copyright (c) 2020 Gothel Software e.K. - * Copyright (c) 2020 ZAFENA AB - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include "org_tinyb_BluetoothUtils.h" - -#include <cstdint> -#include <cinttypes> - -#include <time.h> - -#include "helper_base.hpp" -#include "helper_dbt.hpp" - -jstring Java_org_tinyb_BluetoothUtils_decodeUTF8String(JNIEnv *env, jclass clazz, jbyteArray jbuffer, jint offset, jint size) { - (void)clazz; - - const int buffer_size = env->GetArrayLength(jbuffer); - if( 0 == buffer_size ) { - return env->NewStringUTF(""); - } - if( buffer_size < offset+size ) { - throw direct_bt::IllegalArgumentException("buffer.length "+std::to_string(buffer_size)+ - " < offset "+std::to_string(offset)+ - " + size "+std::to_string(size), E_FILE_LINE); - } - - JNICriticalArray<uint8_t> criticalArray(env); // RAII - release - uint8_t * buffer_ptr = criticalArray.get(jbuffer, criticalArray.Mode::NO_UPDATE_AND_RELEASE); - if( NULL == buffer_ptr ) { - throw direct_bt::InternalError("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE); - } - std::string sres = direct_bt::decodeUTF8String(buffer_ptr+offset, size); - - return from_string_to_jstring(env, sres); -} |