|
1 /* |
|
2 * Copyright (c) 2002-2004 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 "jutils.h" |
|
20 |
|
21 |
|
22 |
|
23 enum TJavaArrayPanic |
|
24 { |
|
25 EBadOffsetIntoJavaArray, |
|
26 EWritingOverEndOfJavaArray, |
|
27 EBadOffsetIntoJavaArrayForRead, |
|
28 EReadingOverEndOfJavaArray, |
|
29 }; |
|
30 |
|
31 |
|
32 |
|
33 /** |
|
34 * Accesses the Java array and copies its data into the native descriptor. |
|
35 * @param aJni The JNI environment. |
|
36 * @param aJavaBuffer The Java buffer to copy data from. |
|
37 * @param aOffset Start of data in Java buffer to copy. This is assumed to be valid. |
|
38 * @param aLength Amount of data to copy. This is assumed to be valid. |
|
39 * @param aNativeBuffer Target for data. This is assumed to be long enough. |
|
40 * @returns An error code. |
|
41 */ |
|
42 TInt ArrayUtils::CopyToNative(JNIEnv& aJni, jbyteArray aJavaBuffer, |
|
43 TInt aOffset, TInt aLength, TDes8& aNativeBuffer) |
|
44 { |
|
45 __ASSERT_DEBUG(aOffset <= aJni.GetArrayLength(aJavaBuffer), |
|
46 User::Panic(_L("ArrayUtils"), EBadOffsetIntoJavaArrayForRead)); |
|
47 __ASSERT_DEBUG(aLength <= aJni.GetArrayLength(aJavaBuffer) - aOffset, |
|
48 User::Panic(_L("ArrayUtils"), EReadingOverEndOfJavaArray)); |
|
49 |
|
50 aNativeBuffer.SetLength(aLength); |
|
51 TUint8* nativeBufferPtr = const_cast<TUint8*>(aNativeBuffer.Ptr()); |
|
52 jbyte* jNativeBufferPtr = reinterpret_cast<jbyte*>(nativeBufferPtr); |
|
53 aJni.GetByteArrayRegion(aJavaBuffer, aOffset, aLength, jNativeBufferPtr); |
|
54 return KErrNone; |
|
55 } |
|
56 |
|
57 |
|
58 |
|
59 /** |
|
60 * Copies data from the native to the Java array. |
|
61 * @return The number of bytes copied. |
|
62 */ |
|
63 EXPORT_C TInt ArrayUtils::CopyToJava(JNIEnv& aJni, const TDesC8& aNativeBuffer, |
|
64 jbyteArray aJavaBuffer, TInt aOffset, TInt aLength) |
|
65 { |
|
66 __ASSERT_DEBUG(aOffset <= aJni.GetArrayLength(aJavaBuffer), |
|
67 User::Panic(_L("ArrayUtils"), EBadOffsetIntoJavaArray)); |
|
68 __ASSERT_DEBUG(aLength <= aJni.GetArrayLength(aJavaBuffer) - aOffset, |
|
69 User::Panic(_L("ArrayUtils"), EWritingOverEndOfJavaArray)); |
|
70 |
|
71 TInt nativeBufferLength = aNativeBuffer.Length(); |
|
72 TInt length = (nativeBufferLength < aLength) ? nativeBufferLength : aLength; |
|
73 TUint8* nativeBufferPtr = const_cast<TUint8*>(aNativeBuffer.Ptr()); |
|
74 jbyte* jNativeBufferPtr = reinterpret_cast<jbyte*>(nativeBufferPtr); |
|
75 aJni.SetByteArrayRegion(aJavaBuffer, aOffset, length, jNativeBufferPtr); |
|
76 return length; |
|
77 } |
|
78 |
|
79 |
|
80 |
|
81 /** |
|
82 * Creates a Java array of strings from a native array of descriptors allocated |
|
83 * on the heap. |
|
84 * @param aJni The JNI environment. |
|
85 * @param aNativeArray The array of descriptors. |
|
86 * @return The newly created Java array of String objects, or NULL on error. |
|
87 */ |
|
88 jobjectArray ArrayUtils::CopyToNewJavaStringArray(JNIEnv& aJni, |
|
89 const RPointerArray<HBufC>& aNativeArray) |
|
90 { |
|
91 jclass stringClass = aJni.FindClass("java/lang/String"); |
|
92 if (stringClass == NULL) |
|
93 { |
|
94 return NULL; |
|
95 } |
|
96 |
|
97 TInt count = aNativeArray.Count(); |
|
98 jobjectArray result = aJni.NewObjectArray(count, stringClass, NULL); |
|
99 if (result == NULL) |
|
100 { |
|
101 return NULL; |
|
102 } |
|
103 |
|
104 for (int ii = 0; ii< count; ii++) |
|
105 { |
|
106 jstring javaString = CreateJavaString(aJni, *aNativeArray[ii]); |
|
107 if (javaString == NULL) |
|
108 { |
|
109 aJni.DeleteLocalRef(result); |
|
110 return NULL; |
|
111 } |
|
112 |
|
113 aJni.SetObjectArrayElement(result, ii, javaString); |
|
114 aJni.DeleteLocalRef(javaString); |
|
115 } |
|
116 |
|
117 return result; |
|
118 } |