jni_array.h 6.9 KB

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