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 jbyte
* elements
= env
->GetByteArrayElements(byte_array
, NULL
);
21 memcpy(elements
, bytes
, len
);
22 env
->ReleaseByteArrayElements(byte_array
, elements
, 0);
25 return ScopedJavaLocalRef
<jbyteArray
>(env
, byte_array
);
28 ScopedJavaLocalRef
<jlongArray
> ToJavaLongArray(
29 JNIEnv
* env
, const int64
* longs
, size_t len
) {
30 jlongArray long_array
= env
->NewLongArray(len
);
34 jlong
* elements
= env
->GetLongArrayElements(long_array
, NULL
);
35 memcpy(elements
, longs
, len
* sizeof(*longs
));
36 env
->ReleaseLongArrayElements(long_array
, elements
, 0);
39 return ScopedJavaLocalRef
<jlongArray
>(env
, long_array
);
42 // Returns a new Java long array converted from the given int64 array.
43 BASE_EXPORT ScopedJavaLocalRef
<jlongArray
> ToJavaLongArray(
44 JNIEnv
* env
, const std::vector
<int64
>& longs
) {
45 return ToJavaLongArray(env
, longs
.begin(), longs
.size());
48 ScopedJavaLocalRef
<jobjectArray
> ToJavaArrayOfByteArray(
49 JNIEnv
* env
, const std::vector
<std::string
>& v
) {
50 ScopedJavaLocalRef
<jclass
> byte_array_clazz
= GetClass(env
, "[B");
51 jobjectArray joa
= env
->NewObjectArray(v
.size(),
52 byte_array_clazz
.obj(), NULL
);
55 for (size_t i
= 0; i
< v
.size(); ++i
) {
56 ScopedJavaLocalRef
<jbyteArray
> byte_array
= ToJavaByteArray(env
,
57 reinterpret_cast<const uint8
*>(v
[i
].data()), v
[i
].length());
58 env
->SetObjectArrayElement(joa
, i
, byte_array
.obj());
60 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
63 ScopedJavaLocalRef
<jobjectArray
> ToJavaArrayOfStrings(
64 JNIEnv
* env
, const std::vector
<std::string
>& v
) {
65 ScopedJavaLocalRef
<jclass
> string_clazz
= GetClass(env
, "java/lang/String");
66 jobjectArray joa
= env
->NewObjectArray(v
.size(), string_clazz
.obj(), NULL
);
69 for (size_t i
= 0; i
< v
.size(); ++i
) {
70 ScopedJavaLocalRef
<jstring
> item
= ConvertUTF8ToJavaString(env
, v
[i
]);
71 env
->SetObjectArrayElement(joa
, i
, item
.obj());
73 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
76 ScopedJavaLocalRef
<jobjectArray
> ToJavaArrayOfStrings(
77 JNIEnv
* env
, const std::vector
<string16
>& v
) {
78 ScopedJavaLocalRef
<jclass
> string_clazz
= GetClass(env
, "java/lang/String");
79 jobjectArray joa
= env
->NewObjectArray(v
.size(), string_clazz
.obj(), NULL
);
82 for (size_t i
= 0; i
< v
.size(); ++i
) {
83 ScopedJavaLocalRef
<jstring
> item
= ConvertUTF16ToJavaString(env
, v
[i
]);
84 env
->SetObjectArrayElement(joa
, i
, item
.obj());
86 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
89 void AppendJavaStringArrayToStringVector(JNIEnv
* env
,
91 std::vector
<string16
>* out
) {
95 jsize len
= env
->GetArrayLength(array
);
96 size_t back
= out
->size();
97 out
->resize(back
+ len
);
98 for (jsize i
= 0; i
< len
; ++i
) {
99 ScopedJavaLocalRef
<jstring
> str(env
,
100 static_cast<jstring
>(env
->GetObjectArrayElement(array
, i
)));
101 ConvertJavaStringToUTF16(env
, str
.obj(), &((*out
)[back
+ i
]));
105 void AppendJavaStringArrayToStringVector(JNIEnv
* env
,
107 std::vector
<std::string
>* 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 ConvertJavaStringToUTF8(env
, str
.obj(), &((*out
)[back
+ i
]));
121 void AppendJavaByteArrayToByteVector(JNIEnv
* env
,
122 jbyteArray byte_array
,
123 std::vector
<uint8
>* out
) {
127 jsize len
= env
->GetArrayLength(byte_array
);
128 jbyte
* bytes
= env
->GetByteArrayElements(byte_array
, NULL
);
129 out
->insert(out
->end(), bytes
, bytes
+ len
);
130 env
->ReleaseByteArrayElements(byte_array
, bytes
, JNI_ABORT
);
133 void JavaByteArrayToByteVector(JNIEnv
* env
,
134 jbyteArray byte_array
,
135 std::vector
<uint8
>* out
) {
138 AppendJavaByteArrayToByteVector(env
, byte_array
, out
);
141 void JavaIntArrayToIntVector(JNIEnv
* env
,
143 std::vector
<int>* out
) {
146 jsize len
= env
->GetArrayLength(array
);
147 jint
* ints
= env
->GetIntArrayElements(array
, NULL
);
148 for (jsize i
= 0; i
< len
; ++i
) {
149 out
->push_back(static_cast<int>(ints
[i
]));
151 env
->ReleaseIntArrayElements(array
, ints
, JNI_ABORT
);
154 void JavaArrayOfByteArrayToStringVector(
157 std::vector
<std::string
>* out
) {
160 jsize len
= env
->GetArrayLength(array
);
162 for (jsize i
= 0; i
< len
; ++i
) {
163 jbyteArray bytes_array
= static_cast<jbyteArray
>(
164 env
->GetObjectArrayElement(array
, i
));
165 jsize bytes_len
= env
->GetArrayLength(bytes_array
);
166 jbyte
* bytes
= env
->GetByteArrayElements(bytes_array
, NULL
);
167 (*out
)[i
].assign(reinterpret_cast<const char*>(bytes
), bytes_len
);
168 env
->ReleaseByteArrayElements(bytes_array
, bytes
, JNI_ABORT
);
172 } // namespace android