46 |
|
|
47 |
#include "target_native.h" |
#include "target_native.h" |
48 |
#ifndef WITHOUT_FILESYSTEM |
#ifndef WITHOUT_FILESYSTEM |
49 |
#include "target_native_file.h" |
#include "target_native_file.h" |
50 |
#endif |
#endif |
51 |
#include "target_native_math_int.h" |
#include "target_native_math_int.h" |
52 |
|
|
57 |
*/ |
*/ |
58 |
|
|
59 |
jint |
jint |
60 |
_javaio_open_read(JNIEnv *env, jstring name) |
_javaio_open_read (JNIEnv * env, jstring name) |
61 |
{ |
{ |
62 |
#ifndef WITHOUT_FILESYSTEM |
#ifndef WITHOUT_FILESYSTEM |
63 |
const char *filename; |
const char *filename; |
64 |
int fd; |
int fd; |
65 |
int result; |
int result; |
66 |
|
|
67 |
filename = JCL_jstring_to_cstring(env, name); |
filename = JCL_jstring_to_cstring (env, name); |
68 |
if (filename == NULL) |
if (filename == NULL) |
69 |
return(-1); |
return (-1); |
70 |
|
|
71 |
TARGET_NATIVE_FILE_OPEN_READ(filename,fd,result); |
TARGET_NATIVE_FILE_OPEN_READ (filename, fd, result); |
72 |
(*env)->ReleaseStringUTFChars(env, name, filename); |
(*env)->ReleaseStringUTFChars (env, name, filename); |
73 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
74 |
{ |
{ |
75 |
if (TARGET_NATIVE_LAST_ERROR() == TARGET_NATIVE_ERROR_NO_SUCH_FILE) |
if (TARGET_NATIVE_LAST_ERROR () == TARGET_NATIVE_ERROR_NO_SUCH_FILE) |
76 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
77 |
"java/io/FileNotFoundException", |
"java/io/FileNotFoundException", |
78 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
79 |
else |
else |
80 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
81 |
"java/io/IOException", |
"java/io/IOException", |
82 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
83 |
} |
} |
84 |
|
|
85 |
JCL_free_cstring(env, name, filename); |
JCL_free_cstring (env, name, filename); |
86 |
return(fd); |
return (fd); |
87 |
#else /* not WITHOUT_FILESYSTEM */ |
#else /* not WITHOUT_FILESYSTEM */ |
88 |
return(-1); |
return (-1); |
89 |
#endif /* not WITHOUT_FILESYSTEM */ |
#endif /* not WITHOUT_FILESYSTEM */ |
90 |
} |
} |
91 |
|
|
94 |
*/ |
*/ |
95 |
|
|
96 |
jint |
jint |
97 |
_javaio_open_readwrite(JNIEnv *env, jstring name) |
_javaio_open_readwrite (JNIEnv * env, jstring name) |
98 |
{ |
{ |
99 |
#ifndef WITHOUT_FILESYSTEM |
#ifndef WITHOUT_FILESYSTEM |
100 |
const char *filename; |
const char *filename; |
101 |
int fd; |
int fd; |
102 |
int result; |
int result; |
103 |
|
|
104 |
filename = JCL_jstring_to_cstring(env, name); |
filename = JCL_jstring_to_cstring (env, name); |
105 |
if (filename == NULL) |
if (filename == NULL) |
106 |
return(-1); |
return (-1); |
107 |
|
|
108 |
TARGET_NATIVE_FILE_OPEN_READWRITE(filename,fd,result); |
TARGET_NATIVE_FILE_OPEN_READWRITE (filename, fd, result); |
109 |
(*env)->ReleaseStringUTFChars(env, name, filename); |
(*env)->ReleaseStringUTFChars (env, name, filename); |
110 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
111 |
{ |
{ |
112 |
if (TARGET_NATIVE_LAST_ERROR() == TARGET_NATIVE_ERROR_NO_SUCH_FILE) |
if (TARGET_NATIVE_LAST_ERROR () == TARGET_NATIVE_ERROR_NO_SUCH_FILE) |
113 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
114 |
"java/io/FileNotFoundException", |
"java/io/FileNotFoundException", |
115 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
116 |
else |
else |
117 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
118 |
"java/io/IOException", |
"java/io/IOException", |
119 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
120 |
} |
} |
121 |
|
|
122 |
JCL_free_cstring(env, name, filename); |
JCL_free_cstring (env, name, filename); |
123 |
return(fd); |
return (fd); |
124 |
#else /* not WITHOUT_FILESYSTEM */ |
#else /* not WITHOUT_FILESYSTEM */ |
125 |
return(-1); |
return (-1); |
126 |
#endif /* not WITHOUT_FILESYSTEM */ |
#endif /* not WITHOUT_FILESYSTEM */ |
127 |
} |
} |
128 |
|
|
133 |
*/ |
*/ |
134 |
|
|
135 |
void |
void |
136 |
_javaio_close(JNIEnv *env, jint fd) |
_javaio_close (JNIEnv * env, jint fd) |
137 |
{ |
{ |
138 |
#ifndef WITHOUT_FILESYSTEM |
#ifndef WITHOUT_FILESYSTEM |
139 |
int result; |
int result; |
140 |
|
|
141 |
if (fd != -1) |
if (fd != -1) |
142 |
{ |
{ |
143 |
TARGET_NATIVE_FILE_CLOSE(fd,result); |
TARGET_NATIVE_FILE_CLOSE (fd, result); |
144 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
145 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
146 |
"java/io/IOException", |
"java/io/IOException", |
147 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
148 |
} |
} |
149 |
#else /* not WITHOUT_FILESYSTEM */ |
#else /* not WITHOUT_FILESYSTEM */ |
150 |
#endif /* not WITHOUT_FILESYSTEM */ |
#endif /* not WITHOUT_FILESYSTEM */ |
157 |
*/ |
*/ |
158 |
|
|
159 |
jlong |
jlong |
160 |
_javaio_skip_bytes(JNIEnv *env, jint fd, jlong num_bytes) |
_javaio_skip_bytes (JNIEnv * env, jint fd, jlong num_bytes) |
161 |
{ |
{ |
162 |
#ifndef WITHOUT_FILESYSTEM |
#ifndef WITHOUT_FILESYSTEM |
163 |
jlong current_offset, new_offset; |
jlong current_offset, new_offset; |
164 |
int result; |
int result; |
165 |
|
|
166 |
TARGET_NATIVE_FILE_SEEK_CURRENT(fd,TARGET_NATIVE_MATH_INT_INT64_CONST_0,current_offset,result); |
TARGET_NATIVE_FILE_SEEK_CURRENT (fd, TARGET_NATIVE_MATH_INT_INT64_CONST_0, |
167 |
|
current_offset, result); |
168 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
169 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
170 |
"java/io/IOException", |
"java/io/IOException", |
171 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
172 |
|
|
173 |
TARGET_NATIVE_FILE_SEEK_CURRENT(fd,num_bytes,new_offset,result); |
TARGET_NATIVE_FILE_SEEK_CURRENT (fd, num_bytes, new_offset, result); |
174 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
175 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
176 |
"java/io/IOException", |
"java/io/IOException", |
177 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
178 |
|
|
179 |
return(TARGET_NATIVE_MATH_INT_INT64_SUB(new_offset,current_offset)); |
return (TARGET_NATIVE_MATH_INT_INT64_SUB (new_offset, current_offset)); |
180 |
#else /* not WITHOUT_FILESYSTEM */ |
#else /* not WITHOUT_FILESYSTEM */ |
181 |
return(TARGET_NATIVE_MATH_INT_INT64_CONST_0); |
return (TARGET_NATIVE_MATH_INT_INT64_CONST_0); |
182 |
#endif /* not WITHOUT_FILESYSTEM */ |
#endif /* not WITHOUT_FILESYSTEM */ |
183 |
} |
} |
184 |
|
|
189 |
*/ |
*/ |
190 |
|
|
191 |
jlong |
jlong |
192 |
_javaio_get_file_length(JNIEnv *env, jint fd) |
_javaio_get_file_length (JNIEnv * env, jint fd) |
193 |
{ |
{ |
194 |
#ifndef WITHOUT_FILESYSTEM |
#ifndef WITHOUT_FILESYSTEM |
195 |
jlong length; |
jlong length; |
196 |
int result; |
int result; |
197 |
|
|
198 |
TARGET_NATIVE_FILE_SIZE(fd,length,result); |
TARGET_NATIVE_FILE_SIZE (fd, length, result); |
199 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
200 |
{ |
{ |
201 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
202 |
"java/io/IOException", |
"java/io/IOException", |
203 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
204 |
return(TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1); |
return (TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1); |
205 |
} |
} |
206 |
|
|
207 |
return(length); |
return (length); |
208 |
#else /* not WITHOUT_FILESYSTEM */ |
#else /* not WITHOUT_FILESYSTEM */ |
209 |
return(TARGET_NATIVE_MATH_INT_INT64_CONST_0); |
return (TARGET_NATIVE_MATH_INT_INT64_CONST_0); |
210 |
#endif /* not WITHOUT_FILESYSTEM */ |
#endif /* not WITHOUT_FILESYSTEM */ |
211 |
} |
} |
212 |
|
|
217 |
*/ |
*/ |
218 |
|
|
219 |
jint |
jint |
220 |
_javaio_read(JNIEnv *env, jint fd, jarray buf, jint offset, jint len) |
_javaio_read (JNIEnv * env, jint fd, jarray buf, jint offset, jint len) |
221 |
{ |
{ |
222 |
#ifndef WITHOUT_FILESYSTEM |
#ifndef WITHOUT_FILESYSTEM |
223 |
jbyte *bufptr; |
jbyte *bufptr; |
224 |
int bytesRead; |
int bytesRead; |
225 |
int result; |
int result; |
226 |
|
|
227 |
assert(offset>=0); |
assert (offset >= 0); |
228 |
assert(len>=0); |
assert (len >= 0); |
229 |
|
|
230 |
if (len == 0) |
if (len == 0) |
231 |
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */ |
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */ |
232 |
|
|
233 |
bufptr = (*env)->GetByteArrayElements(env, buf, JNI_FALSE); |
bufptr = (*env)->GetByteArrayElements (env, buf, JNI_FALSE); |
234 |
if (bufptr == NULL) |
if (bufptr == NULL) |
235 |
{ |
{ |
236 |
JCL_ThrowException(env, "java/io/IOException", "Internal Error: get byte array fail"); |
JCL_ThrowException (env, "java/io/IOException", |
237 |
return(-1); |
"Internal Error: get byte array fail"); |
238 |
|
return (-1); |
239 |
} |
} |
240 |
|
|
241 |
TARGET_NATIVE_FILE_READ(fd,(bufptr + offset),len,bytesRead,result); |
TARGET_NATIVE_FILE_READ (fd, (bufptr + offset), len, bytesRead, result); |
242 |
(*env)->ReleaseByteArrayElements(env, buf, bufptr, 0); |
(*env)->ReleaseByteArrayElements (env, buf, bufptr, 0); |
243 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
244 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
245 |
"java/io/IOException", |
"java/io/IOException", |
246 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
247 |
|
|
248 |
if (bytesRead == 0) |
if (bytesRead == 0) |
249 |
return(-1); |
return (-1); |
250 |
|
|
251 |
return(bytesRead); |
return (bytesRead); |
252 |
#else /* not WITHOUT_FILESYSTEM */ |
#else /* not WITHOUT_FILESYSTEM */ |
253 |
jbyte *bufptr; |
jbyte *bufptr; |
254 |
int bytesRead; |
int bytesRead; |
255 |
|
|
256 |
assert(offset>=0); |
assert (offset >= 0); |
257 |
assert(len>=0); |
assert (len >= 0); |
258 |
|
|
259 |
if ((fd==0) || (fd==1) || (fd==2)) |
if ((fd == 0) || (fd == 1) || (fd == 2)) |
260 |
{ |
{ |
261 |
if (len == 0) |
if (len == 0) |
262 |
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */ |
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */ |
263 |
|
|
264 |
bufptr = (*env)->GetByteArrayElements(env, buf, JNI_FALSE); |
bufptr = (*env)->GetByteArrayElements (env, buf, JNI_FALSE); |
265 |
if (bufptr == NULL) |
if (bufptr == NULL) |
266 |
{ |
{ |
267 |
JCL_ThrowException(env, "java/io/IOException", "Internal Error: get byte array"); |
JCL_ThrowException (env, "java/io/IOException", |
268 |
return(-1); |
"Internal Error: get byte array"); |
269 |
} |
return (-1); |
270 |
|
} |
271 |
TARGET_NATIVE_FILE_READ(fd,(bufptr + offset),len,bytesRead,result); |
|
272 |
(*env)->ReleaseByteArrayElements(env, buf, bufptr, 0); |
TARGET_NATIVE_FILE_READ (fd, (bufptr + offset), len, bytesRead, result); |
273 |
if (result != TARGET_NATIVE_OK) |
(*env)->ReleaseByteArrayElements (env, buf, bufptr, 0); |
274 |
JCL_ThrowException(env, |
if (result != TARGET_NATIVE_OK) |
275 |
"java/io/IOException", |
JCL_ThrowException (env, |
276 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
"java/io/IOException", |
277 |
|
TARGET_NATIVE_LAST_ERROR_STRING ()); |
278 |
|
|
279 |
if (bytesRead == 0) |
if (bytesRead == 0) |
280 |
return(-1); |
return (-1); |
281 |
|
|
282 |
return(bytesRead); |
return (bytesRead); |
283 |
} |
} |
284 |
else |
else |
285 |
{ |
{ |
286 |
return(-1); |
return (-1); |
287 |
} |
} |
288 |
#endif /* not WITHOUT_FILESYSTEM */ |
#endif /* not WITHOUT_FILESYSTEM */ |
289 |
} |
} |
290 |
|
|
295 |
*/ |
*/ |
296 |
|
|
297 |
jint |
jint |
298 |
_javaio_write(JNIEnv *env, jint fd, jarray buf, jint offset, jint len) |
_javaio_write (JNIEnv * env, jint fd, jarray buf, jint offset, jint len) |
299 |
{ |
{ |
300 |
#ifndef WITHOUT_FILESYSTEM |
#ifndef WITHOUT_FILESYSTEM |
301 |
jbyte *bufptr; |
jbyte *bufptr; |
302 |
int bytes_written; |
int bytes_written; |
303 |
int result; |
int result; |
304 |
|
|
305 |
if (len == 0) |
if (len == 0) |
306 |
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */ |
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */ |
307 |
|
|
308 |
bufptr = (*env)->GetByteArrayElements(env, buf, 0); |
bufptr = (*env)->GetByteArrayElements (env, buf, 0); |
309 |
if (bufptr == NULL) |
if (bufptr == NULL) |
310 |
{ |
{ |
311 |
JCL_ThrowException(env, "java/io/IOException", "Internal Error: get byte array"); |
JCL_ThrowException (env, "java/io/IOException", |
312 |
return(-1); |
"Internal Error: get byte array"); |
313 |
|
return (-1); |
314 |
} |
} |
315 |
|
|
316 |
TARGET_NATIVE_FILE_WRITE(fd,(bufptr + offset),len,bytes_written,result); |
TARGET_NATIVE_FILE_WRITE (fd, (bufptr + offset), len, bytes_written, |
317 |
(*env)->ReleaseByteArrayElements(env, buf, bufptr, 0); |
result); |
318 |
|
(*env)->ReleaseByteArrayElements (env, buf, bufptr, 0); |
319 |
if (result != TARGET_NATIVE_OK) |
if (result != TARGET_NATIVE_OK) |
320 |
JCL_ThrowException(env, |
JCL_ThrowException (env, |
321 |
"java/io/IOException", |
"java/io/IOException", |
322 |
TARGET_NATIVE_LAST_ERROR_STRING()); |
TARGET_NATIVE_LAST_ERROR_STRING ()); |
323 |
|
|
324 |
if (bytes_written == 0) |
if (bytes_written == 0) |
325 |
return(-1); |
return (-1); |
326 |
|
|
327 |
return(bytes_written); |
return (bytes_written); |
328 |
#else /* not WITHOUT_FILESYSTEM */ |
#else /* not WITHOUT_FILESYSTEM */ |
329 |
jbyte *bufptr; |
jbyte *bufptr; |
330 |
int bytesWritten; |
int bytesWritten; |
331 |
|
|
332 |
if ((fd==0) || (fd==1) || (fd==2)) |
if ((fd == 0) || (fd == 1) || (fd == 2)) |
333 |
{ |
{ |
334 |
if (len == 0) |
if (len == 0) |
335 |
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */ |
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */ |
|
|
|
|
bufptr = (*env)->GetByteArrayElements(env, buf, 0); |
|
|
if (bufptr==NULL) |
|
|
{ |
|
|
JCL_ThrowException(env, "java/io/IOException", "Internal Error"); |
|
|
return(-1); |
|
|
} |
|
|
|
|
|
TARGET_NATIVE_FILE_WRITE(fd,(bufptr + offset),len,bytes_written,result); |
|
|
(*env)->ReleaseByteArrayElements(env, buf, bufptr, 0); |
|
|
|
|
|
if (bytes_written == -1) |
|
|
JCL_ThrowException(env, |
|
|
"java/io/IOException", |
|
|
TARGET_NATIVE_LAST_ERROR_STRING()); |
|
336 |
|
|
337 |
if (bytes_written == 0) |
bufptr = (*env)->GetByteArrayElements (env, buf, 0); |
338 |
return(-1); |
if (bufptr == NULL) |
339 |
|
{ |
340 |
|
JCL_ThrowException (env, "java/io/IOException", "Internal Error"); |
341 |
|
return (-1); |
342 |
|
} |
343 |
|
|
344 |
|
TARGET_NATIVE_FILE_WRITE (fd, (bufptr + offset), len, bytes_written, |
345 |
|
result); |
346 |
|
(*env)->ReleaseByteArrayElements (env, buf, bufptr, 0); |
347 |
|
|
348 |
|
if (bytes_written == -1) |
349 |
|
JCL_ThrowException (env, |
350 |
|
"java/io/IOException", |
351 |
|
TARGET_NATIVE_LAST_ERROR_STRING ()); |
352 |
|
|
353 |
return(bytes_written); |
if (bytes_written == 0) |
354 |
} |
return (-1); |
355 |
|
|
356 |
|
return (bytes_written); |
357 |
|
} |
358 |
else |
else |
359 |
{ |
{ |
360 |
return(-1); |
return (-1); |
361 |
} |
} |
362 |
#endif /* not WITHOUT_FILESYSTEM */ |
#endif /* not WITHOUT_FILESYSTEM */ |
363 |
} |
} |