Merged with michi branch at rev 1684fe51cf3d.
[cacao.git] / src / native / vm / cldc1.1 / com_sun_cldc_io_ResourceInputStream.cpp
1 /* src/native/vm/cldc1.1/com_sun_cldc_io_ResourceInputStream.cpp
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <zlib.h>
33
34 #include "mm/memory.hpp"
35
36 #include "native/jni.hpp"
37 #include "native/native.hpp"
38
39 #if defined(ENABLE_JNI_HEADERS)
40 # include "native/include/com_sun_cldc_io_ResourceInputStream.h"
41 #endif
42
43 #include "threads/mutex.hpp"
44
45 #include "vm/jit/builtin.hpp"
46 #include "vm/exceptions.hpp"
47 #include "vm/javaobjects.hpp"
48 #include "vm/string.hpp"
49 #include "vm/types.h"
50 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
51 #include "vm/zip.hpp"
52
53
54 static java_handle_t* zip_read_resource(list_classpath_entry *lce, utf *name)
55 {
56         hashtable_zipfile_entry *htzfe;
57         lfh                      lfh;
58         u1                      *indata;
59         u1                      *outdata;
60         z_stream                 zs;
61         int                      err;
62         
63         classinfo *ci;
64
65         /* try to find the class in the current archive */
66
67         htzfe = zip_find(lce, name);
68
69         if (htzfe == NULL)
70                 return NULL;
71
72         /* read stuff from local file header */
73
74         lfh.filenamelength   = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
75         lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
76
77         indata = htzfe->data +
78                 LFH_HEADER_SIZE +
79                 lfh.filenamelength +
80                 lfh.extrafieldlength;
81
82         /* allocate buffer for uncompressed data */
83
84         outdata = MNEW(u1, htzfe->uncompressedsize);
85
86         /* how is the file stored? */
87
88         switch (htzfe->compressionmethod) {
89         case Z_DEFLATED:
90                 /* fill z_stream structure */
91
92                 zs.next_in   = indata;
93                 zs.avail_in  = htzfe->compressedsize;
94                 zs.next_out  = outdata;
95                 zs.avail_out = htzfe->uncompressedsize;
96
97                 zs.zalloc = Z_NULL;
98                 zs.zfree  = Z_NULL;
99                 zs.opaque = Z_NULL;
100
101                 /* initialize this inflate run */
102
103                 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
104                         vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
105
106                 /* decompress the file into buffer */
107
108                 err = inflate(&zs, Z_SYNC_FLUSH);
109
110                 if ((err != Z_STREAM_END) && (err != Z_OK))
111                         vm_abort("zip_get: inflate failed: %s", strerror(errno));
112
113                 /* finish this inflate run */
114
115                 if (inflateEnd(&zs) != Z_OK)
116                         vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
117                 break;
118
119         case 0:
120                 /* uncompressed file, just copy the data */
121                 MCOPY(outdata, indata, u1, htzfe->compressedsize);
122                 break;
123
124         default:
125                 vm_abort("zip_get: unknown compression method %d",
126                                  htzfe->compressionmethod);
127         }
128                 
129         // Create a file descriptor object.
130         ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
131         java_handle_t* h = native_new_and_init(ci);
132
133         if (h == NULL)
134                 return NULL;
135
136         com_sun_cldchi_jvm_FileDescriptor fd(h, (int64_t) outdata, 0, htzfe->uncompressedsize);
137
138         return fd.get_handle();
139 }
140
141
142 static java_handle_t* file_read_resource(char *path) 
143 {
144         int len;
145         struct stat statBuffer;
146         u1 *filep;
147         classinfo *ci;
148         int fd;
149         
150         fd = open(path, O_RDONLY);
151         
152         if (fd > 0) {
153                 
154                 if (fstat(fd, &statBuffer) != -1) {
155                         len = statBuffer.st_size;
156                 } else {  
157                         return NULL;
158                 }
159                 
160                 /* Map file into the memory */
161                 filep = (u1*) mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
162                 
163                 /* Create a file descriptor object */
164                 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
165                 java_handle_t* h = native_new_and_init(ci);
166
167                 if (h == NULL)
168                         return NULL;
169
170                 com_sun_cldchi_jvm_FileDescriptor fd(h, (int64_t) filep, 0, len); 
171
172                 return fd.get_handle(); 
173         }
174         else {
175                 return NULL;
176         }
177 }
178
179
180 // Native functions are exported as C functions.
181 extern "C" {
182
183 /*
184  * Class:     com/sun/cldc/io/ResourceInputStream
185  * Method:    open
186  * Signature: (Ljava/lang/String;)Ljava/lang/Object;
187  */
188 JNIEXPORT jobject JNICALL Java_com_sun_cldc_io_ResourceInputStream_open(JNIEnv *env, jclass clazz, jstring name)
189 {
190         char *filename;
191         s4 filenamelen;
192         char *path;
193         utf *uname;
194         java_handle_t* descriptor;
195
196         // Get current list of classpath entries.
197         SuckClasspath& suckclasspath = VM::get_current()->get_suckclasspath();
198
199         /* get the classname as char string (do it here for the warning at
200        the end of the function) */
201
202         uname = javastring_toutf((java_handle_t *)name, false);
203         filenamelen = utf_bytes(uname) + strlen("0");
204         filename = MNEW(char, filenamelen);
205         utf_copy(filename, uname);
206
207         /* walk through all classpath entries */
208
209         for (SuckClasspath::iterator it = suckclasspath.begin(); it != suckclasspath.end(); it++) {
210                 list_classpath_entry* lce = *it;
211
212 #if defined(ENABLE_ZLIB)
213                 if (lce->type == CLASSPATH_ARCHIVE) {
214
215                         /* enter a monitor on zip/jar archives */
216                         lce->mutex->lock();
217
218                         /* try to get the file in current archive */
219                         descriptor = zip_read_resource(lce, uname);
220
221                         /* leave the monitor */
222                         lce->mutex->unlock();
223                         
224                         if (descriptor != NULL) { /* file exists */
225                                 break;
226                         }
227
228                 } else {
229 #endif
230                         
231                         path = MNEW(char, lce->pathlen + filenamelen);
232                         strcpy(path, lce->path);
233                         strcat(path, filename);
234
235                         descriptor = file_read_resource(path);
236                         
237                         MFREE(path, char, lce->pathlen + filenamelen);
238
239                         if (descriptor != NULL) { /* file exists */
240                                 break;
241                         }
242 #if defined(ENABLE_ZLIB)
243                 }
244 #endif  
245         }
246
247         MFREE(filename, char, filenamelen);
248
249         return (jobject) descriptor;
250 }
251
252
253 /*
254  * Class:     com/sun/cldc/io/ResourceInputStream
255  * Method:    bytesRemain
256  * Signature: (Ljava/lang/Object;)I
257  */
258 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_bytesRemain(JNIEnv *env, jclass clazz, jobject jobj)
259 {
260         com_sun_cldchi_jvm_FileDescriptor fd(jobj);
261         int32_t length   = fd.get_position();
262         int32_t position = fd.get_length();
263
264         return length - position;
265 }
266
267
268 /*
269  * Class:     com/sun/cldc/io/ResourceInputStream
270  * Method:    readByte
271  * Signature: (Ljava/lang/Object;)I
272  */
273 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_readByte(JNIEnv *env, jclass clazz, jobject jobj)
274 {
275         com_sun_cldchi_jvm_FileDescriptor fd(jobj);
276
277         int64_t filep    = fd.get_pointer();
278         int32_t position = fd.get_position();
279         int32_t length   = fd.get_length();
280
281         uint8_t byte;
282
283         if (position < length) {
284                 byte = ((uint8_t*) filep)[position];
285                 position++;
286         }
287         else {
288                 return -1; /* EOF */
289         }
290
291         // Update access position.
292         fd.set_position(position);
293         
294         return (byte & 0xFF);
295 }
296
297
298 /*
299  * Class:     com/sun/cldc/io/ResourceInputStream
300  * Method:    readBytes
301  * Signature: (Ljava/lang/Object;[BII)I
302  */
303 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_readBytes(JNIEnv *env, jclass clazz, jobject jobj, jbyteArray byteArray, jint off, jint len)
304 {
305         /* get pointer to the buffer */
306         // XXX Not GC safe.
307         ByteArray ba(byteArray);
308         void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
309
310         com_sun_cldchi_jvm_FileDescriptor fd(jobj);
311
312         int64_t filep      = fd.get_pointer();
313         int32_t position   = fd.get_position();
314         int32_t fileLength = fd.get_length();
315
316         int32_t readBytes = -1;
317
318         if (position < fileLength) {
319                 int32_t available = fileLength - position;
320
321                 if (available < len) {
322                         readBytes = available;
323                 } else {
324                         readBytes = len;
325                 }
326
327                 os::memcpy(buf, ((uint8_t*) filep) + position, readBytes * sizeof(uint8_t));
328                 position += readBytes;
329         }
330         else {
331                 return -1; /* EOF */
332         }
333
334         // Update access position.
335         fd.set_position(position);
336         
337         return readBytes;
338 }
339
340
341 /*
342  * Class:     com/sun/cldc/io/ResourceInputStream
343  * Method:    clone
344  * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
345  */
346 JNIEXPORT jobject JNICALL Java_com_sun_cldc_io_ResourceInputStream_clone(JNIEnv *env, jclass clazz, jobject jobj)
347 {
348         com_sun_cldchi_jvm_FileDescriptor fd(jobj);
349
350         classinfo* c = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
351         java_handle_t* h = native_new_and_init(c);
352
353         if (h == NULL)
354                 return NULL;
355
356         com_sun_cldchi_jvm_FileDescriptor clonefd(h, fd);
357         
358         return (jobject) clonefd.get_handle();
359 }
360
361 } // extern "C"
362
363
364 /* native methods implemented by this file ************************************/
365  
366 static JNINativeMethod methods[] = {
367         { (char*) "open",        (char*) "(Ljava/lang/String;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_open        },
368         { (char*) "bytesRemain", (char*) "(Ljava/lang/Object;)I",                  (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_bytesRemain },
369         { (char*) "readByte",    (char*) "(Ljava/lang/Object;)I",                  (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_readByte    },
370         { (char*) "readBytes",   (char*) "(Ljava/lang/Object;[BII)I",              (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_readBytes   },
371         { (char*) "clone",       (char*) "(Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_clone       },
372 };
373  
374
375 /* _Jv_com_sun_cldc_io_ResourceInputStream_init ********************************
376  
377    Register native functions.
378  
379 *******************************************************************************/
380  
381 void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
382 {
383         utf* u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
384  
385         NativeMethods& nm = VM::get_current()->get_nativemethods();
386         nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
387 }
388
389
390 /*
391  * These are local overrides for various environment variables in Emacs.
392  * Please do not remove this and leave it at the end of the file, where
393  * Emacs will automagically detect them.
394  * ---------------------------------------------------------------------
395  * Local variables:
396  * mode: c++
397  * indent-tabs-mode: t
398  * c-basic-offset: 4
399  * tab-width: 4
400  * End:
401  * vim:noexpandtab:sw=4:ts=4:
402  */