1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_ANDROID_JNI_ARRAY_H_ 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/strings/string16.h" 16 17 namespace base { 18 namespace android { 19 20 // Returns a new Java byte array converted from the given bytes array. 21 BASE_EXPORT ScopedJavaLocalRef<jbyteArray> ToJavaByteArray(JNIEnv* env, 22 const uint8_t* bytes, 23 size_t len); 24 25 BASE_EXPORT ScopedJavaLocalRef<jbyteArray> ToJavaByteArray( 26 JNIEnv* env, 27 const std::vector<uint8_t>& bytes); 28 29 // Returns a new Java int array converted from the given int array. 30 BASE_EXPORT ScopedJavaLocalRef<jintArray> ToJavaIntArray( 31 JNIEnv* env, const int* ints, size_t len); 32 33 BASE_EXPORT ScopedJavaLocalRef<jintArray> ToJavaIntArray( 34 JNIEnv* env, const std::vector<int>& ints); 35 36 // Returns a new Java long array converted from the given int64_t array. 37 BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray(JNIEnv* env, 38 const int64_t* longs, 39 size_t len); 40 41 BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray( 42 JNIEnv* env, 43 const std::vector<int64_t>& longs); 44 45 // Returns a new Java float array converted from the given C++ float array. 46 BASE_EXPORT ScopedJavaLocalRef<jfloatArray> ToJavaFloatArray( 47 JNIEnv* env, const float* floats, size_t len); 48 49 BASE_EXPORT ScopedJavaLocalRef<jfloatArray> ToJavaFloatArray( 50 JNIEnv* env, 51 const std::vector<float>& floats); 52 53 // Returns a array of Java byte array converted from |v|. 54 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfByteArray( 55 JNIEnv* env, const std::vector<std::string>& v); 56 57 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings( 58 JNIEnv* env, const std::vector<std::string>& v); 59 60 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings( 61 JNIEnv* env, const std::vector<string16>& v); 62 63 // Converts a Java string array to a native array. 64 BASE_EXPORT void AppendJavaStringArrayToStringVector( 65 JNIEnv* env, 66 jobjectArray array, 67 std::vector<string16>* out); 68 69 BASE_EXPORT void AppendJavaStringArrayToStringVector( 70 JNIEnv* env, 71 jobjectArray array, 72 std::vector<std::string>* out); 73 74 // Appends the Java bytes in |bytes_array| onto the end of |out|. 75 BASE_EXPORT void AppendJavaByteArrayToByteVector(JNIEnv* env, 76 jbyteArray byte_array, 77 std::vector<uint8_t>* out); 78 79 // Replaces the content of |out| with the Java bytes in |bytes_array|. 80 BASE_EXPORT void JavaByteArrayToByteVector(JNIEnv* env, 81 jbyteArray byte_array, 82 std::vector<uint8_t>* out); 83 84 // Replaces the content of |out| with the Java ints in |int_array|. 85 BASE_EXPORT void JavaIntArrayToIntVector( 86 JNIEnv* env, 87 jintArray int_array, 88 std::vector<int>* out); 89 90 // Replaces the content of |out| with the Java longs in |long_array|. 91 BASE_EXPORT void JavaLongArrayToInt64Vector(JNIEnv* env, 92 jlongArray long_array, 93 std::vector<int64_t>* out); 94 95 // Replaces the content of |out| with the Java longs in |long_array|. 96 BASE_EXPORT void JavaLongArrayToLongVector( 97 JNIEnv* env, 98 jlongArray long_array, 99 std::vector<jlong>* out); 100 101 // Replaces the content of |out| with the Java floats in |float_array|. 102 BASE_EXPORT void JavaFloatArrayToFloatVector( 103 JNIEnv* env, 104 jfloatArray float_array, 105 std::vector<float>* out); 106 107 // Assuming |array| is an byte[][] (array of byte arrays), replaces the 108 // content of |out| with the corresponding vector of strings. No UTF-8 109 // conversion is performed. 110 BASE_EXPORT void JavaArrayOfByteArrayToStringVector( 111 JNIEnv* env, 112 jobjectArray array, 113 std::vector<std::string>* out); 114 115 // Assuming |array| is an int[][] (array of int arrays), replaces the 116 // contents of |out| with the corresponding vectors of ints. 117 BASE_EXPORT void JavaArrayOfIntArrayToIntVector( 118 JNIEnv* env, 119 jobjectArray array, 120 std::vector<std::vector<int>>* out); 121 122 } // namespace android 123 } // namespace base 124 125 #endif // BASE_ANDROID_JNI_ARRAY_H_ 126