• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "utils/java/jni-base.h"
18 
19 #include <jni.h>
20 #include <type_traits>
21 #include <vector>
22 
23 #include "utils/base/integral_types.h"
24 #include "utils/java/scoped_local_ref.h"
25 #include "utils/java/string_utils.h"
26 #include "utils/memory/mmap.h"
27 
28 using libtextclassifier3::JStringToUtf8String;
29 using libtextclassifier3::ScopedLocalRef;
30 
31 namespace libtextclassifier3 {
32 
ToStlString(JNIEnv * env,const jstring & str)33 std::string ToStlString(JNIEnv* env, const jstring& str) {
34   std::string result;
35   JStringToUtf8String(env, str, &result);
36   return result;
37 }
38 
GetFdFromFileDescriptor(JNIEnv * env,jobject fd)39 jint GetFdFromFileDescriptor(JNIEnv* env, jobject fd) {
40   ScopedLocalRef<jclass> fd_class(env->FindClass("java/io/FileDescriptor"),
41                                   env);
42   if (fd_class == nullptr) {
43     TC3_LOG(ERROR) << "Couldn't find FileDescriptor.";
44     return reinterpret_cast<jlong>(nullptr);
45   }
46   jfieldID fd_class_descriptor =
47       env->GetFieldID(fd_class.get(), "descriptor", "I");
48   if (fd_class_descriptor == nullptr) {
49     env->ExceptionClear();
50     fd_class_descriptor = env->GetFieldID(fd_class.get(), "fd", "I");
51   }
52   if (fd_class_descriptor == nullptr) {
53     TC3_LOG(ERROR) << "Couldn't find descriptor.";
54     return reinterpret_cast<jlong>(nullptr);
55   }
56   return env->GetIntField(fd, fd_class_descriptor);
57 }
58 
GetFdFromAssetFileDescriptor(JNIEnv * env,jobject afd)59 jint GetFdFromAssetFileDescriptor(JNIEnv* env, jobject afd) {
60   ScopedLocalRef<jclass> afd_class(
61       env->FindClass("android/content/res/AssetFileDescriptor"), env);
62   if (afd_class == nullptr) {
63     TC3_LOG(ERROR) << "Couldn't find AssetFileDescriptor.";
64     return reinterpret_cast<jlong>(nullptr);
65   }
66   jmethodID afd_class_getFileDescriptor = env->GetMethodID(
67       afd_class.get(), "getFileDescriptor", "()Ljava/io/FileDescriptor;");
68   if (afd_class_getFileDescriptor == nullptr) {
69     TC3_LOG(ERROR) << "Couldn't find getFileDescriptor.";
70     return reinterpret_cast<jlong>(nullptr);
71   }
72   jobject bundle_jfd = env->CallObjectMethod(afd, afd_class_getFileDescriptor);
73   return GetFdFromFileDescriptor(env, bundle_jfd);
74 }
75 
76 }  // namespace libtextclassifier3
77