Merged everything with MIPS version
[cacao.git] / nat / RandomAccessFile.c
1 /* class: java/io/RandomAccessFile */
2
3 /*
4  * Class:     java/io/RandomAccessFile
5  * Method:    close
6  * Signature: ()V
7  */
8 JNIEXPORT void JNICALL Java_java_io_RandomAccessFile_close ( JNIEnv *env ,  struct java_io_RandomAccessFile* this)
9 {
10   if (this->fd->fd >= 0) {
11     s4 r = close (this->fd->fd);
12                 this->fd->fd = -1;
13                 if (r<0) 
14                         exceptionptr = native_new_and_init (class_java_io_IOException);
15                 }
16 }
17
18 /*
19  * Class:     java/io/RandomAccessFile
20  * Method:    getFilePointer
21  * Signature: ()J
22  */
23 JNIEXPORT s8 JNICALL Java_java_io_RandomAccessFile_getFilePointer ( JNIEnv *env ,  struct java_io_RandomAccessFile* this)
24 {
25         s4 p = lseek (this->fd->fd, 0, SEEK_CUR);
26         if (p>=0) return builtin_i2l(p);
27         exceptionptr = native_new_and_init (class_java_io_IOException);
28         return builtin_i2l(0);
29 }
30
31 /*
32  * Class:     java/io/RandomAccessFile
33  * Method:    initIDs
34  * Signature: ()V
35  */
36 JNIEXPORT void JNICALL Java_java_io_RandomAccessFile_initIDs ( JNIEnv *env  )
37 {
38     /* no IDs required */
39 }
40
41 /*
42  * Class:     java/io/RandomAccessFile
43  * Method:    length
44  * Signature: ()J
45  */
46 JNIEXPORT s8 JNICALL Java_java_io_RandomAccessFile_length ( JNIEnv *env ,  struct java_io_RandomAccessFile* this)
47 {
48         struct stat buffer;
49         s4 r = fstat(this->fd->fd, &buffer);
50         if (r>=0) return builtin_i2l(buffer.st_size);
51         exceptionptr = native_new_and_init (class_java_io_IOException);
52         return builtin_i2l(0);
53 }
54
55 /*
56  * Class:     java/io/RandomAccessFile
57  * Method:    open
58  * Signature: (Ljava/lang/String;Z)V
59  */
60 JNIEXPORT void JNICALL Java_java_io_RandomAccessFile_open ( JNIEnv *env ,  struct java_io_RandomAccessFile* this, struct java_lang_String* name, s4 writeable)
61 {
62         s4 fd;
63         char *fname = javastring_tochar ((java_objectheader*)name);
64         
65         if (writeable) fd = open (fname, O_RDWR, 0);
66         else           fd = open (fname, O_RDONLY, 0);
67         if (fd==-1) goto fail;
68
69         threadedFileDescriptor(fd);
70         
71         this->fd->fd = fd;
72         return;
73
74         fail:
75                 exceptionptr = native_new_and_init (class_java_io_IOException);
76                 return;
77 }
78
79 /*
80  * Class:     java/io/RandomAccessFile
81  * Method:    read
82  * Signature: ()I
83  */
84 JNIEXPORT s4 JNICALL Java_java_io_RandomAccessFile_read ( JNIEnv *env ,  struct java_io_RandomAccessFile* this)
85 {
86         s4 r;
87         u1 buffer[1];
88         r = threadedRead (this->fd->fd, (char *) buffer, 1);    
89         if (r>0) return buffer[1];
90         if (r==0) return -1;
91         exceptionptr = native_new_and_init (class_java_io_IOException);
92         return 0;
93 }
94
95 /*
96  * Class:     java/io/RandomAccessFile
97  * Method:    readBytes
98  * Signature: ([BII)I
99  */
100 JNIEXPORT s4 JNICALL Java_java_io_RandomAccessFile_readBytes ( JNIEnv *env ,  struct java_io_RandomAccessFile* this, java_bytearray* buffer, s4 start, s4 len)
101 {
102         s4 r = threadedRead (this->fd->fd, (char *) buffer->data+start, len);
103         if (r>0) return r;
104         if (r==0) return -1;
105         exceptionptr = native_new_and_init (class_java_io_IOException);
106         return 0;
107 }
108
109 /*
110  * Class:     java/io/RandomAccessFile
111  * Method:    seek
112  * Signature: (J)V
113  */
114 JNIEXPORT void JNICALL Java_java_io_RandomAccessFile_seek ( JNIEnv *env ,  struct java_io_RandomAccessFile* this, s8 offset)
115 {
116         s4 p = lseek (this->fd->fd, builtin_l2i(offset), SEEK_SET); 
117         if (p<0) {
118                 exceptionptr = native_new_and_init (class_java_io_IOException);
119                 }
120 }
121
122 /*
123  * Class:     java/io/RandomAccessFile
124  * Method:    setLength
125  * Signature: (J)V
126  */
127 JNIEXPORT void JNICALL Java_java_io_RandomAccessFile_setLength ( JNIEnv *env ,  struct java_io_RandomAccessFile* this, s8 length)
128 {
129   if (ftruncate(this->fd->fd, length)<0)
130                 exceptionptr = native_new_and_init (class_java_io_IOException);
131 }
132
133 /*
134  * Class:     java/io/RandomAccessFile
135  * Method:    write
136  * Signature: (I)V
137  */
138 JNIEXPORT void JNICALL Java_java_io_RandomAccessFile_write ( JNIEnv *env ,  struct java_io_RandomAccessFile* this, s4 byte)
139 {
140         u1 buffer[1];
141         int r;
142         buffer[1] = byte;
143         r = write (this->fd->fd, buffer, 1);
144         if (r<0) {
145                 exceptionptr = native_new_and_init (class_java_io_IOException);
146                 }
147 }
148
149 /*
150  * Class:     java/io/RandomAccessFile
151  * Method:    writeBytes
152  * Signature: ([BII)V
153  */
154 JNIEXPORT void JNICALL Java_java_io_RandomAccessFile_writeBytes ( JNIEnv *env ,  struct java_io_RandomAccessFile* this, java_bytearray* buffer, s4 start, s4 len)
155 {
156         s4 o;
157         if (len == 0)
158                 return;
159         o = threadedWrite (this->fd->fd, (char *) buffer->data+start, len);
160         if (o!=len) exceptionptr = native_new_and_init (class_java_io_IOException);
161 }
162
163
164
165
166
167
168
169
170