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
<jobjectArray
> ToJavaArrayOfByteArray(
29 JNIEnv
* env
, const std::vector
<std::string
>& v
) {
30 ScopedJavaLocalRef
<jclass
> byte_array_clazz
= GetClass(env
, "[B");
31 jobjectArray joa
= env
->NewObjectArray(v
.size(),
32 byte_array_clazz
.obj(), NULL
);
35 for (size_t i
= 0; i
< v
.size(); ++i
) {
36 ScopedJavaLocalRef
<jbyteArray
> byte_array
= ToJavaByteArray(env
,
37 reinterpret_cast<const uint8
*>(v
[i
].data()), v
[i
].length());
38 env
->SetObjectArrayElement(joa
, i
, byte_array
.obj());
40 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
43 ScopedJavaLocalRef
<jobjectArray
> ToJavaArrayOfStrings(
44 JNIEnv
* env
, const std::vector
<std::string
>& v
) {
45 ScopedJavaLocalRef
<jclass
> string_clazz
= GetClass(env
, "java/lang/String");
46 jobjectArray joa
= env
->NewObjectArray(v
.size(), string_clazz
.obj(), NULL
);
49 for (size_t i
= 0; i
< v
.size(); ++i
) {
50 ScopedJavaLocalRef
<jstring
> item
= ConvertUTF8ToJavaString(env
, v
[i
]);
51 env
->SetObjectArrayElement(joa
, i
, item
.obj());
53 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
56 ScopedJavaLocalRef
<jobjectArray
> ToJavaArrayOfStrings(
57 JNIEnv
* env
, const std::vector
<string16
>& v
) {
58 ScopedJavaLocalRef
<jclass
> string_clazz
= GetClass(env
, "java/lang/String");
59 jobjectArray joa
= env
->NewObjectArray(v
.size(), string_clazz
.obj(), NULL
);
62 for (size_t i
= 0; i
< v
.size(); ++i
) {
63 ScopedJavaLocalRef
<jstring
> item
= ConvertUTF16ToJavaString(env
, v
[i
]);
64 env
->SetObjectArrayElement(joa
, i
, item
.obj());
66 return ScopedJavaLocalRef
<jobjectArray
>(env
, joa
);
69 void AppendJavaStringArrayToStringVector(JNIEnv
* env
,
71 std::vector
<string16
>* out
) {
75 jsize len
= env
->GetArrayLength(array
);
76 size_t back
= out
->size();
77 out
->resize(back
+ len
);
78 for (jsize i
= 0; i
< len
; ++i
) {
79 ScopedJavaLocalRef
<jstring
> str(env
,
80 static_cast<jstring
>(env
->GetObjectArrayElement(array
, i
)));
81 ConvertJavaStringToUTF16(env
, str
.obj(), &((*out
)[back
+ i
]));
85 void AppendJavaStringArrayToStringVector(JNIEnv
* env
,
87 std::vector
<std::string
>* out
) {
91 jsize len
= env
->GetArrayLength(array
);
92 size_t back
= out
->size();
93 out
->resize(back
+ len
);
94 for (jsize i
= 0; i
< len
; ++i
) {
95 ScopedJavaLocalRef
<jstring
> str(env
,
96 static_cast<jstring
>(env
->GetObjectArrayElement(array
, i
)));
97 ConvertJavaStringToUTF8(env
, str
.obj(), &((*out
)[back
+ i
]));
101 void AppendJavaByteArrayToByteVector(JNIEnv
* env
,
102 jbyteArray byte_array
,
103 std::vector
<uint8
>* out
) {
107 jsize len
= env
->GetArrayLength(byte_array
);
108 jbyte
* bytes
= env
->GetByteArrayElements(byte_array
, NULL
);
109 out
->insert(out
->end(), bytes
, bytes
+ len
);
110 env
->ReleaseByteArrayElements(byte_array
, bytes
, JNI_ABORT
);
113 void JavaByteArrayToByteVector(JNIEnv
* env
,
114 jbyteArray byte_array
,
115 std::vector
<uint8
>* out
) {
118 AppendJavaByteArrayToByteVector(env
, byte_array
, out
);
121 void JavaIntArrayToIntVector(JNIEnv
* env
,
123 std::vector
<int>* out
) {
126 jsize len
= env
->GetArrayLength(array
);
127 jint
* ints
= env
->GetIntArrayElements(array
, NULL
);
128 for (jsize i
= 0; i
< len
; ++i
) {
129 out
->push_back(static_cast<int>(ints
[i
]));
131 env
->ReleaseIntArrayElements(array
, ints
, JNI_ABORT
);
134 void JavaArrayOfByteArrayToStringVector(
137 std::vector
<std::string
>* out
) {
140 jsize len
= env
->GetArrayLength(array
);
142 for (jsize i
= 0; i
< len
; ++i
) {
143 jbyteArray bytes_array
= static_cast<jbyteArray
>(
144 env
->GetObjectArrayElement(array
, i
));
145 jsize bytes_len
= env
->GetArrayLength(bytes_array
);
146 jbyte
* bytes
= env
->GetByteArrayElements(bytes_array
, NULL
);
147 (*out
)[i
].assign(reinterpret_cast<const char*>(bytes
), bytes_len
);
148 env
->ReleaseByteArrayElements(bytes_array
, bytes
, JNI_ABORT
);
152 } // namespace android