javacommons/utils/src/jniarrayutils.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 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 #include "jniarrayutils.h"
       
    19 #include "wchar.h"
       
    20 
       
    21 
       
    22 OS_EXPORT int JNIArrayUtils::CopyToJava(JNIEnv& aJni, const char* aNativeBuffer, int aNativeBufferLength,
       
    23                                         jbyteArray aJavaBuffer, int aOffset, int aLength)
       
    24 {
       
    25 
       
    26     int length = (aNativeBufferLength < aLength) ? aNativeBufferLength : aLength;
       
    27 
       
    28 
       
    29     char* nativeBufferPtr = const_cast<char *>(aNativeBuffer);
       
    30     jbyte* jNativeBufferPtr = reinterpret_cast<jbyte*>(nativeBufferPtr);
       
    31     aJni.SetByteArrayRegion(aJavaBuffer, aOffset, length, jNativeBufferPtr);
       
    32     return length;
       
    33 }
       
    34 
       
    35 
       
    36 OS_EXPORT int JNIArrayUtils::CopyToNative(JNIEnv& aJni, jbyteArray aJavaBuffer,
       
    37         int aOffset, int aLength, char* aNativeBuffer)
       
    38 {
       
    39     char* nativeBufferPtr = const_cast<char*>(aNativeBuffer);
       
    40     jbyte* jNativeBufferPtr = reinterpret_cast<jbyte*>(nativeBufferPtr);
       
    41     aJni.GetByteArrayRegion(aJavaBuffer, aOffset, aLength, jNativeBufferPtr);
       
    42     return aLength;
       
    43 }
       
    44 
       
    45 
       
    46 OS_EXPORT jstring JNIArrayUtils::WStringToJavaString(JNIEnv& aJni, const wchar_t* aString, const int aLength)
       
    47 {
       
    48     wchar_t* nativeCharArray = const_cast<wchar_t*>(aString);
       
    49     const jchar* stringPtr = reinterpret_cast<jchar*>(nativeCharArray);
       
    50     const jsize stringLength = aLength;
       
    51     jstring jniString = aJni.NewString(stringPtr, stringLength);
       
    52     return jniString;
       
    53 }
       
    54