1 /* 2 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. 3 */ 4 #include "config.h" 5 6 #include <wtf/Vector.h> 7 #include <wtf/text/WTFString.h> 8 9 namespace WTF { 10 11 // String conversions 12 String::String(JNIEnv* env, const JLString &s) 13 { 14 if (!s) { 15 m_impl = StringImpl::empty(); 16 } else { 17 unsigned int len = env->GetStringLength(s); 18 if (!len) { 19 m_impl = StringImpl::empty(); 20 } else { 21 const jchar* str = env->GetStringCritical(s, NULL); 22 if (str) { 23 m_impl = StringImpl::create((const UChar*)str, len); 24 env->ReleaseStringCritical(s, str); 25 } else { 26 m_impl = StringImpl::create(reinterpret_cast<const UChar*>(L"OME"), 3); 27 } 28 } 29 } 30 } 31 32 JLString String::toJavaString(JNIEnv *env) const 33 { 34 if (isNull()) { 35 return NULL; 36 } else { 37 const unsigned len = length(); 38 if (is8Bit()) { 39 // Convert latin1 chars to unicode. 40 Vector<jchar> jchars(len); 41 for (unsigned i = 0; i < len; i++) { 42 jchars[i] = characterAt(i); 43 } 44 return env->NewString(jchars.data(), len); 45 } else { 46 return env->NewString((jchar*)characters16(), len); 47 } 48 } 49 } 50 51 } // namespace WTF