Disable webaudio support on Android ARM by turning off use_openmax_dl_fft.
[chromium-blink-merge.git] / base / android / jni_array.cc
blob596914b26c91270f71a4168f64e60c1d90b1347c
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<jlongArray> ToJavaLongArray(
29 JNIEnv* env, const int64* longs, size_t len) {
30 jlongArray long_array = env->NewLongArray(len);
31 CheckException(env);
32 DCHECK(long_array);
34 jlong* elements = env->GetLongArrayElements(long_array, NULL);
35 memcpy(elements, longs, len * sizeof(*longs));
36 env->ReleaseLongArrayElements(long_array, elements, 0);
37 CheckException(env);
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);
53 CheckException(env);
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);
67 CheckException(env);
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);
80 CheckException(env);
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,
90 jobjectArray array,
91 std::vector<string16>* out) {
92 DCHECK(out);
93 if (!array)
94 return;
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,
106 jobjectArray array,
107 std::vector<std::string>* out) {
108 DCHECK(out);
109 if (!array)
110 return;
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) {
124 DCHECK(out);
125 if (!byte_array)
126 return;
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) {
136 DCHECK(out);
137 out->clear();
138 AppendJavaByteArrayToByteVector(env, byte_array, out);
141 void JavaIntArrayToIntVector(JNIEnv* env,
142 jintArray array,
143 std::vector<int>* out) {
144 DCHECK(out);
145 out->clear();
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(
155 JNIEnv* env,
156 jobjectArray array,
157 std::vector<std::string>* out) {
158 DCHECK(out);
159 out->clear();
160 jsize len = env->GetArrayLength(array);
161 out->resize(len);
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
173 } // namespace base