This source file includes following definitions.
- ConvertUTF16ToJavaStringImpl
- ConvertJavaStringToUTF8
- ConvertJavaStringToUTF8
- ConvertJavaStringToUTF8
- ConvertUTF8ToJavaString
- ConvertJavaStringToUTF16
- ConvertJavaStringToUTF16
- ConvertJavaStringToUTF16
- ConvertUTF16ToJavaString
#include "base/android/jni_string.h"
#include "base/android/jni_android.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
namespace {
jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env,
const base::StringPiece16& str) {
jstring result = env->NewString(str.data(), str.length());
base::android::CheckException(env);
return result;
}
}
namespace base {
namespace android {
void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) {
if (!str) {
LOG(WARNING) << "ConvertJavaStringToUTF8 called with null string.";
result->clear();
return;
}
const jchar* chars = env->GetStringChars(str, NULL);
DCHECK(chars);
UTF16ToUTF8(chars, env->GetStringLength(str), result);
env->ReleaseStringChars(str, chars);
CheckException(env);
}
std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) {
std::string result;
ConvertJavaStringToUTF8(env, str, &result);
return result;
}
std::string ConvertJavaStringToUTF8(const JavaRef<jstring>& str) {
return ConvertJavaStringToUTF8(AttachCurrentThread(), str.obj());
}
ScopedJavaLocalRef<jstring> ConvertUTF8ToJavaString(
JNIEnv* env,
const base::StringPiece& str) {
return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl(
env, UTF8ToUTF16(str)));
}
void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) {
if (!str) {
LOG(WARNING) << "ConvertJavaStringToUTF16 called with null string.";
result->clear();
return;
}
const jchar* chars = env->GetStringChars(str, NULL);
DCHECK(chars);
result->assign(chars, env->GetStringLength(str));
env->ReleaseStringChars(str, chars);
CheckException(env);
}
string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) {
string16 result;
ConvertJavaStringToUTF16(env, str, &result);
return result;
}
string16 ConvertJavaStringToUTF16(const JavaRef<jstring>& str) {
return ConvertJavaStringToUTF16(AttachCurrentThread(), str.obj());
}
ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString(
JNIEnv* env,
const base::StringPiece16& str) {
return ScopedJavaLocalRef<jstring>(env,
ConvertUTF16ToJavaStringImpl(env, str));
}
}
}