• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 "jni.h"
18 #include "ScopedLocalRef.h"
19 
20 #include <string>
21 #include <vector>
22 
23 template <typename Counter, typename Getter>
toStringArray(JNIEnv * env,Counter * counter,Getter * getter)24 jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) {
25     size_t count = (*counter)();
26     jobjectArray result = env->NewObjectArray(count, JniConstants::stringClass, NULL);
27     if (result == NULL) {
28         return NULL;
29     }
30     for (size_t i = 0; i < count; ++i) {
31         ScopedLocalRef<jstring> s(env, env->NewStringUTF((*getter)(i)));
32         if (env->ExceptionCheck()) {
33             return NULL;
34         }
35         env->SetObjectArrayElement(result, i, s.get());
36         if (env->ExceptionCheck()) {
37             return NULL;
38         }
39     }
40     return result;
41 }
42 
43 template <typename Counter, typename Getter>
toStringArray16(JNIEnv * env,Counter * counter,Getter * getter)44 jobjectArray toStringArray16(JNIEnv* env, Counter* counter, Getter* getter) {
45     size_t count = (*counter)();
46     jobjectArray result = env->NewObjectArray(count, JniConstants::stringClass, NULL);
47     if (result == NULL) {
48         return NULL;
49     }
50     for (size_t i = 0; i < count; ++i) {
51         int32_t charCount;
52         const jchar* chars = (*getter)(&charCount);
53         ScopedLocalRef<jstring> s(env, env->NewString(chars, charCount));
54         if (env->ExceptionCheck()) {
55             return NULL;
56         }
57         env->SetObjectArrayElement(result, i, s.get());
58         if (env->ExceptionCheck()) {
59             return NULL;
60         }
61     }
62     return result;
63 }
64 
65 jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings);
66 jobjectArray toStringArray(JNIEnv* env, const char* const* strings);
67 
68 char** convertStrings(JNIEnv* env, jobjectArray javaArray);
69 void freeStrings(JNIEnv* env, jobjectArray javaArray, char** array);
70