|
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: JNI Layer for FileStreamHandler |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <errno.h> |
|
19 |
|
20 #include "logger.h" |
|
21 #include "jniarrayutils.h" |
|
22 #include "javajniutils.h" |
|
23 #include "javacommonutils.h" |
|
24 |
|
25 #include "fileextendedcommon.h" |
|
26 #include "nativefileiohandler.h" |
|
27 #include "com_nokia_mj_impl_fileutils_FileStreamHandler.h" |
|
28 |
|
29 using namespace std; |
|
30 using namespace java::util; |
|
31 using namespace java::fileutils; |
|
32 using namespace java::fileutility; |
|
33 |
|
34 /* |
|
35 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
36 * Method: _available |
|
37 * Signature: (I)J |
|
38 */ |
|
39 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1available |
|
40 (JNIEnv *, jobject, jint aHandle) |
|
41 { |
|
42 try |
|
43 { |
|
44 NativeFileIOHandler* handler = |
|
45 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
46 return handler->available(); |
|
47 } |
|
48 catch (...) |
|
49 { |
|
50 return 0; |
|
51 } |
|
52 } |
|
53 |
|
54 /* |
|
55 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
56 * Method: _skip |
|
57 * Signature: (IJ)J |
|
58 */ |
|
59 JNIEXPORT jlong JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1skip |
|
60 (JNIEnv *, jobject, jint aHandle, jlong aOffset) |
|
61 { |
|
62 try |
|
63 { |
|
64 NativeFileIOHandler* handler = |
|
65 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
66 return handler->skip(aOffset); |
|
67 } |
|
68 catch (...) |
|
69 { |
|
70 return 0; |
|
71 } |
|
72 } |
|
73 |
|
74 /* |
|
75 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
76 * Method: _stopReading |
|
77 * Signature: (I)V |
|
78 */ |
|
79 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1stopReading |
|
80 (JNIEnv *, jobject, jint aHandle) |
|
81 { |
|
82 NativeFileIOHandler* handler = |
|
83 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
84 handler->stopReading(); |
|
85 } |
|
86 |
|
87 /* |
|
88 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
89 * Method: _stopWriting |
|
90 * Signature: (I)V |
|
91 */ |
|
92 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1stopWriting |
|
93 (JNIEnv *, jobject, jint aHandle) |
|
94 { |
|
95 NativeFileIOHandler* handler = |
|
96 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
97 handler->stopWriting(); |
|
98 } |
|
99 |
|
100 /* |
|
101 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
102 * Method: _createNativePeer |
|
103 * Signature: (Ljava/lang/String;)I |
|
104 */ |
|
105 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1createNativePeer |
|
106 (JNIEnv *aJni, jobject, jstring aName) |
|
107 { |
|
108 std::wstring name = FileUtil::jstringToWstring(aJni, aName); |
|
109 NativeFileIOHandler* handler = new NativeFileIOHandler(name); |
|
110 return reinterpret_cast<jint>(handler); |
|
111 } |
|
112 |
|
113 /* |
|
114 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
115 * Method: _readdata |
|
116 * Signature: ([BII)I |
|
117 */ |
|
118 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1readData |
|
119 (JNIEnv *aJni, jobject, jint aHandle, jbyteArray aJavaBuffer, jint aOffset, jint aLength) |
|
120 { |
|
121 char* buffer = new char[aLength + 1]; |
|
122 try |
|
123 { |
|
124 NativeFileIOHandler* handler = |
|
125 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
126 int bytesRead = handler->readBytes(buffer, aLength); |
|
127 |
|
128 if (bytesRead > 0) |
|
129 { |
|
130 JNIArrayUtils::CopyToJava(*aJni, buffer, bytesRead, aJavaBuffer, |
|
131 aOffset, bytesRead); |
|
132 } |
|
133 delete[] buffer; |
|
134 return bytesRead; |
|
135 } |
|
136 catch (int error) |
|
137 { |
|
138 WLOG1(EJavaFile, "FileStreamHandler: JNI: readData error : Error: %d", |
|
139 error); |
|
140 delete[] buffer; |
|
141 JniUtils::throwNewException(aJni, "java/io/IOException", "Read failed."); |
|
142 return 0; |
|
143 } |
|
144 catch (...) |
|
145 { |
|
146 ELOG(EJavaFile, "FileStreamHandler: JNI: readData: Unknown error."); |
|
147 delete[] buffer; |
|
148 JniUtils::throwNewException(aJni, "java/io/IOException", "Read failed."); |
|
149 return 0; |
|
150 } |
|
151 } |
|
152 |
|
153 /* |
|
154 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
155 * Method: _writeData |
|
156 * Signature: (I[BII)V |
|
157 */ |
|
158 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1writeData |
|
159 (JNIEnv *aJni, jobject, jint aHandle, jbyteArray aJavaBuffer, jint aOffset, jint aLength) |
|
160 { |
|
161 char* writeBuffer = new char[aLength + 1]; |
|
162 try |
|
163 { |
|
164 NativeFileIOHandler* handler = |
|
165 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
166 JNIArrayUtils::CopyToNative(*aJni, aJavaBuffer, aOffset, aLength, |
|
167 writeBuffer); |
|
168 handler->writeBytes(writeBuffer, aLength); |
|
169 delete[] writeBuffer; |
|
170 } |
|
171 catch (int error) |
|
172 { |
|
173 WLOG1(EJavaFile, "FileStreamHandler: JNI: writeData error : Error: %d", |
|
174 error); |
|
175 delete[] writeBuffer; |
|
176 JniUtils::throwNewException(aJni, "java/io/IOException", |
|
177 "Write failed."); |
|
178 } |
|
179 catch (...) |
|
180 { |
|
181 ELOG(EJavaFile, "FileStreamHandler: JNI: writeData: Unknown error."); |
|
182 delete[] writeBuffer; |
|
183 JniUtils::throwNewException(aJni, "java/io/IOException", |
|
184 "Write failed."); |
|
185 } |
|
186 } |
|
187 |
|
188 /* |
|
189 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
190 * Method: _closeFileStream |
|
191 * Signature: (I)V |
|
192 */ |
|
193 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1closeFileStream |
|
194 (JNIEnv *, jobject, jint aHandle) |
|
195 { |
|
196 try |
|
197 { |
|
198 NativeFileIOHandler* handler = |
|
199 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
200 handler->closeFileToReopen(); |
|
201 } |
|
202 catch (...) |
|
203 { |
|
204 ELOG(EJavaFile, "FileUtility: JNI: closeFileStream threw exception."); |
|
205 //Nothing to do |
|
206 } |
|
207 } |
|
208 |
|
209 /* |
|
210 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
211 * Method: _openFileForReading |
|
212 * Signature: (I)V |
|
213 */ |
|
214 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1openFileForReading |
|
215 (JNIEnv *aJni, jobject, jint aHandle) |
|
216 { |
|
217 try |
|
218 { |
|
219 NativeFileIOHandler* handler = |
|
220 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
221 handler->openForReading(); |
|
222 } |
|
223 catch (int error) |
|
224 { |
|
225 WLOG1(EJavaFile, "FileStreamHandler: JNI: openForReading Error: %d", |
|
226 error); |
|
227 if ((EACCES == errno) || (EPERM == errno)) |
|
228 { |
|
229 JniUtils::throwNewException(aJni, "java/lang/SecurityException", |
|
230 "Permission denied"); |
|
231 return; |
|
232 } |
|
233 else |
|
234 { |
|
235 JniUtils::throwNewException(aJni, "java/io/IOException", |
|
236 "Unable to open file for reading"); |
|
237 } |
|
238 } |
|
239 catch (...) |
|
240 { |
|
241 ELOG(EJavaFile, |
|
242 "FileStreamHandler: JNI: openForReading: Unknown error caught"); |
|
243 JniUtils::throwNewException(aJni, "java/io/IOException", |
|
244 "Unable to open file for reading"); |
|
245 } |
|
246 } |
|
247 |
|
248 /* |
|
249 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
250 * Method: _openFileForWriting |
|
251 * Signature: (IJ)V |
|
252 */ |
|
253 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1openFileForWriting |
|
254 (JNIEnv *aJni, jobject, jint aHandle, jlong aOffset) |
|
255 { |
|
256 try |
|
257 { |
|
258 NativeFileIOHandler* handler = |
|
259 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
260 handler->openForWriting(aOffset); |
|
261 } |
|
262 catch (int error) |
|
263 { |
|
264 WLOG1(EJavaFile, "FileStreamHandler: JNI: openForWriting Error: %d", |
|
265 error); |
|
266 if ((EACCES == errno) || (EPERM == errno)) |
|
267 { |
|
268 JniUtils::throwNewException(aJni, "java/lang/SecurityException", |
|
269 "Permission denied"); |
|
270 return; |
|
271 } |
|
272 else |
|
273 { |
|
274 JniUtils::throwNewException(aJni, "java/io/IOException", |
|
275 "Unable to open file for writing"); |
|
276 } |
|
277 } |
|
278 catch (...) |
|
279 { |
|
280 ELOG(EJavaFile, |
|
281 "FileStreamHandler: JNI: openForWriting: Unknown error caught"); |
|
282 JniUtils::throwNewException(aJni, "java/io/IOException", |
|
283 "Unable to open file for Writing"); |
|
284 } |
|
285 } |
|
286 |
|
287 /* |
|
288 * Class: com_nokia_mj_impl_fileutils_FileStreamHandler |
|
289 * Method: _dispose |
|
290 * Signature: (I)V |
|
291 */ |
|
292 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_fileutils_FileStreamHandler__1dispose |
|
293 (JNIEnv *, jobject, jint aHandle) |
|
294 { |
|
295 try |
|
296 { |
|
297 NativeFileIOHandler* handler = |
|
298 reinterpret_cast<NativeFileIOHandler*>(aHandle); |
|
299 delete handler; |
|
300 } |
|
301 catch (...) |
|
302 { |
|
303 ELOG(EJavaFile, |
|
304 "FileUtility: JNI: dispose. Error deleting NativeFileUtils"); |
|
305 //Nothing to do |
|
306 } |
|
307 } |