1
/* javaio.c - Common java.io native functions
2
Copyright (C) 1998, 2002, 2004 Free Software Foundation, Inc.
4
This file is part of GNU Classpath.
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING. If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library. Thus, the terms and
23
conditions of the GNU General Public License cover the whole
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module. An independent module is a module which is not derived from
33
or based on this library. If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so. If you do not wish to do so, delete this
36
exception statement from your version. */
38
/* do not move; needed here because of some macro definitions */
47
#include "target_native.h"
48
#ifndef WITHOUT_FILESYSTEM
49
#include "target_native_file.h"
51
#include "target_native_math_int.h"
56
* Function to open a file
60
_javaio_open_read (JNIEnv * env, jstring name)
62
#ifndef WITHOUT_FILESYSTEM
67
filename = JCL_jstring_to_cstring (env, name);
71
TARGET_NATIVE_FILE_OPEN_READ (filename, fd, result);
72
(*env)->ReleaseStringUTFChars (env, name, filename);
73
if (result != TARGET_NATIVE_OK)
75
if (TARGET_NATIVE_LAST_ERROR () == TARGET_NATIVE_ERROR_NO_SUCH_FILE)
76
JCL_ThrowException (env,
77
"java/io/FileNotFoundException",
78
TARGET_NATIVE_LAST_ERROR_STRING ());
80
JCL_ThrowException (env,
81
"java/io/IOException",
82
TARGET_NATIVE_LAST_ERROR_STRING ());
85
JCL_free_cstring (env, name, filename);
87
#else /* not WITHOUT_FILESYSTEM */
89
#endif /* not WITHOUT_FILESYSTEM */
93
* Function to open a file for reading/writing
97
_javaio_open_readwrite (JNIEnv * env, jstring name)
99
#ifndef WITHOUT_FILESYSTEM
100
const char *filename;
104
filename = JCL_jstring_to_cstring (env, name);
105
if (filename == NULL)
108
TARGET_NATIVE_FILE_OPEN_READWRITE (filename, fd, result);
109
(*env)->ReleaseStringUTFChars (env, name, filename);
110
if (result != TARGET_NATIVE_OK)
112
if (TARGET_NATIVE_LAST_ERROR () == TARGET_NATIVE_ERROR_NO_SUCH_FILE)
113
JCL_ThrowException (env,
114
"java/io/FileNotFoundException",
115
TARGET_NATIVE_LAST_ERROR_STRING ());
117
JCL_ThrowException (env,
118
"java/io/IOException",
119
TARGET_NATIVE_LAST_ERROR_STRING ());
122
JCL_free_cstring (env, name, filename);
124
#else /* not WITHOUT_FILESYSTEM */
126
#endif /* not WITHOUT_FILESYSTEM */
129
/*************************************************************************/
132
* Function to close a file
136
_javaio_close (JNIEnv * env, jint fd)
138
#ifndef WITHOUT_FILESYSTEM
143
TARGET_NATIVE_FILE_CLOSE (fd, result);
144
if (result != TARGET_NATIVE_OK)
145
JCL_ThrowException (env,
146
"java/io/IOException",
147
TARGET_NATIVE_LAST_ERROR_STRING ());
149
#else /* not WITHOUT_FILESYSTEM */
150
#endif /* not WITHOUT_FILESYSTEM */
153
/*************************************************************************/
156
* Skips bytes in a file
160
_javaio_skip_bytes (JNIEnv * env, jint fd, jlong num_bytes)
162
#ifndef WITHOUT_FILESYSTEM
163
jlong current_offset, new_offset;
166
TARGET_NATIVE_FILE_SEEK_CURRENT (fd, TARGET_NATIVE_MATH_INT_INT64_CONST_0,
167
current_offset, result);
168
if (result != TARGET_NATIVE_OK)
169
JCL_ThrowException (env,
170
"java/io/IOException",
171
TARGET_NATIVE_LAST_ERROR_STRING ());
173
TARGET_NATIVE_FILE_SEEK_CURRENT (fd, num_bytes, new_offset, result);
174
if (result != TARGET_NATIVE_OK)
175
JCL_ThrowException (env,
176
"java/io/IOException",
177
TARGET_NATIVE_LAST_ERROR_STRING ());
179
return (TARGET_NATIVE_MATH_INT_INT64_SUB (new_offset, current_offset));
180
#else /* not WITHOUT_FILESYSTEM */
181
return (TARGET_NATIVE_MATH_INT_INT64_CONST_0);
182
#endif /* not WITHOUT_FILESYSTEM */
185
/*************************************************************************/
188
* Gets the size of the file
192
_javaio_get_file_length (JNIEnv * env, jint fd)
194
#ifndef WITHOUT_FILESYSTEM
198
TARGET_NATIVE_FILE_SIZE (fd, length, result);
199
if (result != TARGET_NATIVE_OK)
201
JCL_ThrowException (env,
202
"java/io/IOException",
203
TARGET_NATIVE_LAST_ERROR_STRING ());
204
return (TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1);
208
#else /* not WITHOUT_FILESYSTEM */
209
return (TARGET_NATIVE_MATH_INT_INT64_CONST_0);
210
#endif /* not WITHOUT_FILESYSTEM */
213
/*************************************************************************/
216
* Reads data from a file
220
_javaio_read (JNIEnv * env, jint fd, jarray buf, jint offset, jint len)
222
#ifndef WITHOUT_FILESYSTEM
227
assert (offset >= 0);
231
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */
233
bufptr = (*env)->GetByteArrayElements (env, buf, JNI_FALSE);
236
JCL_ThrowException (env, "java/io/IOException",
237
"Internal Error: get byte array fail");
241
TARGET_NATIVE_FILE_READ (fd, (bufptr + offset), len, bytesRead, result);
242
(*env)->ReleaseByteArrayElements (env, buf, bufptr, 0);
243
if (result != TARGET_NATIVE_OK)
244
JCL_ThrowException (env,
245
"java/io/IOException",
246
TARGET_NATIVE_LAST_ERROR_STRING ());
252
#else /* not WITHOUT_FILESYSTEM */
256
assert (offset >= 0);
259
if ((fd == 0) || (fd == 1) || (fd == 2))
262
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */
264
bufptr = (*env)->GetByteArrayElements (env, buf, JNI_FALSE);
267
JCL_ThrowException (env, "java/io/IOException",
268
"Internal Error: get byte array");
272
TARGET_NATIVE_FILE_READ (fd, (bufptr + offset), len, bytesRead, result);
273
(*env)->ReleaseByteArrayElements (env, buf, bufptr, 0);
274
if (result != TARGET_NATIVE_OK)
275
JCL_ThrowException (env,
276
"java/io/IOException",
277
TARGET_NATIVE_LAST_ERROR_STRING ());
288
#endif /* not WITHOUT_FILESYSTEM */
291
/*************************************************************************/
294
* Writes data to a file
298
_javaio_write (JNIEnv * env, jint fd, jarray buf, jint offset, jint len)
300
#ifndef WITHOUT_FILESYSTEM
306
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */
308
bufptr = (*env)->GetByteArrayElements (env, buf, 0);
311
JCL_ThrowException (env, "java/io/IOException",
312
"Internal Error: get byte array");
316
TARGET_NATIVE_FILE_WRITE (fd, (bufptr + offset), len, bytes_written,
318
(*env)->ReleaseByteArrayElements (env, buf, bufptr, 0);
319
if (result != TARGET_NATIVE_OK)
320
JCL_ThrowException (env,
321
"java/io/IOException",
322
TARGET_NATIVE_LAST_ERROR_STRING ());
324
if (bytes_written == 0)
327
return (bytes_written);
328
#else /* not WITHOUT_FILESYSTEM */
332
if ((fd == 0) || (fd == 1) || (fd == 2))
335
return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */
337
bufptr = (*env)->GetByteArrayElements (env, buf, 0);
340
JCL_ThrowException (env, "java/io/IOException", "Internal Error");
344
TARGET_NATIVE_FILE_WRITE (fd, (bufptr + offset), len, bytes_written,
346
(*env)->ReleaseByteArrayElements (env, buf, bufptr, 0);
348
if (bytes_written == -1)
349
JCL_ThrowException (env,
350
"java/io/IOException",
351
TARGET_NATIVE_LAST_ERROR_STRING ());
353
if (bytes_written == 0)
356
return (bytes_written);
362
#endif /* not WITHOUT_FILESYSTEM */