~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to native/jni/java-io/javaio.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* javaio.c - Common java.io native functions
2
 
   Copyright (C) 1998, 2002, 2004 Free Software Foundation, Inc.
3
 
 
4
 
This file is part of GNU Classpath.
5
 
 
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)
9
 
any later version.
10
 
 
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.
15
 
 
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
19
 
02110-1301 USA.
20
 
 
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
24
 
combination.
25
 
 
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. */
37
 
 
38
 
/* do not move; needed here because of some macro definitions */
39
 
#include <config.h>
40
 
 
41
 
#include <stdio.h>
42
 
#include <stdlib.h>
43
 
 
44
 
#include <jni.h>
45
 
#include <jcl.h>
46
 
 
47
 
#include "target_native.h"
48
 
#ifndef WITHOUT_FILESYSTEM
49
 
#include "target_native_file.h"
50
 
#endif
51
 
#include "target_native_math_int.h"
52
 
 
53
 
#include "javaio.h"
54
 
 
55
 
/*
56
 
 * Function to open a file
57
 
 */
58
 
 
59
 
jint
60
 
_javaio_open_read (JNIEnv * env, jstring name)
61
 
{
62
 
#ifndef WITHOUT_FILESYSTEM
63
 
  const char *filename;
64
 
  int fd;
65
 
  int result;
66
 
 
67
 
  filename = JCL_jstring_to_cstring (env, name);
68
 
  if (filename == NULL)
69
 
    return (-1);
70
 
 
71
 
  TARGET_NATIVE_FILE_OPEN_READ (filename, fd, result);
72
 
  (*env)->ReleaseStringUTFChars (env, name, filename);
73
 
  if (result != TARGET_NATIVE_OK)
74
 
    {
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 ());
79
 
      else
80
 
        JCL_ThrowException (env,
81
 
                            "java/io/IOException",
82
 
                            TARGET_NATIVE_LAST_ERROR_STRING ());
83
 
    }
84
 
 
85
 
  JCL_free_cstring (env, name, filename);
86
 
  return (fd);
87
 
#else /* not WITHOUT_FILESYSTEM */
88
 
  return (-1);
89
 
#endif /* not WITHOUT_FILESYSTEM */
90
 
}
91
 
 
92
 
/*
93
 
 * Function to open a file for reading/writing
94
 
 */
95
 
 
96
 
jint
97
 
_javaio_open_readwrite (JNIEnv * env, jstring name)
98
 
{
99
 
#ifndef WITHOUT_FILESYSTEM
100
 
  const char *filename;
101
 
  int fd;
102
 
  int result;
103
 
 
104
 
  filename = JCL_jstring_to_cstring (env, name);
105
 
  if (filename == NULL)
106
 
    return (-1);
107
 
 
108
 
  TARGET_NATIVE_FILE_OPEN_READWRITE (filename, fd, result);
109
 
  (*env)->ReleaseStringUTFChars (env, name, filename);
110
 
  if (result != TARGET_NATIVE_OK)
111
 
    {
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 ());
116
 
      else
117
 
        JCL_ThrowException (env,
118
 
                            "java/io/IOException",
119
 
                            TARGET_NATIVE_LAST_ERROR_STRING ());
120
 
    }
121
 
 
122
 
  JCL_free_cstring (env, name, filename);
123
 
  return (fd);
124
 
#else /* not WITHOUT_FILESYSTEM */
125
 
  return (-1);
126
 
#endif /* not WITHOUT_FILESYSTEM */
127
 
}
128
 
 
129
 
/*************************************************************************/
130
 
 
131
 
/*
132
 
 * Function to close a file
133
 
 */
134
 
 
135
 
void
136
 
_javaio_close (JNIEnv * env, jint fd)
137
 
{
138
 
#ifndef WITHOUT_FILESYSTEM
139
 
  int result;
140
 
 
141
 
  if (fd != -1)
142
 
    {
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 ());
148
 
    }
149
 
#else /* not WITHOUT_FILESYSTEM */
150
 
#endif /* not WITHOUT_FILESYSTEM */
151
 
}
152
 
 
153
 
/*************************************************************************/
154
 
 
155
 
/*
156
 
 * Skips bytes in a file
157
 
 */
158
 
 
159
 
jlong
160
 
_javaio_skip_bytes (JNIEnv * env, jint fd, jlong num_bytes)
161
 
{
162
 
#ifndef WITHOUT_FILESYSTEM
163
 
  jlong current_offset, new_offset;
164
 
  int result;
165
 
 
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 ());
172
 
 
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 ());
178
 
 
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 */
183
 
}
184
 
 
185
 
/*************************************************************************/
186
 
 
187
 
/*
188
 
 * Gets the size of the file
189
 
 */
190
 
 
191
 
jlong
192
 
_javaio_get_file_length (JNIEnv * env, jint fd)
193
 
