1 #include "mmapiutils.h" |
|
2 #include "s60commonutils.h" |
|
3 using namespace java::util; |
|
4 |
|
5 enum TJavaArrayPanic |
|
6 { |
|
7 EBadOffsetIntoJavaArray, |
|
8 EWritingOverEndOfJavaArray, |
|
9 EBadOffsetIntoJavaArrayForRead, |
|
10 EReadingOverEndOfJavaArray, |
|
11 }; |
|
12 |
|
13 |
|
14 /** |
|
15 * Copies data from the native to the Java array. |
|
16 * @return The number of bytes copied. |
|
17 */ |
|
18 TInt MMAPIUtils::CopyToJava(JNIEnv& aJni, const TDesC8& aNativeBuffer, |
|
19 jbyteArray aJavaBuffer, TInt aOffset, TInt aLength) |
|
20 { |
|
21 __ASSERT_DEBUG(aOffset <= aJni.GetArrayLength(aJavaBuffer), |
|
22 User::Panic(_L("ArrayUtils"), EBadOffsetIntoJavaArray)); |
|
23 __ASSERT_DEBUG(aLength <= aJni.GetArrayLength(aJavaBuffer) - aOffset, |
|
24 User::Panic(_L("ArrayUtils"), EWritingOverEndOfJavaArray)); |
|
25 |
|
26 TInt nativeBufferLength = aNativeBuffer.Length(); |
|
27 TInt length = (nativeBufferLength < aLength) ? nativeBufferLength : aLength; |
|
28 TUint8* nativeBufferPtr = const_cast<TUint8*>(aNativeBuffer.Ptr()); |
|
29 jbyte* jNativeBufferPtr = reinterpret_cast<jbyte*>(nativeBufferPtr); |
|
30 aJni.SetByteArrayRegion(aJavaBuffer, aOffset, length, jNativeBufferPtr); |
|
31 return length; |
|
32 } |
|
33 |
|
34 jobjectArray MMAPIUtils::CopyToNewJavaStringArrayL(JNIEnv& aJni, const CDesCArray& aNativeArray) |
|
35 { |
|
36 jclass stringClass = aJni.FindClass("java/lang/String"); |
|
37 User::LeaveIfNull(stringClass); |
|
38 // |
|
39 jobjectArray result = aJni.NewObjectArray(aNativeArray.Count(), stringClass, NULL); |
|
40 if (result != NULL) |
|
41 { |
|
42 TPtrC item; |
|
43 for (int i=0; i<aNativeArray.Count(); ++i) |
|
44 { |
|
45 item.Set(aNativeArray[i]); |
|
46 AddToJavaStringArrayL(aJni, result, i, item); |
|
47 } |
|
48 } |
|
49 return result; |
|
50 } |
|
51 |
|
52 void MMAPIUtils::AddToJavaStringArrayL(JNIEnv& aJni, jobjectArray& aContainer, TInt aPosition, const TDesC& aString) |
|
53 { |
|
54 jstring javaString = S60CommonUtils::NativeToJavaString(aJni, aString); |
|
55 User::LeaveIfNull(javaString); |
|
56 // |
|
57 aJni.SetObjectArrayElement(aContainer, aPosition, javaString); |
|
58 aJni.DeleteLocalRef(javaString); |
|
59 } |
|