/[classpath]/classpath/native/jni/java-io/javaio.c
ViewVC logotype

Diff of /classpath/native/jni/java-io/javaio.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7 by mark, Fri Nov 29 18:41:04 2002 UTC revision 1.8 by rupp, Wed Jul 9 08:14:18 2003 UTC
# Line 35  this exception to your version of the li Line 35  this exception to your version of the li
35  obligated to do so.  If you do not wish to do so, delete this  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */  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>  #include <stdio.h>
42  #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  
 #include <errno.h>  
 #include <fcntl.h>  
 #include <unistd.h>  
 #include <sys/stat.h>  
 #include <malloc.h>  
43    
44  #include <jni.h>  #include <jni.h>
45  #include <jcl.h>  #include <jcl.h>
46    
47  #include "javaio.h"  #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   * Function to open a file
57   */   */
58    
59  jint  jint
60  _javaio_open(JNIEnv *env, jstring name, int flags)  _javaio_open_read(JNIEnv *env, jstring name)
61  {  {
62    char *str_name;  #ifndef WITHOUT_FILESYSTEM
63    int fd;    const char *filename;
64      int        fd;
65      int        result;
66    
67    // fprintf(stderr,"call %s (line %d):\n",__FUNCTION__,__LINE__);
68      filename = JCL_jstring_to_cstring(env, name);
69      if (filename == NULL)
70        return(-1);
71    
72    str_name = JCL_jstring_to_cstring(env, name);    TARGET_NATIVE_FILE_OPEN_READ(filename,fd,result);
73    if (!str_name)    (*env)->ReleaseStringUTFChars(env, name, filename);
74      if (result != TARGET_NATIVE_OK)
75        {
76          if (TARGET_NATIVE_LAST_ERROR() == TARGET_NATIVE_ERROR_NO_SUCH_FILE)
77            JCL_ThrowException(env,
78                               "java/io/FileNotFoundException",
79                               TARGET_NATIVE_LAST_ERROR_STRING());
80          else
81            JCL_ThrowException(env,
82                               "java/io/IOException",
83                               TARGET_NATIVE_LAST_ERROR_STRING());
84        }
85    
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    // fprintf(stderr,"call %s (line %d):\n",__FUNCTION__,__LINE__);
105      filename = JCL_jstring_to_cstring(env, name);
106      if (filename == NULL)
107      return(-1);      return(-1);
108    
109    fd = open(str_name, flags, 0777);    TARGET_NATIVE_FILE_OPEN_READWRITE(filename,fd,result);
110    (*env)->ReleaseStringUTFChars(env, name, str_name);    (*env)->ReleaseStringUTFChars(env, name, filename);
111    if (fd == -1)    if (result != TARGET_NATIVE_OK)
112      {      {
113        if (errno == ENOENT)        if (TARGET_NATIVE_LAST_ERROR() == TARGET_NATIVE_ERROR_NO_SUCH_FILE)
114          JCL_ThrowException(env, "java/io/FileNotFoundException",          JCL_ThrowException(env,
115                             strerror(errno));                             "java/io/FileNotFoundException",
116                               TARGET_NATIVE_LAST_ERROR_STRING());
117        else        else
118          JCL_ThrowException(env, "java/io/IOException", strerror(errno));          JCL_ThrowException(env,
119                               "java/io/IOException",
120                               TARGET_NATIVE_LAST_ERROR_STRING());
121      }      }
122    
123    return(fd);    return(fd);
124    #else /* not WITHOUT_FILESYSTEM */
125      return(-1);
126    #endif /* not WITHOUT_FILESYSTEM */
127  }  }
128    
129  /*************************************************************************/  /*************************************************************************/
# Line 88  _javaio_open(JNIEnv *env, jstring name, Line 135  _javaio_open(JNIEnv *env, jstring name,
135  void  void
136  _javaio_close(JNIEnv *env, jint fd)  _javaio_close(JNIEnv *env, jint fd)
137  {  {
138    int rc = 0;  #ifndef WITHOUT_FILESYSTEM
139      int result;
140    
141    if (fd != -1)    if (fd != -1)
142      rc = close(fd);      {
143          TARGET_NATIVE_FILE_CLOSE(fd,result);
144    if (rc == -1)        if (result != TARGET_NATIVE_OK)
145      JCL_ThrowException(env, "java/io/IOException", strerror(errno));          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  /*************************************************************************/  /*************************************************************************/
# Line 106  _javaio_close(JNIEnv *env, jint fd) Line 159  _javaio_close(JNIEnv *env, jint fd)
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    int cur, new;  #ifndef WITHOUT_FILESYSTEM
163      jlong current_offset, new_offset;
164    cur = lseek(fd, 0, SEEK_CUR);    int   result;
165    if (cur == -1)  
166      JCL_ThrowException(env, "java/io/IOException", strerror(errno));  // fprintf(stderr,"call %s (line %d):\n",__FUNCTION__,__LINE__);
167      TARGET_NATIVE_FILE_SEEK_CURRENT(fd,JNI_JLONG_CONST_0,current_offset,result);
168    new = lseek(fd, num_bytes, SEEK_CUR);    if (result != TARGET_NATIVE_OK)
169    if (new == -1)      JCL_ThrowException(env,
170      JCL_ThrowException(env, "java/io/IOException", strerror(errno));                         "java/io/IOException",
171                           TARGET_NATIVE_LAST_ERROR_STRING());
172    return(new - cur);  
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(JNI_JLONG_CONST_0);
182    #endif /* not WITHOUT_FILESYSTEM */
183  }  }
184    
185  /*************************************************************************/  /*************************************************************************/
# Line 128  _javaio_skip_bytes(JNIEnv *env, jint fd, Line 191  _javaio_skip_bytes(JNIEnv *env, jint fd,
191  jlong  jlong
192  _javaio_get_file_length(JNIEnv *env, jint fd)  _javaio_get_file_length(JNIEnv *env, jint fd)
193  {  {
194    struct stat buf;  #ifndef WITHOUT_FILESYSTEM
195    int rc;    jlong length;
196      int   result;
197    rc = fstat(fd, &buf);  
198    if (rc == -1)  // fprintf(stderr,"call %s (line %d):\n",__FUNCTION__,__LINE__);
199      TARGET_NATIVE_FILE_SIZE(fd,length,result);
200      if (result != TARGET_NATIVE_OK)
201      {      {
202        JCL_ThrowException(env, "java/io/IOException", strerror(errno));        JCL_ThrowException(env,
203        return(-1);                           "java/io/IOException",
204                             TARGET_NATIVE_LAST_ERROR_STRING());
205          return(TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1);
206      }      }
207    
208    return(buf.st_size);    return(length);
209    #else /* not WITHOUT_FILESYSTEM */
210      return(JNI_JLONG_CONST_0);
211    #endif /* not WITHOUT_FILESYSTEM */
212  }  }
213    
214  /*************************************************************************/  /*************************************************************************/
# Line 151  jint Line 221  jint
221  _javaio_read(JNIEnv *env, jobject obj, jint fd, jarray buf, jint offset,  _javaio_read(JNIEnv *env, jobject obj, jint fd, jarray buf, jint offset,
222               jint len)               jint len)
223  {  {
224    #ifndef WITHOUT_FILESYSTEM
225    jbyte *bufptr;    jbyte *bufptr;
226    int rc;    int   bytesRead;
227      int   result;
228    
229      assert(offset>=0);
230      assert(len>=0);
231    
232    // fprintf(stderr,"call %s (line %d):\n",__FUNCTION__,__LINE__);
233    if (len == 0)    if (len == 0)
234      return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */      return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */
235    
236    bufptr = (*env)->GetByteArrayElements(env, buf, JNI_FALSE);    bufptr = (*env)->GetByteArrayElements(env, buf, JNI_FALSE);
237    if (!bufptr)    if (bufptr == NULL)
238      {      {
239        JCL_ThrowException(env, "java/io/IOException", "Internal Error");        JCL_ThrowException(env, "java/io/IOException", "Internal Error: get byte array fail");
240        return(-1);        return(-1);
241      }      }
242    
243    rc = read(fd, (bufptr + offset), len);    TARGET_NATIVE_FILE_READ(fd,(bufptr + offset),len,bytesRead,result);
   if (rc == -1)  
     JCL_ThrowException(env, "java/io/IOException", strerror(errno));  
   
244    (*env)->ReleaseByteArrayElements(env, buf, bufptr, 0);    (*env)->ReleaseByteArrayElements(env, buf, bufptr, 0);
245      if (result != TARGET_NATIVE_OK)
246        JCL_ThrowException(env,
247                           "java/io/IOException",
248                           TARGET_NATIVE_LAST_ERROR_STRING());
249    
250      if (bytesRead == 0)
251        return(-1);
252    
253    if (rc == 0)    return(bytesRead);
254      rc = -1;  #else /* not WITHOUT_FILESYSTEM */
255      jbyte *bufptr;
256      int   bytesRead;
257    
258      assert(offset>=0);
259      assert(len>=0);
260    
261    // fprintf(stderr,"call %s (line %d):\n",__FUNCTION__,__LINE__);
262      if ((fd==0) || (fd==1) || (fd==2))
263      {
264        if (len == 0)
265          return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */
266    
267        bufptr = (*env)->GetByteArrayElements(env, buf, JNI_FALSE);
268        if (bufptr == NULL)
269          {
270            JCL_ThrowException(env, "java/io/IOException", "Internal Error: get byte array");
271            return(-1);
272          }
273    
274        TARGET_NATIVE_FILE_READ(fd,(bufptr + offset),len,bytesRead,result);
275        (*env)->ReleaseByteArrayElements(env, buf, bufptr, 0);
276        if (result != TARGET_NATIVE_OK)
277          JCL_ThrowException(env,
278                             "java/io/IOException",
279                             TARGET_NATIVE_LAST_ERROR_STRING());
280    
281        if (bytesRead == 0)
282          return(-1);
283    
284    return(rc);      return(bytesRead);
285      }
286      else
287      {
288        return(-1);
289      }
290    #endif /* not WITHOUT_FILESYSTEM */
291  }  }
292    
293  /*************************************************************************/  /*************************************************************************/
# Line 186  jint Line 300  jint
300  _javaio_write(JNIEnv *env, jobject obj, jint fd, jarray buf, jint offset,  _javaio_write(JNIEnv *env, jobject obj, jint fd, jarray buf, jint offset,
301                jint len)                jint len)
302  {  {
303    #ifndef WITHOUT_FILESYSTEM
304    jbyte *bufptr;    jbyte *bufptr;
305    int rc;    int   bytes_written;
306      int   result;
307    
308    if (len == 0)    if (len == 0)
309      return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */      return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */
310    
311    bufptr = (*env)->GetByteArrayElements(env, buf, 0);    bufptr = (*env)->GetByteArrayElements(env, buf, 0);
312    if (!bufptr)    if (bufptr == NULL)
313      {      {
314        JCL_ThrowException(env, "java/io/IOException", "Internal Error");        JCL_ThrowException(env, "java/io/IOException", "Internal Error: get byte array");
315        return(-1);        return(-1);
316      }      }
317    
318    rc = write(fd, (bufptr + offset), len);    TARGET_NATIVE_FILE_WRITE(fd,(bufptr + offset),len,bytes_written,result);
   if (rc == -1)  
     JCL_ThrowException(env, "java/io/IOException", strerror(errno));  
   
319    (*env)->ReleaseByteArrayElements(env, buf, bufptr, 0);    (*env)->ReleaseByteArrayElements(env, buf, bufptr, 0);
320      if (result != TARGET_NATIVE_OK)
321        JCL_ThrowException(env,
322                           "java/io/IOException",
323                           TARGET_NATIVE_LAST_ERROR_STRING());
324    
325    if (rc == 0)    if (bytes_written == 0)
326      rc = -1;      return(-1);
   
   return(rc);  
 }  
   
327    
328      return(bytes_written);
329    #else /* not WITHOUT_FILESYSTEM */
330      jbyte *bufptr;
331      int   bytesWritten;
332    
333      if ((fd==0) || (fd==1) || (fd==2))
334      {
335        if (len == 0)
336          return 0; /* Nothing todo, and GetByteArrayElements() seems undefined. */
337    
338        bufptr = (*env)->GetByteArrayElements(env, buf, 0);
339        if (bufptr==NULL)
340          {
341            JCL_ThrowException(env, "java/io/IOException", "Internal Error");
342            return(-1);
343          }
344    
345        TARGET_NATIVE_FILE_WRITE(fd,(bufptr + offset),len,bytes_written,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    }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26