Updated version, required for darwin
[cacao.git] / nat / FileInputStream.c
1 /* class: java/io/FileInputStream */
2
3 /*
4  * Class:     java/io/FileInputStream
5  * Method:    available
6  * Signature: ()I
7  */
8 JNIEXPORT s4 JNICALL Java_java_io_FileInputStream_available ( JNIEnv *env ,  struct java_io_FileInputStream* this)
9 {
10         struct stat buffer;
11         s4 r1,r2;
12         
13         r1 = fstat (this->fd->fd, &buffer);
14         r2 = lseek(this->fd->fd, 0, SEEK_CUR);
15         
16         if ( (r1 >= 0) && (r2 >= 0) )  
17                 return buffer.st_size - r2; 
18
19         exceptionptr = native_new_and_init (class_java_io_IOException);
20         return 0;
21 }
22 /*
23  * Class:     java/io/FileInputStream
24  * Method:    close
25  * Signature: ()V
26  */
27 JNIEXPORT void JNICALL Java_java_io_FileInputStream_close ( JNIEnv *env ,  struct java_io_FileInputStream* this)
28 {
29         if (this->fd->fd >= 0) {
30                 s4 r = close (this->fd->fd);
31                 this->fd->fd = -1;
32                 if (r < 0) 
33                         exceptionptr = native_new_and_init (class_java_io_IOException);
34         }
35 }
36 /*
37  * Class:     java/io/FileInputStream
38  * Method:    initIDs
39  * Signature: ()V
40  */
41 JNIEXPORT void JNICALL Java_java_io_FileInputStream_initIDs ( JNIEnv *env  )
42 {
43     /* no IDs required */
44 }
45
46 /*
47  * Class:     java/io/FileInputStream
48  * Method:    open
49  * Signature: (Ljava/lang/String;)V
50  */
51 JNIEXPORT void JNICALL Java_java_io_FileInputStream_open ( JNIEnv *env ,  struct java_io_FileInputStream* this, struct java_lang_String* name)
52 {
53         s4 fd;
54         char *fname = javastring_tochar ((java_objectheader*)name);
55
56         if (!fname) goto fail;
57
58         fd = open (fname, O_RDONLY, 0);
59         if (fd<0) goto fail;
60         
61         threadedFileDescriptor(fd);
62
63         this->fd->fd = fd;
64         return;
65
66         fail:
67                 printf("failed to open: %s\n",fname);          
68
69                 exceptionptr = native_new_and_init (class_java_io_IOException);
70                 return;
71 }
72
73 /*
74  * Class:     java/io/FileInputStream
75  * Method:    read
76  * Signature: ()I
77  */
78 JNIEXPORT s4 JNICALL Java_java_io_FileInputStream_read ( JNIEnv *env ,  struct java_io_FileInputStream* this)
79 {
80         s4 r;
81         u1 buffer[1];
82         r = threadedRead (this->fd->fd, (char *) buffer, 1);    
83
84         if (r>0) return buffer[0];
85         if (r==0) return -1;
86         
87         exceptionptr = native_new_and_init (class_java_io_IOException);
88         return 0;
89 }
90 /*
91  * Class:     java/io/FileInputStream
92  * Method:    readBytes
93  * Signature: ([BII)I
94  */
95 JNIEXPORT s4 JNICALL Java_java_io_FileInputStream_readBytes ( JNIEnv *env ,  struct java_io_FileInputStream* this, java_bytearray* buffer, s4 start, s4 len)
96 {
97         s4 ret = threadedRead (this->fd->fd, (char *) buffer->data+start, len);
98         if (ret>0) return ret;
99         if (ret==0) return -1;
100         
101         exceptionptr = native_new_and_init (class_java_io_IOException);
102         return 0;
103 }
104 /*
105  * Class:     java/io/FileInputStream
106  * Method:    skip
107  * Signature: (J)J
108  */
109 JNIEXPORT s8 JNICALL Java_java_io_FileInputStream_skip ( JNIEnv *env ,  struct java_io_FileInputStream* this, s8 numbytes)
110 {
111         s4 ret = lseek (this->fd->fd, builtin_l2i(numbytes), SEEK_CUR);
112         if (ret>0) return builtin_i2l(ret);
113         if (ret==0) return builtin_i2l(-1);
114         
115         exceptionptr = native_new_and_init (class_java_io_IOException);
116         return builtin_i2l(0);
117 }
118
119
120
121