{
194
 
#ifndef WITHOUT_FILESYSTEM
195
 
  jlong length;
196
 
  int result;
197
 
 
198
 
  TARGET_NATIVE_FILE_SIZE (fd, length, result);
199
 
  if (result != TARGET_NATIVE_OK)
200
 
    {
201
 
      JCL_ThrowException (env,
202
 
                          "java/io/IOException",
203
 
                          TARGET_NATIVE_LAST_ERROR_STRING ());
204
 
      return (TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1);
205
 
    }
206
 
 
207
 
  return (length);
208
 
#else /* not WITHOUT_FILESYSTEM */
209
 
  return (TARGET_NATIVE_MATH_INT_INT64_CONST_0);
210
 
#endif /* not WITHOUT_FILESYSTEM */
211
 
}
212
 
 
213
 
/*************************************************************************/
214
 
 
215
 
/*
216
 
 * Reads data from a file
217
 
 */
218
 
 
219
 
jint
220
 
_javaio_read (JNIEnv * env, jint fd, jarray buf, jint offset, jint len)
221
 
{
222
 
#ifndef WITHOUT_FILESYSTEM
223
 
  jbyte *bufptr;
224
 
  int bytesRead;
225
 
  int result;
226
 
 
227
 
  assert (offset >= 0);
228
 
  assert (len >= 0);
229
 
 
230
 
  if (len == 0)
231
 
    return 0;                   /* Nothing todo, and GetByteArrayElements() seems undefined. */
232
 
 
233
 
  bufptr = (*env)->GetByteArrayElements (env, buf, JNI_FALSE);
234
 
  if (bufptr == NULL)
235
 
    {
236
 
      JCL_ThrowException (env, "java/io/IOException",
237
 
                          "Internal Error: get byte array fail");
238
 
      return (-1);
239
 
    }
240
 
 
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 ());
247
 
 
248
 
  if (bytesRead == 0)
249
 
    return (-1);
250
 
 
251
 
  return (bytesRead);
252
 
#else /* not WITHOUT_FILESYSTEM */
253
 
  jbyte *bufptr;
254
 
  int bytesRead;
255
 
 
256
 
  assert (offset >= 0);
257
 
  assert (len >= 0);
258
 
 
259
 
  if ((fd == 0) || (fd == 1) || (fd == 2))
260
 
    {
261
 
      if (len == 0)
262
 
        return 0;               /* Nothing todo, and GetByteArrayElements() seems undefined. */
263
 
 
264
 
      bufptr = (*env)->GetByteArrayElements (env, buf, JNI_FALSE);
265
 
      if (bufptr == NULL)
266
 
        {
267
 
          JCL_ThrowException (env, "java/io/IOException",
268
 
                              "Internal Error: get byte array");
269
 
          return (-1);
270
 
        }
271
 
 
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 ());
278
 
 
279
 
      if (bytesRead == 0)
280
 
        return (-1);
281
 
 
282
 
      return (bytesRead);
283
 
    }
284
 
  else
285
 
    {
286
 
      return (-1);
287
 
    }
288
 
#endif /* not WITHOUT_FILESYSTEM */
289
 
}
290
 
 
291
 
/*************************************************************************/
292
 
 
293
 
/*
294
 
 * Writes data to a file
295
 
 */
296
 
 
297
 
jint
298
 
_javaio_write (JNIEnv * env, jint fd, jarray buf, jint offset, jint len)
299
 
{
300
 
#ifndef WITHOUT_FILESYSTEM
301
 
  jbyte *bufptr;
302
 
  int bytes_written;
303
 
  int result;
304
 
 
305
 
  if (len == 0)
306
 
    return 0;                   /* Nothing todo, and GetByteArrayElements() seems undefined. */
307
 
 
308
 
  bufptr = (*env)->GetByteArrayElements (env, buf, 0);
309
 
  if (bufptr == NULL)
310
 
    {
311
 
      JCL_ThrowException (env, "java/io/IOException",
312
 
                          "Internal Error: get byte array");
313
 
      return (-1);
314
 
    }
315
 
 
316
 
  TARGET_NATIVE_FILE_WRITE (fd, (bufptr + offset), len, bytes_written,
317
 
                            result);
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 ());
323
 
 
324
 
  if (bytes_written == 0)
325
 
    return (-1);
326
 
 
327
 
  return (bytes_written);
328
 
#else /* not WITHOUT_FILESYSTEM */
329
 
  jbyte *bufptr;
330
 
  int bytesWritten;
331
 
 
332
 
  if ((fd == 0) || (fd == 1) || (fd == 2))
333
 
    {
334
 
      if (len == 0)
335
 
        return 0;               /* Nothing todo, and GetByteArrayElements() seems undefined. */
336
 
 
337
 
      bufptr = (*env)->GetByteArrayElements (env, buf, 0);
338
 
      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
 
      if (bytes_written == 0)
354
 
        return (-1);
355
 
 
356
 
      return (bytes_written);
357
 
    }
358
 
  else
359
 
    {
360
 
      return (-1);
361
 
    }
362
 
#endif /* not WITHOUT_FILESYSTEM */
363
 
}