1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_ANDROID_JNI_ARRAY_H_
6 #define BASE_ANDROID_JNI_ARRAY_H_
15 #include "base/android/scoped_java_ref.h"
16 #include "base/check_op.h"
17 #include "base/containers/span.h"
19 namespace base::android {
21 // As |GetArrayLength| makes no guarantees about the returned value (e.g., it
22 // may be -1 if |array| is not a valid Java array), provide a safe wrapper
23 // that always returns a valid, non-negative size.
24 // Returns the length of Java array.
25 template <typename JavaArrayType>
26 BASE_EXPORT size_t SafeGetArrayLength(JNIEnv* env,
27 const JavaRef<JavaArrayType>& jarray) {
29 jsize length = env->GetArrayLength(jarray.obj());
30 DCHECK_GE(length, 0) << "Invalid array length: " << length;
31 return static_cast<size_t>(std::max(0, length));
34 // Returns a new Java byte array converted from the given bytes array.
35 BASE_EXPORT ScopedJavaLocalRef<jbyteArray> ToJavaByteArray(JNIEnv* env,
39 BASE_EXPORT ScopedJavaLocalRef<jbyteArray> ToJavaByteArray(
41 base::span<const uint8_t> bytes);
43 // Returns a new Java byte array converted from the given string. No UTF-8
44 // conversion is performed.
45 BASE_EXPORT ScopedJavaLocalRef<jbyteArray> ToJavaByteArray(
47 const std::string& str);
49 // Returns a new Java boolean array converted from the given bool array.
50 BASE_EXPORT ScopedJavaLocalRef<jbooleanArray>
51 ToJavaBooleanArray(JNIEnv* env, const bool* bools, size_t len);
53 // Returns a new Java int array converted from the given int array.
54 BASE_EXPORT ScopedJavaLocalRef<jintArray> ToJavaIntArray(
55 JNIEnv* env, const int* ints, size_t len);
57 BASE_EXPORT ScopedJavaLocalRef<jintArray> ToJavaIntArray(
59 base::span<const int> ints);
61 // Returns a new Java long array converted from the given int64_t array.
62 BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray(JNIEnv* env,
66 BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray(
68 base::span<const int64_t> longs);
70 // Returns a new Java float array converted from the given C++ float array.
71 BASE_EXPORT ScopedJavaLocalRef<jfloatArray> ToJavaFloatArray(
72 JNIEnv* env, const float* floats, size_t len);
74 BASE_EXPORT ScopedJavaLocalRef<jfloatArray> ToJavaFloatArray(
76 base::span<const float> floats);
78 // Returns a new Java double array converted from the given C++ double array.
79 BASE_EXPORT ScopedJavaLocalRef<jdoubleArray>
80 ToJavaDoubleArray(JNIEnv* env, const double* doubles, size_t len);
82 BASE_EXPORT ScopedJavaLocalRef<jdoubleArray> ToJavaDoubleArray(
84 base::span<const double> doubles);
86 // Returns a new clazz[] with the content of |v|.
87 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfObjects(
89 ScopedJavaLocalRef<jclass> clazz,
90 base::span<const ScopedJavaLocalRef<jobject>> v);
92 // Returns a new Object[] with the content of |v|.
93 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfObjects(
95 base::span<const ScopedJavaLocalRef<jobject>> v);
96 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfObjects(
98 base::span<const ScopedJavaGlobalRef<jobject>> v);
100 // Returns a new Type[] with the content of |v|.
101 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToTypedJavaArrayOfObjects(
103 base::span<const ScopedJavaLocalRef<jobject>> v,
104 const JavaRef<jclass>& type);
105 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToTypedJavaArrayOfObjects(
107 base::span<const ScopedJavaGlobalRef<jobject>> v,
108 const JavaRef<jclass>& type);
110 // Returns a array of Java byte array converted from |v|.
111 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfByteArray(
113 base::span<const std::string> v);
115 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfByteArray(
117 base::span<const std::vector<uint8_t>> v);
119 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings(
121 base::span<const std::string> v);
123 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings(
125 base::span<const std::u16string> v);
127 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStringArray(
129 base::span<const std::vector<std::string>> v);
131 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStringArray(
133 base::span<const std::vector<std::u16string>> v);
135 // Converts a Java string array to a native array.
136 BASE_EXPORT void AppendJavaStringArrayToStringVector(
138 const JavaRef<jobjectArray>& array,
139 std::vector<std::u16string>* out);
141 BASE_EXPORT void AppendJavaStringArrayToStringVector(
143 const JavaRef<jobjectArray>& array,
144 std::vector<std::string>* out);
146 // Appends the Java bytes in |bytes_array| onto the end of |out|.
147 BASE_EXPORT void AppendJavaByteArrayToByteVector(
149 const JavaRef<jbyteArray>& byte_array,
150 std::vector<uint8_t>* out);
152 // Replaces the content of |out| with the Java bytes in |byte_array|.
153 BASE_EXPORT void JavaByteArrayToByteVector(
155 const JavaRef<jbyteArray>& byte_array,
156 std::vector<uint8_t>* out);
158 // Copy the contents of java |byte_array| into |dest|. The span must be larger
159 // than or equal to the array.
160 // Returns the number of bytes copied.
162 JavaByteArrayToByteSpan(JNIEnv* env,
163 const JavaRef<jbyteArray>& byte_array,
164 base::span<uint8_t> dest);
166 // Replaces the content of |out| with the Java bytes in |byte_array|. No UTF-8
167 // conversion is performed.
168 BASE_EXPORT void JavaByteArrayToString(JNIEnv* env,
169 const JavaRef<jbyteArray>& byte_array,
172 // Replaces the content of |out| with the Java booleans in |boolean_array|.
173 BASE_EXPORT void JavaBooleanArrayToBoolVector(
175 const JavaRef<jbooleanArray>& boolean_array,
176 std::vector<bool>* out);
178 // Replaces the content of |out| with the Java ints in |int_array|.
179 BASE_EXPORT void JavaIntArrayToIntVector(JNIEnv* env,
180 const JavaRef<jintArray>& int_array,
181 std::vector<int>* out);
183 // Replaces the content of |out| with the Java longs in |long_array|.
184 BASE_EXPORT void JavaLongArrayToInt64Vector(
186 const JavaRef<jlongArray>& long_array,
187 std::vector<int64_t>* out);
189 // Replaces the content of |out| with the Java longs in |long_array|.
190 BASE_EXPORT void JavaLongArrayToLongVector(
192 const JavaRef<jlongArray>& long_array,
193 std::vector<jlong>* out);
195 // Replaces the content of |out| with the Java floats in |float_array|.
196 BASE_EXPORT void JavaFloatArrayToFloatVector(
198 const JavaRef<jfloatArray>& float_array,
199 std::vector<float>* out);
201 // Replaces the content of |out| with the Java doubles in |double_array|.
202 BASE_EXPORT void JavaDoubleArrayToDoubleVector(
204 const JavaRef<jdoubleArray>& double_array,
205 std::vector<double>* out);
207 // Assuming |array| is an byte[][] (array of byte arrays), replaces the
208 // content of |out| with the corresponding vector of strings. No UTF-8
209 // conversion is performed.
210 BASE_EXPORT void JavaArrayOfByteArrayToStringVector(
212 const JavaRef<jobjectArray>& array,
213 std::vector<std::string>* out);
215 // Assuming |array| is an byte[][] (array of byte arrays), replaces the
216 // content of |out| with the corresponding vector of vector of uint8. No UTF-8
217 // conversion is performed.
218 BASE_EXPORT void JavaArrayOfByteArrayToBytesVector(
220 const JavaRef<jobjectArray>& array,
221 std::vector<std::vector<uint8_t>>* out);
223 // Assuming |array| is an String[][] (array of String arrays), replaces the
224 // content of |out| with the corresponding vector of string vectors.
225 BASE_EXPORT void Java2dStringArrayTo2dStringVector(
227 const JavaRef<jobjectArray>& array,
228 std::vector<std::vector<std::string>>* out);
230 // Assuming |array| is an String[][] (array of String arrays), replaces the
231 // content of |out| with the corresponding vector of string vectors. No UTF-8
232 // conversion is performed.
233 BASE_EXPORT void Java2dStringArrayTo2dStringVector(
235 const JavaRef<jobjectArray>& array,
236 std::vector<std::vector<std::u16string>>* out);
238 // Assuming |array| is an int[][] (array of int arrays), replaces the
239 // contents of |out| with the corresponding vectors of ints.
240 BASE_EXPORT void JavaArrayOfIntArrayToIntVector(
242 const JavaRef<jobjectArray>& array,
243 std::vector<std::vector<int>>* out);
245 } // namespace base::android
247 #endif // BASE_ANDROID_JNI_ARRAY_H_