• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 boolean array converted from the given bool array.
30 BASE_EXPORT ScopedJavaLocalRef<jbooleanArray>
31 ToJavaBooleanArray(JNIEnv* env, const bool* bools, size_t len);
32 
33 // Returns a new Java int array converted from the given int array.
34 BASE_EXPORT ScopedJavaLocalRef<jintArray> ToJavaIntArray(
35     JNIEnv* env, const int* ints, size_t len);
36 
37 BASE_EXPORT ScopedJavaLocalRef<jintArray> ToJavaIntArray(
38     JNIEnv* env, const std::vector<int>& ints);
39 
40 // Returns a new Java long array converted from the given int64_t array.
41 BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray(JNIEnv* env,
42                                                            const int64_t* longs,
43                                                            size_t len);
44 
45 BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray(
46     JNIEnv* env,
47     const std::vector<int64_t>& longs);
48 
49 // Returns a new Java float array converted from the given C++ float array.
50 BASE_EXPORT ScopedJavaLocalRef<jfloatArray> ToJavaFloatArray(
51     JNIEnv* env, const float* floats, size_t len);
52 
53 BASE_EXPORT ScopedJavaLocalRef<jfloatArray> ToJavaFloatArray(
54     JNIEnv* env,
55     const std::vector<float>& floats);
56 
57 // Returns a array of Java byte array converted from |v|.
58 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfByteArray(
59     JNIEnv* env, const std::vector<std::string>& v);
60 
61 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings(
62     JNIEnv* env,  const std::vector<std::string>& v);
63 
64 BASE_EXPORT ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings(
65     JNIEnv* env,  const std::vector<string16>& v);
66 
67 // Converts a Java string array to a native array.
68 BASE_EXPORT void AppendJavaStringArrayToStringVector(
69     JNIEnv* env,
70     jobjectArray array,
71     std::vector<string16>* out);
72 
73 BASE_EXPORT void AppendJavaStringArrayToStringVector(
74     JNIEnv* env,
75     jobjectArray array,
76     std::vector<std::string>* out);
77 
78 // Appends the Java bytes in |bytes_array| onto the end of |out|.
79 BASE_EXPORT void AppendJavaByteArrayToByteVector(JNIEnv* env,
80                                                  jbyteArray byte_array,
81                                                  std::vector<uint8_t>* out);
82 
83 // Replaces the content of |out| with the Java bytes in |bytes_array|.
84 BASE_EXPORT void JavaByteArrayToByteVector(JNIEnv* env,
85                                            jbyteArray byte_array,
86                                            std::vector<uint8_t>* out);
87 
88 // Replaces the content of |out| with the Java booleans in |boolean_array|.
89 BASE_EXPORT void JavaBooleanArrayToBoolVector(JNIEnv* env,
90                                               jbooleanArray boolean_array,
91                                               std::vector<bool>* out);
92 
93 // Replaces the content of |out| with the Java ints in |int_array|.
94 BASE_EXPORT void JavaIntArrayToIntVector(
95     JNIEnv* env,
96     jintArray int_array,
97     std::vector<int>* out);
98 
99 // Replaces the content of |out| with the Java longs in |long_array|.
100 BASE_EXPORT void JavaLongArrayToInt64Vector(JNIEnv* env,
101                                             jlongArray long_array,
102                                             std::vector<int64_t>* out);
103 
104 // Replaces the content of |out| with the Java longs in |long_array|.
105 BASE_EXPORT void JavaLongArrayToLongVector(
106     JNIEnv* env,
107     jlongArray long_array,
108     std::vector<jlong>* out);
109 
110 // Replaces the content of |out| with the Java floats in |float_array|.
111 BASE_EXPORT void JavaFloatArrayToFloatVector(
112     JNIEnv* env,
113     jfloatArray float_array,
114     std::vector<float>* out);
115 
116 // Assuming |array| is an byte[][] (array of byte arrays), replaces the
117 // content of |out| with the corresponding vector of strings. No UTF-8
118 // conversion is performed.
119 BASE_EXPORT void JavaArrayOfByteArrayToStringVector(
120     JNIEnv* env,
121     jobjectArray array,
122     std::vector<std::string>* out);
123 
124 // Assuming |array| is an int[][] (array of int arrays), replaces the
125 // contents of |out| with the corresponding vectors of ints.
126 BASE_EXPORT void JavaArrayOfIntArrayToIntVector(
127     JNIEnv* env,
128     jobjectArray array,
129     std::vector<std::vector<int>>* out);
130 
131 }  // namespace android
132 }  // namespace base
133 
134 #endif  // BASE_ANDROID_JNI_ARRAY_H_
135