javacommons/utils/src/javajniutils.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "logger.h"
       
    20 #include "javacommonutils.h"
       
    21 #include "javajniutils.h"
       
    22 
       
    23 using namespace java::util;
       
    24 
       
    25 
       
    26 OS_EXPORT
       
    27 std::wstring JniUtils::jstringToWstring(JNIEnv* env, const jstring& jStr)
       
    28 {
       
    29     jboolean iscopy;
       
    30 #ifdef __SYMBIAN32__
       
    31     // In Symbian the size of jchar == the size of wchar_t
       
    32     const jchar* jChr = env->GetStringChars(jStr, &iscopy);
       
    33     std::wstring wStr((wchar_t *)jChr, env->GetStringLength(jStr));
       
    34     env->ReleaseStringChars(jStr, jChr);
       
    35 #else
       
    36     // The size of jchar != the size of wchar_t
       
    37     const char* utf8(env->GetStringUTFChars(jStr, &iscopy));
       
    38     std::wstring wStr = JavaCommonUtils::utf8ToWstring(utf8);
       
    39     env->ReleaseStringUTFChars(jStr, utf8);
       
    40 #endif
       
    41 
       
    42     return wStr;
       
    43 }
       
    44 
       
    45 OS_EXPORT
       
    46 jstring JniUtils::wstringToJstring(JNIEnv* env, const std::wstring& wStr)
       
    47 {
       
    48 #ifdef __SYMBIAN32__
       
    49     // In Symbian the size of jchar == the size of wchar_t
       
    50     jstring jStr = env->NewString(
       
    51                        reinterpret_cast<const jchar*>(wStr.c_str()), wStr.length());
       
    52 #else
       
    53     // The size of jchar != the size of wchar_t
       
    54     const char* utf8 = JavaCommonUtils::wstringToUtf8(wStr);
       
    55     jstring jStr = env->NewStringUTF(utf8);
       
    56     delete[] utf8;
       
    57 #endif
       
    58 
       
    59     return jStr;
       
    60 }
       
    61 
       
    62 OS_EXPORT
       
    63 void JniUtils::throwNewException(JNIEnv* aEnv,const std::string& aClassNameOfException,
       
    64                                  const std::string& aExceptionText)
       
    65 {
       
    66     jclass cls = aEnv->FindClass(aClassNameOfException.c_str());
       
    67     // if cls is NULL, an exception has already been thrown
       
    68     if (cls != 0)
       
    69     {
       
    70         aEnv->ThrowNew(cls, aExceptionText.c_str());
       
    71     }
       
    72     // free the local ref
       
    73     aEnv->DeleteLocalRef(cls);
       
    74 }
       
    75