• 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 "JniConstants.h"
18 #include "toStringArray.h"
19 
20 #include <string>
21 #include <vector>
22 
23 struct VectorCounter {
24     const std::vector<std::string>& strings;
VectorCounterVectorCounter25     VectorCounter(const std::vector<std::string>& strings) : strings(strings) {}
operator ()VectorCounter26     size_t operator()() {
27         return strings.size();
28     }
29 };
30 struct VectorGetter {
31     const std::vector<std::string>& strings;
VectorGetterVectorGetter32     VectorGetter(const std::vector<std::string>& strings) : strings(strings) {}
operator ()VectorGetter33     const char* operator()(size_t i) {
34         return strings[i].c_str();
35     }
36 };
37 
toStringArray(JNIEnv * env,const std::vector<std::string> & strings)38 jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings) {
39     VectorCounter counter(strings);
40     VectorGetter getter(strings);
41     return toStringArray<VectorCounter, VectorGetter>(env, &counter, &getter);
42 }
43 
44 struct ArrayCounter {
45     const char* const* strings;
ArrayCounterArrayCounter46     ArrayCounter(const char* const* strings) : strings(strings) {}
operator ()ArrayCounter47     size_t operator()() {
48         size_t count = 0;
49         while (strings[count] != NULL) {
50             ++count;
51         }
52         return count;
53     }
54 };
55 
56 struct ArrayGetter {
57     const char* const* strings;
ArrayGetterArrayGetter58     ArrayGetter(const char* const* strings) : strings(strings) {}
operator ()ArrayGetter59     const char* operator()(size_t i) {
60         return strings[i];
61     }
62 };
63 
toStringArray(JNIEnv * env,const char * const * strings)64 jobjectArray toStringArray(JNIEnv* env, const char* const* strings) {
65     ArrayCounter counter(strings);
66     ArrayGetter getter(strings);
67     return toStringArray(env, &counter, &getter);
68 }
69 
70 /** Converts a Java String[] to a 0-terminated char**. */
convertStrings(JNIEnv * env,jobjectArray javaArray)71 char** convertStrings(JNIEnv* env, jobjectArray javaArray) {
72     if (javaArray == NULL) {
73         return NULL;
74     }
75 
76     jsize length = env->GetArrayLength(javaArray);
77     char** array = new char*[length + 1];
78     array[length] = 0;
79     for (jsize i = 0; i < length; ++i) {
80         ScopedLocalRef<jstring> javaEntry(env, reinterpret_cast<jstring>(env->GetObjectArrayElement(javaArray, i)));
81         // We need to pass these strings to const-unfriendly code.
82         char* entry = const_cast<char*>(env->GetStringUTFChars(javaEntry.get(), NULL));
83         array[i] = entry;
84     }
85 
86     return array;
87 }
88 
89 /** Frees a char** which was converted from a Java String[]. */
freeStrings(JNIEnv * env,jobjectArray javaArray,char ** array)90 void freeStrings(JNIEnv* env, jobjectArray javaArray, char** array) {
91     if (javaArray == NULL) {
92         return;
93     }
94 
95     jsize length = env->GetArrayLength(javaArray);
96     for (jsize i = 0; i < length; ++i) {
97         ScopedLocalRef<jstring> javaEntry(env, reinterpret_cast<jstring>(env->GetObjectArrayElement(javaArray, i)));
98         env->ReleaseStringUTFChars(javaEntry.get(), array[i]);
99     }
100 
101     delete[] array;
102 }
103