xref: /aosp_15_r20/external/tflite-support/tensorflow_lite_support/cc/utils/jni_utils.cc (revision b16991f985baa50654c05c5adbb3c8bbcfb40082)
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
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 "tensorflow_lite_support/cc/utils/jni_utils.h"
17 
18 #include <string.h>
19 
20 namespace tflite {
21 namespace support {
22 namespace utils {
23 
JStringToString(JNIEnv * env,jstring jstr)24 std::string JStringToString(JNIEnv* env, jstring jstr) {
25   if (jstr == nullptr) {
26     return std::string();
27   }
28   const char* cstring = env->GetStringUTFChars(jstr, nullptr);
29   std::string result(cstring);
30   env->ReleaseStringUTFChars(jstr, cstring);
31   return result;
32 }
33 
StringListToVector(JNIEnv * env,jobject list_object)34 std::vector<std::string> StringListToVector(JNIEnv* env, jobject list_object) {
35   jobject j_iterator = env->CallObjectMethod(
36       list_object, env->GetMethodID(env->GetObjectClass(list_object),
37                                     "iterator", "()Ljava/util/Iterator;"));
38   std::vector<std::string> result;
39   jmethodID has_next =
40       env->GetMethodID(env->GetObjectClass(j_iterator), "hasNext", "()Z");
41   jmethodID get_next = env->GetMethodID(env->GetObjectClass(j_iterator), "next",
42                                         "()Ljava/lang/Object;");
43   while (env->CallBooleanMethod(j_iterator, has_next)) {
44     jstring jstr =
45         static_cast<jstring>(env->CallObjectMethod(j_iterator, get_next));
46     const char* raw_str = env->GetStringUTFChars(jstr, JNI_FALSE);
47     result.emplace_back(std::string(raw_str));
48     env->ReleaseStringUTFChars(jstr, raw_str);
49   }
50   return result;
51 }
52 
GetMappedFileBuffer(JNIEnv * env,const jobject & file_buffer)53 absl::string_view GetMappedFileBuffer(JNIEnv* env, const jobject& file_buffer) {
54   return absl::string_view(
55       static_cast<char*>(env->GetDirectBufferAddress(file_buffer)),
56       static_cast<size_t>(env->GetDirectBufferCapacity(file_buffer)));
57 }
58 
CreateByteArray(JNIEnv * env,const jbyte * data,int num_bytes)59 jbyteArray CreateByteArray(JNIEnv* env, const jbyte* data, int num_bytes) {
60   jbyteArray ret = env->NewByteArray(num_bytes);
61   env->SetByteArrayRegion(ret, 0, num_bytes, data);
62 
63   return ret;
64 }
65 
ThrowException(JNIEnv * env,const char * clazz,const char * fmt,...)66 void ThrowException(JNIEnv* env, const char* clazz, const char* fmt, ...) {
67   va_list args;
68   va_start(args, fmt);
69   const size_t max_msg_len = 512;
70   auto* message = static_cast<char*>(malloc(max_msg_len));
71   if (message && (vsnprintf(message, max_msg_len, fmt, args) >= 0)) {
72     ThrowExceptionWithMessage(env, clazz, message);
73   } else {
74     ThrowExceptionWithMessage(env, clazz, "");
75   }
76   if (message) {
77     free(message);
78   }
79   va_end(args);
80 }
81 
ThrowExceptionWithMessage(JNIEnv * env,const char * clazz,const char * message)82 void ThrowExceptionWithMessage(JNIEnv* env, const char* clazz,
83                                const char* message) {
84   jclass e_class = env->FindClass(clazz);
85   if (strcmp(clazz, kAssertionError) == 0) {
86     // AssertionError cannot use ThrowNew in Java 7
87     jmethodID constructor =
88         env->GetMethodID(e_class, "<init>", "(Ljava/lang/Object;)V");
89     jstring jstr_message = env->NewStringUTF(message);
90     jobject e_object = env->NewObject(e_class, constructor,
91                                       static_cast<jobject>(jstr_message));
92     env->Throw(static_cast<jthrowable>(e_object));
93     return;
94   }
95   env->ThrowNew(e_class, message);
96 }
97 
98 }  // namespace utils
99 }  // namespace support
100 }  // namespace tflite
101