Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "base/android/jni_string.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 | |
| 11 namespace base { | |
| 12 namespace android { | |
| 13 | |
| 14 std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) { | |
| 15 // JNI's GetStringUTFChars() returns strings in Java-modified UTF8, so we | |
|
M-A Ruel
2011/11/11 13:09:07
This seems relatively inefficient in a potentially
| |
| 16 // instead get the String in UTF16 and convert using our own utility function. | |
| 17 return UTF16ToUTF8(ConvertJavaStringToUTF16(env, str)); | |
| 18 } | |
| 19 | |
| 20 jstring ConvertUTF8ToJavaString(JNIEnv* env, const std::string& str) { | |
| 21 jstring result = env->NewStringUTF(str.c_str()); | |
| 22 CheckException(env); | |
| 23 return result; | |
| 24 } | |
| 25 | |
| 26 string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) { | |
| 27 const jchar* chars = env->GetStringChars(str, NULL); | |
| 28 DCHECK(chars); | |
| 29 // GetStringChars isn't required to NULL-terminate the strings | |
| 30 // it returns, so the length must be explicitly checked. | |
| 31 string16 result(chars, env->GetStringLength(str)); | |
| 32 env->ReleaseStringChars(str, chars); | |
| 33 CheckException(env); | |
| 34 return result; | |
| 35 } | |
| 36 | |
| 37 jstring ConvertUTF16ToJavaString(JNIEnv* env, const string16& str) { | |
| 38 jstring result = env->NewString(str.data(), str.length()); | |
| 39 CheckException(env); | |
| 40 return result; | |
| 41 } | |
| 42 | |
| 43 } // namespace android | |
| 44 } // namespace base | |
| OLD | NEW |