Speculative fix for the white/stale tab issue on Windows.
[chromium-blink-merge.git] / base / android / jni_array.cc
blobfe2aadb745c087386bf01512108a2c6235c1d3d8
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"
11 namespace base {
12 namespace android {
14 ScopedJavaLocalRef<jbyteArray> ToJavaByteArray(
15 JNIEnv* env, const uint8* bytes, size_t len) {
16 jbyteArray byte_array = env->NewByteArray(len);
17 CheckException(env);
18 DCHECK(byte_array);
20 jbyte* elements = env->GetByteArrayElements(byte_array, NULL);
21 memcpy(elements, bytes, len);
22 env->ReleaseByteArrayElements(byte_array, elements, 0);
23 CheckException(env);
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);
33 CheckException(env);
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);
47 CheckException(env);
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);
60 CheckException(env);
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,
70 jobjectArray array,
71 std::vector<string16>* out) {
72 DCHECK(out);
73 if (!array)
74 return;
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,
86 jobjectArray array,
87 std::vector<std::string>* out) {
88 DCHECK(out);
89 if (!array)
90 return;
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) {
104 DCHECK(out);
105 if (!byte_array)
106 return;
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) {
116 DCHECK(out);
117 out->clear();
118 AppendJavaByteArrayToByteVector(env, byte_array, out);
121 void JavaIntArrayToIntVector(JNIEnv* env,
122 jintArray array,
123 std::vector<int>* out) {
124 DCHECK(out);
125 out->clear();
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(
135 JNIEnv* env,
136 jobjectArray array,
137 std::vector<std::string>* out) {
138 DCHECK(out);
139 out->clear();
140 jsize len = env->GetArrayLength(array);
141 out->resize(len);
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
153 } // namespace base