1 /* src/native/vm/cldc1.1/com_sun_cldc_io_ResourceInputStream.cpp
3 Copyright (C) 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
34 #include "mm/memory.h"
36 #include "native/jni.h"
37 #include "native/llni.h"
38 #include "native/native.h"
40 #include "native/include/java_lang_Object.h"
41 #include "native/include/java_lang_String.h"
42 #include "native/include/com_sun_cldchi_jvm_FileDescriptor.h"
46 #include "native/include/com_sun_cldc_io_ResourceInputStream.h"
50 #include "vm/builtin.h"
51 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
52 #include "vm/exceptions.hpp"
53 #include "vm/string.hpp"
55 #include "vmcore/zip.h"
57 #include "threads/lock-common.h"
60 /* native methods implemented by this file ************************************/
62 static JNINativeMethod methods[] = {
63 { (char*) "open", (char*) "(Ljava/lang/String;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_open },
64 { (char*) "bytesRemain", (char*) "(Ljava/lang/Object;)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_bytesRemain },
65 { (char*) "readByte", (char*) "(Ljava/lang/Object;)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_readByte },
66 { (char*) "readBytes", (char*) "(Ljava/lang/Object;[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_readBytes },
67 { (char*) "clone", (char*) "(Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_clone },
71 /* _Jv_com_sun_cldc_io_ResourceInputStream_init ********************************
73 Register native functions.
75 *******************************************************************************/
79 void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
83 u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
85 native_method_register(u, methods, NATIVE_METHODS_COUNT);
89 static struct com_sun_cldchi_jvm_FileDescriptor* zip_read_resource(list_classpath_entry *lce, utf *name)
91 hashtable_zipfile_entry *htzfe;
99 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor = NULL;
101 /* try to find the class in the current archive */
103 htzfe = zip_find(lce, name);
108 /* read stuff from local file header */
110 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
111 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
113 indata = htzfe->data +
116 lfh.extrafieldlength;
118 /* allocate buffer for uncompressed data */
120 outdata = MNEW(u1, htzfe->uncompressedsize);
122 /* how is the file stored? */
124 switch (htzfe->compressionmethod) {
126 /* fill z_stream structure */
129 zs.avail_in = htzfe->compressedsize;
130 zs.next_out = outdata;
131 zs.avail_out = htzfe->uncompressedsize;
137 /* initialize this inflate run */
139 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
140 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
142 /* decompress the file into buffer */
144 err = inflate(&zs, Z_SYNC_FLUSH);
146 if ((err != Z_STREAM_END) && (err != Z_OK))
147 vm_abort("zip_get: inflate failed: %s", strerror(errno));
149 /* finish this inflate run */
151 if (inflateEnd(&zs) != Z_OK)
152 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
156 /* uncompressed file, just copy the data */
157 MCOPY(outdata, indata, u1, htzfe->compressedsize);
161 vm_abort("zip_get: unknown compression method %d",
162 htzfe->compressionmethod);
165 /* Create a file descriptor object */
166 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
167 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
168 LLNI_field_set_val(fileDescriptor, pointer, (int64_t)outdata);
169 LLNI_field_set_val(fileDescriptor, length, htzfe->uncompressedsize);
170 LLNI_field_set_val(fileDescriptor, position, 0);
171 return fileDescriptor;
175 static struct com_sun_cldchi_jvm_FileDescriptor* file_read_resource(char *path)
178 struct stat statBuffer;
180 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor = NULL;
184 fd = open(path, O_RDONLY);
188 if (fstat(fd, &statBuffer) != -1) {
189 len = statBuffer.st_size;
194 /* Map file into the memory */
195 filep = (u1*) mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
197 /* Create a file descriptor object */
198 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
199 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
200 LLNI_field_set_val(fileDescriptor, pointer, (int64_t)filep);
201 LLNI_field_set_val(fileDescriptor, length, len);
202 LLNI_field_set_val(fileDescriptor, position, 0);
204 return fileDescriptor;
213 // Native functions are exported as C functions.
217 * Class: com/sun/cldc/io/ResourceInputStream
219 * Signature: (Ljava/lang/String;)Ljava/lang/Object;
221 JNIEXPORT struct java_lang_Object* JNICALL Java_com_sun_cldc_io_ResourceInputStream_open(JNIEnv *env, jclass clazz, java_lang_String *name)
224 list_classpath_entry *lce;
229 com_sun_cldchi_jvm_FileDescriptor* descriptor;
231 /* get the classname as char string (do it here for the warning at
232 the end of the function) */
234 uname = javastring_toutf((java_handle_t *)name, false);
235 filenamelen = utf_bytes(uname) + strlen("0");
236 filename = MNEW(char, filenamelen);
237 utf_copy(filename, uname);
239 /* walk through all classpath entries */
241 for (lce = (list_classpath_entry*) list_first(list_classpath_entries); lce != NULL;
242 lce = (list_classpath_entry*) list_next(list_classpath_entries, lce)) {
244 #if defined(ENABLE_ZLIB)
245 if (lce->type == CLASSPATH_ARCHIVE) {
247 /* enter a monitor on zip/jar archives */
248 LOCK_MONITOR_ENTER(lce);
250 /* try to get the file in current archive */
251 descriptor = zip_read_resource(lce, uname);
253 /* leave the monitor */
254 LOCK_MONITOR_EXIT(lce);
256 if (descriptor != NULL) { /* file exists */
263 path = MNEW(char, lce->pathlen + filenamelen);
264 strcpy(path, lce->path);
265 strcat(path, filename);
267 descriptor = file_read_resource(path);
269 MFREE(path, char, lce->pathlen + filenamelen);
271 if (descriptor != NULL) { /* file exists */
275 #if defined(ENABLE_ZLIB)
281 MFREE(filename, char, filenamelen);
283 return (java_lang_Object*) descriptor;
289 * Class: com_sun_cldc_io_ResourceInputStream
290 * Method: bytesRemain
291 * Signature: (Ljava/lang/Object;)I
293 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_bytesRemain(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
295 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
299 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
300 LLNI_field_get_val(fileDescriptor, position, position);
301 LLNI_field_get_val(fileDescriptor, length, length);
303 return length - position;
308 * Class: com_sun_cldc_io_ResourceInputStream
310 * Signature: (Ljava/lang/Object;)I
312 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_readByte(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
314 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
320 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
321 LLNI_field_get_val(fileDescriptor, position, position);
322 LLNI_field_get_val(fileDescriptor, length, length);
323 LLNI_field_get_val(fileDescriptor, pointer, filep);
325 if (position < length) {
326 byte = ((u1*)(int)filep)[position];
332 /* Update access position */
333 LLNI_field_set_val(fileDescriptor, position, position);
335 return (byte & 0xFF);
340 * Class: com_sun_cldc_io_ResourceInputStream
342 * Signature: (Ljava/lang/Object;[BII)I
344 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_readBytes(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj, java_handle_bytearray_t* byteArray, s4 off, s4 len) {
346 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
354 /* get pointer to the buffer */
355 buf = &(LLNI_array_direct(byteArray, off));
357 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
358 LLNI_field_get_val(fileDescriptor, position, position);
359 LLNI_field_get_val(fileDescriptor, length, fileLength);
360 LLNI_field_get_val(fileDescriptor, pointer, filep);
362 if (position < fileLength) {
363 available = fileLength - position;
364 if (available < len) {
365 readBytes = available;
369 memcpy(buf, ((u1*)(int)filep) + position, readBytes * sizeof(u1));
370 position += readBytes;
375 /* Update access position */
376 LLNI_field_set_val(fileDescriptor, position, position);
382 * Class: com_sun_cldc_io_ResourceInputStream
384 * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
386 JNIEXPORT struct java_lang_Object* JNICALL Java_com_sun_cldc_io_ResourceInputStream_clone(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
389 com_sun_cldchi_jvm_FileDescriptor *srcFileDescriptor;
390 com_sun_cldchi_jvm_FileDescriptor *dstFileDescriptor;
393 int64_t srcFilePointer;
395 srcFileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
396 LLNI_field_get_val(srcFileDescriptor, position, srcPosition);
397 LLNI_field_get_val(srcFileDescriptor, length, srcLength);
398 LLNI_field_get_val(srcFileDescriptor, pointer, srcFilePointer);
400 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
401 dstFileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
402 LLNI_field_set_val(dstFileDescriptor, position, srcPosition);
403 LLNI_field_set_val(dstFileDescriptor, length, srcLength);
404 LLNI_field_set_val(dstFileDescriptor, pointer, srcFilePointer);
406 return (java_lang_Object*) dstFileDescriptor;
414 * These are local overrides for various environment variables in Emacs.
415 * Please do not remove this and leave it at the end of the file, where
416 * Emacs will automagically detect them.
417 * ---------------------------------------------------------------------
420 * indent-tabs-mode: t
424 * vim:noexpandtab:sw=4:ts=4: