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.
5 #include "base/android/jni_array.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/logging.h"
14 ScopedJavaLocalRef
<jbyteArray
> ToJavaByteArray(
15 JNIEnv
* env
, const uint8
* bytes
, size_t len
) {
16 jbyteArray byte_array
= env
->NewByteArray(len
);
20 env
->SetByteArrayRegion(
21 byte_array
, 0, len
, reinterpret_cast<const jbyte
*>(bytes
));
24 return ScopedJavaLocalRef
<jbyteArray
>(env
, byte_array
);
27 ScopedJavaLocalRef
<jintArray
> ToJavaIntArray(
28 JNIEnv
* env
, const int* ints
, size_t len
) {
29 jintArray int_array
= env
->NewIntArray(len
);
33 env
->SetIntArrayRegion(
34 int_array
, 0, len
, reinterpret_cast<const jint
*>(ints
));
37 return ScopedJavaLocalRef
<jintArray
>(env
, int_array
);
40 ScopedJavaLocalRef
<jintArray
> ToJavaIntArray(
41 JNIEnv
* env
, const std::vector
<int>& ints
) {
42 return ToJavaIntArray(env
, ints
.begin(), ints
.size());
45 ScopedJavaLocalRef
<jlongArray
> ToJavaLongArray(
46 JNIEnv
* env
, const int64
* longs
, size_t len
) {
47 jlongArray long_array
= env
->NewLongArray(len
);
51 env
->SetLongArrayRegion(
52 long_array
, 0, len
, reinterpret_cast<const jlong
*>(longs
));
55 return ScopedJavaLocalRef
<jlongArray
>(env
, long_array
);
58 // Returns a new Java long array converted from the given int64 array.
59 BASE_EXPORT ScopedJavaLocalRef
<jlongArray
> ToJavaLongArray(
60 JNIEnv
* env
, const std::vector
<int64
>& longs
) {
61 return ToJavaLongArray(env
, longs
.begin(), longs
.size());
64 ScopedJavaLocalRef
<jobjectArray
> ToJavaArrayOfByteArray(
65 JNIEnv
* env
, const std::vector
<std::string
>& v
) {
66 ScopedJavaLocalRef
<jclass
> byte_array_clazz
= GetClass(env
, "[B");
67 jobjectArray joa
= env
->NewObjectArray(v
.size(),
68 byte_array_clazz
.obj(), NULL
);
71 for (size_t i
= 0; i
< v
.size(); ++i
) {
72 ScopedJavaLocalRef
<jbyteArray
> byte_array
= ToJavaByteArray(env
,
73 reinterpret_cast<const uint8
*>(v
[i
].data()), v
[i
].length());
74 env
->SetObjectArrayElement(joa
, i
, byte_array
.obj());
76 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
79 ScopedJavaLocalRef
<jobjectArray
> ToJavaArrayOfStrings(
80 JNIEnv
* env
, const std::vector
<std::string
>& v
) {
81 ScopedJavaLocalRef
<jclass
> string_clazz
= GetClass(env
, "java/lang/String");
82 jobjectArray joa
= env
->NewObjectArray(v
.size(), string_clazz
.obj(), NULL
);
85 for (size_t i
= 0; i
< v
.size(); ++i
) {
86 ScopedJavaLocalRef
<jstring
> item
= ConvertUTF8ToJavaString(env
, v
[i
]);
87 env
->SetObjectArrayElement(joa
, i
, item
.obj());
89 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
92 ScopedJavaLocalRef
<jobjectArray
> ToJavaArrayOfStrings(
93 JNIEnv
* env
, const std::vector
<string16
>& v
) {
94 ScopedJavaLocalRef
<jclass
> string_clazz
= GetClass(env
, "java/lang/String");
95 jobjectArray joa
= env
->NewObjectArray(v
.size(), string_clazz
.obj(), NULL
);
98 for (size_t i
= 0; i
< v
.size(); ++i
) {
99 ScopedJavaLocalRef
<jstring
> item
= ConvertUTF16ToJavaString(env
, v
[i
]);
100 env
->SetObjectArrayElement(joa
, i
, item
.obj());
102 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
105 void AppendJavaStringArrayToStringVector(JNIEnv
* env
,
107 std::vector
<string16
>* out
) {
111 jsize len
= env
->GetArrayLength(array
);
112 size_t back
= out
->size();
113 out
->resize(back
+ len
);
114 for (jsize i
= 0; i
< len
; ++i
) {
115 ScopedJavaLocalRef
<jstring
> str(env
,
116 static_cast<jstring
>(env
->GetObjectArrayElement(array
, i
)));
117 ConvertJavaStringToUTF16(env
, str
.obj(), &((*out
)[back
+ i
]));
121 void AppendJavaStringArrayToStringVector(JNIEnv
* env
,
123 std::vector
<std::string
>* out
) {
127 jsize len
= env
->GetArrayLength(array
);
128 size_t back
= out
->size();
129 out
->resize(back
+ len
);
130 for (jsize i
= 0; i
< len
; ++i
) {
131 ScopedJavaLocalRef
<jstring
> str(env
,
132 static_cast<jstring
>(env
->GetObjectArrayElement(array
, i
)));
133 ConvertJavaStringToUTF8(env
, str
.obj(), &((*out
)[back
+ i
]));
137 void AppendJavaByteArrayToByteVector(JNIEnv
* env
,
138 jbyteArray byte_array
,
139 std::vector
<uint8
>* out
) {
143 jsize len
= env
->GetArrayLength(byte_array
);
144 jbyte
* bytes
= env
->GetByteArrayElements(byte_array
, NULL
);
145 out
->insert(out
->end(), bytes
, bytes
+ len
);
146 env
->ReleaseByteArrayElements(byte_array
, bytes
, JNI_ABORT
);
149 void JavaByteArrayToByteVector(JNIEnv
* env
,
150 jbyteArray byte_array
,
151 std::vector
<uint8
>* out
) {
154 AppendJavaByteArrayToByteVector(env
, byte_array
, out
);
157 void JavaIntArrayToIntVector(JNIEnv
* env
,
159 std::vector
<int>* out
) {
162 jsize len
= env
->GetArrayLength(int_array
);
163 jint
* ints
= env
->GetIntArrayElements(int_array
, NULL
);
164 for (jsize i
= 0; i
< len
; ++i
) {
165 out
->push_back(static_cast<int>(ints
[i
]));
167 env
->ReleaseIntArrayElements(int_array
, ints
, JNI_ABORT
);
170 void JavaLongArrayToLongVector(JNIEnv
* env
,
171 jlongArray long_array
,
172 std::vector
<long>* out
) {
175 jsize len
= env
->GetArrayLength(long_array
);
176 jlong
* longs
= env
->GetLongArrayElements(long_array
, NULL
);
177 for (jsize i
= 0; i
< len
; ++i
) {
178 out
->push_back(static_cast<long>(longs
[i
]));
180 env
->ReleaseLongArrayElements(long_array
, longs
, JNI_ABORT
);
183 void JavaFloatArrayToFloatVector(JNIEnv
* env
,
184 jfloatArray float_array
,
185 std::vector
<float>* out
) {
188 jsize len
= env
->GetArrayLength(float_array
);
189 jfloat
* floats
= env
->GetFloatArrayElements(float_array
, NULL
);
190 for (jsize i
= 0; i
< len
; ++i
) {
191 out
->push_back(static_cast<float>(floats
[i
]));
193 env
->ReleaseFloatArrayElements(float_array
, floats
, JNI_ABORT
);
196 void JavaArrayOfByteArrayToStringVector(
199 std::vector
<std::string
>* out
) {
202 jsize len
= env
->GetArrayLength(array
);
204 for (jsize i
= 0; i
< len
; ++i
) {
205 ScopedJavaLocalRef
<jbyteArray
> bytes_array(
206 env
, static_cast<jbyteArray
>(
207 env
->GetObjectArrayElement(array
, i
)));
208 jsize bytes_len
= env
->GetArrayLength(bytes_array
.obj());
209 jbyte
* bytes
= env
->GetByteArrayElements(bytes_array
.obj(), NULL
);
210 (*out
)[i
].assign(reinterpret_cast<const char*>(bytes
), bytes_len
);
211 env
->ReleaseByteArrayElements(bytes_array
.obj(), bytes
, JNI_ABORT
);
215 } // namespace android