|
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: ?Description |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32def.h> |
|
20 #include <e32cmn.h> |
|
21 #include <e32std.h> |
|
22 |
|
23 #include "jutils.h" |
|
24 #include "jni.h" |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 enum TJavaArrayPanic |
|
30 { |
|
31 EBadOffsetIntoJavaArray, |
|
32 EWritingOverEndOfJavaArray, |
|
33 EBadOffsetIntoJavaArrayForRead, |
|
34 EReadingOverEndOfJavaArray, |
|
35 }; |
|
36 |
|
37 |
|
38 |
|
39 /** |
|
40 * Accesses the Java array and copies its data into the native descriptor. |
|
41 * @param aJni The JNI environment. |
|
42 * @param aJavaBuffer The Java buffer to copy data from. |
|
43 * @param aOffset Start of data in Java buffer to copy. This is assumed to be valid. |
|
44 * @param aLength Amount of data to copy. This is assumed to be valid. |
|
45 * @param aNativeBuffer Target for data. This is assumed to be long enough. |
|
46 * @returns An error code. |
|
47 */ |
|
48 OS_EXPORT TInt ArrayUtils::CopyToNative(JNIEnv& aJni, jbyteArray aJavaBuffer, |
|
49 TInt aOffset, TInt aLength, TDes8& aNativeBuffer) |
|
50 { |
|
51 |
|
52 aNativeBuffer.SetLength(aLength); |
|
53 TUint8* nativeBufferPtr = const_cast<TUint8*>(aNativeBuffer.Ptr()); |
|
54 jbyte* jNativeBufferPtr = reinterpret_cast<jbyte*>(nativeBufferPtr); |
|
55 aJni.GetByteArrayRegion(aJavaBuffer, aOffset, aLength, jNativeBufferPtr); |
|
56 return KErrNone; |
|
57 } |
|
58 |
|
59 |
|
60 |
|
61 /** |
|
62 * Copies data from the native to the Java array. |
|
63 * @return The number of bytes copied. |
|
64 */ |
|
65 OS_EXPORT TInt ArrayUtils::CopyToJava(JNIEnv& aJni, const TDesC8& aNativeBuffer, |
|
66 jbyteArray aJavaBuffer, TInt aOffset, TInt aLength) |
|
67 { |
|
68 |
|
69 TInt nativeBufferLength = aNativeBuffer.Length(); |
|
70 TInt length = (nativeBufferLength < aLength) ? nativeBufferLength : aLength; |
|
71 TUint8* nativeBufferPtr = const_cast<TUint8*>(aNativeBuffer.Ptr()); |
|
72 jbyte* jNativeBufferPtr = reinterpret_cast<jbyte*>(nativeBufferPtr); |
|
73 aJni.SetByteArrayRegion(aJavaBuffer, aOffset, length, jNativeBufferPtr); |
|
74 return length; |
|
75 } |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |