1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/android/jni_array.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/logging.h"
namespace base {
namespace android {
ScopedJavaLocalRef<jbyteArray> ToJavaByteArray(
JNIEnv* env, const uint8* bytes, size_t len) {
jbyteArray byte_array = env->NewByteArray(len);
CheckException(env);
DCHECK(byte_array);
env->SetByteArrayRegion(
byte_array, 0, len, reinterpret_cast<const jbyte*>(bytes));
CheckException(env);
return ScopedJavaLocalRef<jbyteArray>(env, byte_array);
}
ScopedJavaLocalRef<jintArray> ToJavaIntArray(
JNIEnv* env, const int* ints, size_t len) {
jintArray int_array = env->NewIntArray(len);
CheckException(env);
DCHECK(int_array);
env->SetIntArrayRegion(
int_array, 0, len, reinterpret_cast<const jint*>(ints));
CheckException(env);
return ScopedJavaLocalRef<jintArray>(env, int_array);
}
ScopedJavaLocalRef<jintArray> ToJavaIntArray(
JNIEnv* env, const std::vector<int>& ints) {
return ToJavaIntArray(env, ints.begin(), ints.size());
}
ScopedJavaLocalRef<jlongArray> ToJavaLongArray(
JNIEnv* env, const int64* longs, size_t len) {
jlongArray long_array = env->NewLongArray(len);
CheckException(env);
DCHECK(long_array);
env->SetLongArrayRegion(
long_array, 0, len, reinterpret_cast<const jlong*>(longs));
CheckException(env);
return ScopedJavaLocalRef<jlongArray>(env, long_array);
}
// Returns a new Java long array converted from the given int64 array.
BASE_EXPORT ScopedJavaLocalRef<jlongArray> ToJavaLongArray(
JNIEnv* env, const std::vector<int64>& longs) {
return ToJavaLongArray(env, longs.begin(), longs.size());
}
ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfByteArray(
JNIEnv* env, const std::vector<std::string>& v) {
ScopedJavaLocalRef<jclass> byte_array_clazz = GetClass(env, "[B");
jobjectArray joa = env->NewObjectArray(v.size(),
byte_array_clazz.obj(), NULL);
CheckException(env);
for (size_t i = 0; i < v.size(); ++i) {
ScopedJavaLocalRef<jbyteArray> byte_array = ToJavaByteArray(env,
reinterpret_cast<const uint8*>(v[i].data()), v[i].length());
env->SetObjectArrayElement(joa, i, byte_array.obj());
}
return ScopedJavaLocalRef<jobjectArray>(env, joa);
}
ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings(
JNIEnv* env, const std::vector<std::string>& v) {
ScopedJavaLocalRef<jclass> string_clazz = GetClass(env, "java/lang/String");
jobjectArray joa = env->NewObjectArray(v.size(), string_clazz.obj(), NULL);
CheckException(env);
for (size_t i = 0; i < v.size(); ++i) {
ScopedJavaLocalRef<jstring> item = ConvertUTF8ToJavaString(env, v[i]);
env->SetObjectArrayElement(joa, i, item.obj());
}
return ScopedJavaLocalRef<jobjectArray>(env, joa);
}
ScopedJavaLocalRef<jobjectArray> ToJavaArrayOfStrings(
JNIEnv* env, const std::vector<string16>& v) {
ScopedJavaLocalRef<jclass> string_clazz = GetClass(env, "java/lang/String");
jobjectArray joa = env->NewObjectArray(v.size(), string_clazz.obj(), NULL);
CheckException(env);
for (size_t i = 0; i < v.size(); ++i) {
ScopedJavaLocalRef<jstring> item = ConvertUTF16ToJavaString(env, v[i]);
env->SetObjectArrayElement(joa, i, item.obj());
}
return ScopedJavaLocalRef<jobjectArray>(env, joa);
}
void AppendJavaStringArrayToStringVector(JNIEnv* env,
jobjectArray array,
std::vector<string16>* out) {
DCHECK(out);
if (!array)
return;
jsize len = env->GetArrayLength(array);
size_t back = out->size();
out->resize(back + len);
for (jsize i = 0; i < len; ++i) {
ScopedJavaLocalRef<jstring> str(env,
static_cast<jstring>(env->GetObjectArrayElement(array, i)));
ConvertJavaStringToUTF16(env, str.obj(), &((*out)[back + i]));
}
}
void AppendJavaStringArrayToStringVector(JNIEnv* env,
jobjectArray array,
std::vector<std::string>* out) {
DCHECK(out);
if (!array)
return;
jsize len = env->GetArrayLength(array);
size_t back = out->size();
out->resize(back + len);
for (jsize i = 0; i < len; ++i) {
ScopedJavaLocalRef<jstring> str(env,
static_cast<jstring>(env->GetObjectArrayElement(array, i)));
ConvertJavaStringToUTF8(env, str.obj(), &((*out)[back + i]));
}
}
void AppendJavaByteArrayToByteVector(JNIEnv* env,
jbyteArray byte_array,
std::vector<uint8>* out) {
DCHECK(out);
if (!byte_array)
return;
jsize len = env->GetArrayLength(byte_array);
jbyte* bytes = env->GetByteArrayElements(byte_array, NULL);
out->insert(out->end(), bytes, bytes + len);
env->ReleaseByteArrayElements(byte_array, bytes, JNI_ABORT);
}
void JavaByteArrayToByteVector(JNIEnv* env,
jbyteArray byte_array,
std::vector<uint8>* out) {
DCHECK(out);
out->clear();
AppendJavaByteArrayToByteVector(env, byte_array, out);
}
void JavaIntArrayToIntVector(JNIEnv* env,
jintArray int_array,
std::vector<int>* out) {
DCHECK(out);
out->clear();
jsize len = env->GetArrayLength(int_array);
jint* ints = env->GetIntArrayElements(int_array, NULL);
for (jsize i = 0; i < len; ++i) {
out->push_back(static_cast<int>(ints[i]));
}
env->ReleaseIntArrayElements(int_array, ints, JNI_ABORT);
}
void JavaLongArrayToLongVector(JNIEnv* env,
jlongArray long_array,
std::vector<long>* out) {
DCHECK(out);
out->clear();
jsize len = env->GetArrayLength(long_array);
jlong* longs = env->GetLongArrayElements(long_array, NULL);
for (jsize i = 0; i < len; ++i) {
out->push_back(static_cast<long>(longs[i]));
}
env->ReleaseLongArrayElements(long_array, longs, JNI_ABORT);
}
void JavaFloatArrayToFloatVector(JNIEnv* env,
jfloatArray float_array,
std::vector<float>* out) {
DCHECK(out);
out->clear();
jsize len = env->GetArrayLength(float_array);
jfloat* floats = env->GetFloatArrayElements(float_array, NULL);
for (jsize i = 0; i < len; ++i) {
out->push_back(static_cast<float>(floats[i]));
}
env->ReleaseFloatArrayElements(float_array, floats, JNI_ABORT);
}
void JavaArrayOfByteArrayToStringVector(
JNIEnv* env,
jobjectArray array,
std::vector<std::string>* out) {
DCHECK(out);
out->clear();
jsize len = env->GetArrayLength(array);
out->resize(len);
for (jsize i = 0; i < len; ++i) {
ScopedJavaLocalRef<jbyteArray> bytes_array(
env, static_cast<jbyteArray>(
env->GetObjectArrayElement(array, i)));
jsize bytes_len = env->GetArrayLength(bytes_array.obj());
jbyte* bytes = env->GetByteArrayElements(bytes_array.obj(), NULL);
(*out)[i].assign(reinterpret_cast<const char*>(bytes), bytes_len);
env->ReleaseByteArrayElements(bytes_array.obj(), bytes, JNI_ABORT);
}
}
} // namespace android
} // namespace base
|