1 /* src/native/vm/cldc1.1/com_sun_cldc_io_ResourceInputStream.c
3 Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: java_lang_VMRuntime.c 5900 2006-11-04 17:30:44Z michi $
37 #include "mm/memory.h"
39 #include "native/jni.h"
40 #include "native/llni.h"
41 #include "native/native.h"
43 #include "native/include/java_lang_Object.h"
44 #include "native/include/java_lang_String.h"
45 #include "native/include/com_sun_cldc_io_ResourceInputStream.h"
46 #include "native/include/com_sun_cldchi_jvm_FileDescriptor.h"
49 #include "vm/builtin.h"
50 #include "vm/vm.h" /* REMOVE ME: temporarily */
51 #include "vm/exceptions.h"
52 #include "vm/stringlocal.h"
54 #include "vmcore/zip.h"
56 #include "threads/lock-common.h"
58 /* native methods implemented by this file ************************************/
60 static JNINativeMethod methods[] = {
61 { "open", "(Ljava/lang/String;)Ljava/lang/Object;", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_open },
62 { "bytesRemain", "(Ljava/lang/Object;)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_bytesRemain },
63 { "readByte", "(Ljava/lang/Object;)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_readByte },
64 { "readBytes", "(Ljava/lang/Object;[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_readBytes },
65 { "clone", "(Ljava/lang/Object;)Ljava/lang/Object;", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_clone },
68 /* _Jv_com_sun_cldc_io_ResourceInputStream_init ********************************
70 Register native functions.
72 *******************************************************************************/
74 void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
78 u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
80 native_method_register(u, methods, NATIVE_METHODS_COUNT);
83 static struct com_sun_cldchi_jvm_FileDescriptor* zip_read_resource(list_classpath_entry *lce, utf *name)
85 hashtable_zipfile_entry *htzfe;
93 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor = NULL;
95 /* try to find the class in the current archive */
97 htzfe = zip_find(lce, name);
102 /* read stuff from local file header */
104 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
105 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
107 indata = htzfe->data +
110 lfh.extrafieldlength;
112 /* allocate buffer for uncompressed data */
114 outdata = MNEW(u1, htzfe->uncompressedsize);
116 /* how is the file stored? */
118 switch (htzfe->compressionmethod) {
120 /* fill z_stream structure */
123 zs.avail_in = htzfe->compressedsize;
124 zs.next_out = outdata;
125 zs.avail_out = htzfe->uncompressedsize;
131 /* initialize this inflate run */
133 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
134 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
136 /* decompress the file into buffer */
138 err = inflate(&zs, Z_SYNC_FLUSH);
140 if ((err != Z_STREAM_END) && (err != Z_OK))
141 vm_abort("zip_get: inflate failed: %s", strerror(errno));
143 /* finish this inflate run */
145 if (inflateEnd(&zs) != Z_OK)
146 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
150 /* uncompressed file, just copy the data */
151 MCOPY(outdata, indata, u1, htzfe->compressedsize);
155 vm_abort("zip_get: unknown compression method %d",
156 htzfe->compressionmethod);
159 /* Create a file descriptor object */
160 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
161 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
162 LLNI_field_set_val(fileDescriptor, pointer, (int)outdata);
163 LLNI_field_set_val(fileDescriptor, length, htzfe->uncompressedsize);
164 LLNI_field_set_val(fileDescriptor, position, 0);
165 return fileDescriptor;
169 static struct com_sun_cldchi_jvm_FileDescriptor* file_read_resource(char *path)
172 struct stat statBuffer;
174 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor = NULL;
178 fd = open(path, O_RDONLY);
182 if (fstat(fd, &statBuffer) != -1) {
183 len = statBuffer.st_size;
188 /* Map file into the memory */
189 filep = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
191 /* Create a file descriptor object */
192 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
193 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
194 LLNI_field_set_val(fileDescriptor, pointer, (int)filep);
195 LLNI_field_set_val(fileDescriptor, length, len);
196 LLNI_field_set_val(fileDescriptor, position, 0);
198 return fileDescriptor;
208 * Class: com/sun/cldc/io/ResourceInputStream
210 * Signature: (Ljava/lang/String;)Ljava/lang/Object;
212 JNIEXPORT struct java_lang_Object* JNICALL Java_com_sun_cldc_io_ResourceInputStream_open(JNIEnv *env, jclass clazz, java_lang_String *name)
215 list_classpath_entry *lce;
220 com_sun_cldchi_jvm_FileDescriptor* descriptor;
222 /* get the classname as char string (do it here for the warning at
223 the end of the function) */
225 uname = javastring_toutf((java_handle_t *)name, false);
226 filenamelen = utf_bytes(uname) + strlen("0");
227 filename = MNEW(char, filenamelen);
228 utf_copy(filename, uname);
230 /* walk through all classpath entries */
232 for (lce = list_first(list_classpath_entries); lce != NULL;
233 lce = list_next(list_classpath_entries, lce)) {
235 #if defined(ENABLE_ZLIB)
236 if (lce->type == CLASSPATH_ARCHIVE) {
238 /* enter a monitor on zip/jar archives */
239 LOCK_MONITOR_ENTER(lce);
241 /* try to get the file in current archive */
242 descriptor = zip_read_resource(lce, uname);
244 /* leave the monitor */
245 LOCK_MONITOR_EXIT(lce);
247 if (descriptor != NULL) { /* file exists */
254 path = MNEW(char, lce->pathlen + filenamelen);
255 strcpy(path, lce->path);
256 strcat(path, filename);
258 descriptor = file_read_resource(path);
260 MFREE(path, char, lce->pathlen + filenamelen);
262 if (descriptor != NULL) { /* file exists */
266 #if defined(ENABLE_ZLIB)
272 MFREE(filename, char, filenamelen);
274 return (java_lang_Object*) descriptor;
280 * Class: com_sun_cldc_io_ResourceInputStream
281 * Method: bytesRemain
282 * Signature: (Ljava/lang/Object;)I
284 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_bytesRemain(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
286 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
290 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
291 LLNI_field_get_val(fileDescriptor, position, position);
292 LLNI_field_get_val(fileDescriptor, length, length);
294 return length - position;
299 * Class: com_sun_cldc_io_ResourceInputStream
301 * Signature: (Ljava/lang/Object;)I
303 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_readByte(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
305 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
311 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
312 LLNI_field_get_val(fileDescriptor, position, position);
313 LLNI_field_get_val(fileDescriptor, length, length);
314 LLNI_field_get_val(fileDescriptor, pointer, filep);
316 if (position < length) {
317 byte = ((u1*)(int)filep)[position];
323 /* Update access position */
324 LLNI_field_set_val(fileDescriptor, position, position);
326 return (byte & 0xFF);
331 * Class: com_sun_cldc_io_ResourceInputStream
333 * Signature: (Ljava/lang/Object;[BII)I
335 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) {
337 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
345 /* get pointer to the buffer */
346 buf = &(LLNI_array_direct(byteArray, off));
348 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
349 LLNI_field_get_val(fileDescriptor, position, position);
350 LLNI_field_get_val(fileDescriptor, length, fileLength);
351 LLNI_field_get_val(fileDescriptor, pointer, filep);
353 if (position < fileLength) {
354 available = fileLength - position;
355 if (available < len) {
356 readBytes = available;
360 memcpy(buf, ((u1*)(int)filep) + position, readBytes * sizeof(u1));
361 position += readBytes;
366 /* Update access position */
367 LLNI_field_set_val(fileDescriptor, position, position);
373 * Class: com_sun_cldc_io_ResourceInputStream
375 * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
377 JNIEXPORT struct java_lang_Object* JNICALL Java_com_sun_cldc_io_ResourceInputStream_clone(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
380 com_sun_cldchi_jvm_FileDescriptor *srcFileDescriptor;
381 com_sun_cldchi_jvm_FileDescriptor *dstFileDescriptor;
384 int64_t srcFilePointer;
386 srcFileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
387 LLNI_field_get_val(srcFileDescriptor, position, srcPosition);
388 LLNI_field_get_val(srcFileDescriptor, length, srcLength);
389 LLNI_field_get_val(srcFileDescriptor, pointer, srcFilePointer);
391 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
392 dstFileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
393 LLNI_field_set_val(dstFileDescriptor, position, srcPosition);
394 LLNI_field_set_val(dstFileDescriptor, length, srcLength);
395 LLNI_field_set_val(dstFileDescriptor, pointer, srcFilePointer);
397 return (java_lang_Object*) dstFileDescriptor;
403 * These are local overrides for various environment variables in Emacs.
404 * Please do not remove this and leave it at the end of the file, where
405 * Emacs will automagically detect them.
406 * ---------------------------------------------------------------------
409 * indent-tabs-mode: t
413 * vim:noexpandtab:sw=4:ts=4: