| 6 | #define BASE_ANDROID_JNI_ARRAY_H_ |
| 7 |
| 8 | #include <jni.h> |
| 9 | #include <stddef.h> |
| 10 | #include <stdint.h> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 |
| 14 | #include "base/android/scoped_java_ref.h" |
| 15 | #include "base/containers/span.h" |
| 16 | #include "base/strings/string16.h" |
| 17 |
| 18 | namespace base { |
| 19 | namespace android { |
| 20 |
| 21 | // Returns a new Java byte array converted from the given bytes array. |
| 22 | BASE_EXPORT ScopedJavaLocalRef<jbyteArray> ToJavaByteArray(JNIEnv* env, |
| 23 | const uint8_t* bytes, |
| 24 | size_t len); |
| 25 |
| 26 | BASE_EXPORT ScopedJavaLocalRef<jbyteArray> ToJavaByteArray( |
| 27 | JNIEnv* env, |
| 28 | base::span<const uint8_t> bytes); |
| 29 |
| 30 | // Returns a new Java byte array converted from the given string. No UTF-8 |
| 31 | // conversion is performed. |
| 32 | BASE_EXPORT ScopedJavaLocalRef<jbyteArray> ToJavaByteArray( |
| 33 | JNIEnv* env, |
| 34 | const std::string& str); |
| 35 |
| 36 | // Returns a new Java boolean array converted from the given bool array. |
| 37 | BASE_EXPORT ScopedJavaLocalRef<jbooleanArray> |
| 38 | ToJavaBooleanArray(JNIEnv* env, const bool* bools, size_t len); |
| 39 |
| 40 | // Returns a new Java int array converted from the given int array. |
| 41 | BASE_EXPORT ScopedJavaLocalRef<jintArray> ToJavaIntArray( |
| 42 | JNIEnv* env, const int* ints, size_t len); |
| 43 |
| 44 | BASE_EXPORT ScopedJavaLocalRef<jintArray> ToJavaIntArray( |
| 45 | JNIEnv* env, |
| 46 | base::span<const int> ints); |
| 47 |
| 48 | // Returns a new Java long array converted from the given int64_t array. |
| 49 | BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray(JNIEnv* env, |
| 50 | const int64_t* longs, |
| 51 | size_t len); |
| 52 |
| 53 | BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray( |
| 54 | JNIEnv* env, |
| 55 | base::span<const int64_t> longs); |
| 56 |
| 57 | // Returns a new Java float array converted from the given C++ float array. |
| 58 | BASE_EXPORT ScopedJavaLocalRef<jfloatArray> ToJavaFloatArray( |
| 59 | JNIEnv* env, const float* floats, size_t len); |
| 60 |
| 61 | BASE_EXPORT ScopedJavaLocalRef<jfloatArray> ToJavaFloatArray( |
| 62 | JNIEnv* env, |
| 63 | base::span<const float> floats); |
| 64 |
| 65 | // Returns a new Java double array converted from the given C++ double array. |
| 66 | BASE_EXPORT ScopedJavaLocalRef<jdoubleArray> |
| 67 | ToJavaDoubleArray(JNIEnv* env, const double* doubles, size_t len); |
| 68 |
| 69 | BASE_EXPORT ScopedJavaLocalRef<jdoubleArray> ToJavaDoubleArray( |
| 70 | JNIEnv* env, |
| 71 | base::span<const double> doubles); |
| 72 |
| 73 | // Returns a array of Java byte array converted from |v|. |
| 74 | BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfByteArray( |
| 75 | JNIEnv* env, |
| 76 | base::span<const std::string> v); |
| 77 |
| 78 | BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfByteArray( |
| 79 | JNIEnv* env, |
| 80 | base::span<std::vector<uint8_t>> v); |
| 81 |
| 82 | BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings( |
| 83 | JNIEnv* env, |
| 84 | base::span<const std::string> v); |
| 85 |
| 86 | BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings( |
| 87 | JNIEnv* env, |
| 88 | base::span<const string16> v); |
| 89 |
| 90 | BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStringArray( |
| 91 | JNIEnv* env, |
| 92 | base::span<const std::vector<string16>> v); |
| 93 |
| 94 | // Converts a Java string array to a native array. |
| 95 | BASE_EXPORT void AppendJavaStringArrayToStringVector( |
| 96 | JNIEnv* env, |
| 97 | const JavaRef<jobjectArray>& array, |
| 98 | std::vector<string16>* out); |
| 99 |
| 100 | BASE_EXPORT void AppendJavaStringArrayToStringVector( |
| 101 | JNIEnv* env, |
| 102 | const JavaRef<jobjectArray>& array, |
| 103 | std::vector<std::string>* out); |
| 104 |
| 105 | // Appends the Java bytes in |bytes_array| onto the end of |out|. |
| 106 | BASE_EXPORT void AppendJavaByteArrayToByteVector( |
| 107 | JNIEnv* env, |
| 108 | const JavaRef<jbyteArray>& byte_array, |
| 109 | std::vector<uint8_t>* out); |
| 110 |
| 111 | // Replaces the content of |out| with the Java bytes in |byte_array|. |
| 112 | BASE_EXPORT void JavaByteArrayToByteVector( |
| 113 | JNIEnv* env, |
| 114 | const JavaRef<jbyteArray>& byte_array, |
| 115 | std::vector<uint8_t>* out); |
| 116 |
| 117 | // Replaces the content of |out| with the Java bytes in |byte_array|. No UTF-8 |
| 118 | // conversion is performed. |
| 119 | BASE_EXPORT void JavaByteArrayToString(JNIEnv* env, |
| 120 | const JavaRef<jbyteArray>& byte_array, |
| 121 | std::string* out); |
| 122 |
| 123 | // Replaces the content of |out| with the Java booleans in |boolean_array|. |
| 124 | BASE_EXPORT void JavaBooleanArrayToBoolVector( |
| 125 | JNIEnv* env, |
| 126 | const JavaRef<jbooleanArray>& boolean_array, |
| 127 | std::vector<bool>* out); |
| 128 |
| 129 | // Replaces the content of |out| with the Java ints in |int_array|. |
| 130 | BASE_EXPORT void JavaIntArrayToIntVector(JNIEnv* env, |
| 131 | const JavaRef<jintArray>& int_array, |
| 132 | std::vector<int>* out); |
| 133 |
| 134 | // Replaces the content of |out| with the Java longs in |long_array|. |
| 135 | BASE_EXPORT void JavaLongArrayToInt64Vector( |
| 136 | JNIEnv* env, |
| 137 | const JavaRef<jlongArray>& long_array, |
| 138 | std::vector<int64_t>* out); |
| 139 |
| 140 | // Replaces the content of |out| with the Java longs in |long_array|. |
| 141 | BASE_EXPORT void JavaLongArrayToLongVector( |
| 142 | JNIEnv* env, |
| 143 | const JavaRef<jlongArray>& long_array, |
| 144 | std::vector<jlong>* out); |
| 145 |
| 146 | // Replaces the content of |out| with the Java floats in |float_array|. |
| 147 | BASE_EXPORT void JavaFloatArrayToFloatVector( |
| 148 | JNIEnv* env, |
| 149 | const JavaRef<jfloatArray>& float_array, |
| 150 | std::vector<float>* out); |
| 151 |
| 152 | // Assuming |array| is an byte[][] (array of byte arrays), replaces the |
| 153 | // content of |out| with the corresponding vector of strings. No UTF-8 |
| 154 | // conversion is performed. |
| 155 | BASE_EXPORT void JavaArrayOfByteArrayToStringVector( |
| 156 | JNIEnv* env, |
| 157 | const JavaRef<jobjectArray>& array, |
| 158 | std::vector<std::string>* out); |
| 159 |
| 160 | // Assuming |array| is an byte[][] (array of byte arrays), replaces the |
| 161 | // content of |out| with the corresponding vector of vector of uint8. No UTF-8 |
| 162 | // conversion is performed. |
| 163 | BASE_EXPORT void JavaArrayOfByteArrayToBytesVector( |
| 164 | JNIEnv* env, |
| 165 | const JavaRef<jobjectArray>& array, |
| 166 | std::vector<std::vector<uint8_t>>* out); |
| 167 |
| 168 | // Assuming |array| is an String[][] (array of String arrays), replaces the |
| 169 | // content of |out| with the corresponding vector of string vectors. No UTF-8 |
| 170 | // conversion is performed. |
| 171 | BASE_EXPORT void Java2dStringArrayTo2dStringVector( |
| 172 | JNIEnv* env, |
| 173 | const JavaRef<jobjectArray>& array, |
| 174 | std::vector<std::vector<string16>>* out); |
| 175 |
| 176 | // Assuming |array| is an int[][] (array of int arrays), replaces the |
| 177 | // contents of |out| with the corresponding vectors of ints. |
| 178 | BASE_EXPORT void JavaArrayOfIntArrayToIntVector( |
| 179 | JNIEnv* env, |
| 180 | const JavaRef<jobjectArray>& array, |
| 181 | std::vector<std::vector<int>>* out); |
| 182 |
| 183 | } // namespace android |
| 184 | } // namespace base |
| 185 |
| 186 | #endif // BASE_ANDROID_JNI_ARRAY_H_ |
| 187 |