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
35 #include "mm/memory.h"
37 #include "native/jni.h"
38 #include "native/llni.h"
39 #include "native/native.h"
41 #include "native/include/java_lang_Object.h"
42 #include "native/include/java_lang_String.h"
43 #include "native/include/com_sun_cldc_io_ResourceInputStream.h"
44 #include "native/include/com_sun_cldchi_jvm_FileDescriptor.h"
47 #include "vm/builtin.h"
48 #include "vm/vm.h" /* REMOVE ME: temporarily */
49 #include "vm/exceptions.h"
50 #include "vm/stringlocal.h"
52 #include "vmcore/zip.h"
54 #include "threads/lock-common.h"
56 /* native methods implemented by this file ************************************/
58 static JNINativeMethod methods[] = {
59 { "open", "(Ljava/lang/String;)Ljava/lang/Object;", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_open },
60 { "bytesRemain", "(Ljava/lang/Object;)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_bytesRemain },
61 { "readByte", "(Ljava/lang/Object;)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_readByte },
62 { "readBytes", "(Ljava/lang/Object;[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_readBytes },
63 { "clone", "(Ljava/lang/Object;)Ljava/lang/Object;", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_clone },
66 /* _Jv_com_sun_cldc_io_ResourceInputStream_init ********************************
68 Register native functions.
70 *******************************************************************************/
72 void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
76 u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
78 native_method_register(u, methods, NATIVE_METHODS_COUNT);
81 static struct com_sun_cldchi_jvm_FileDescriptor* zip_read_resource(list_classpath_entry *lce, utf *name)
83 hashtable_zipfile_entry *htzfe;
91 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor = NULL;
93 /* try to find the class in the current archive */
95 htzfe = zip_find(lce, name);
100 /* read stuff from local file header */
102 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
103 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
105 indata = htzfe->data +
108 lfh.extrafieldlength;
110 /* allocate buffer for uncompressed data */
112 outdata = MNEW(u1, htzfe->uncompressedsize);
114 /* how is the file stored? */
116 switch (htzfe->compressionmethod) {
118 /* fill z_stream structure */
121 zs.avail_in = htzfe->compressedsize;
122 zs.next_out = outdata;
123 zs.avail_out = htzfe->uncompressedsize;
129 /* initialize this inflate run */
131 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
132 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
134 /* decompress the file into buffer */
136 err = inflate(&zs, Z_SYNC_FLUSH);
138 if ((err != Z_STREAM_END) && (err != Z_OK))
139 vm_abort("zip_get: inflate failed: %s", strerror(errno));
141 /* finish this inflate run */
143 if (inflateEnd(&zs) != Z_OK)
144 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
148 /* uncompressed file, just copy the data */
149 MCOPY(outdata, indata, u1, htzfe->compressedsize);
153 vm_abort("zip_get: unknown compression method %d",
154 htzfe->compressionmethod);
157 /* Create a file descriptor object */
158 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
159 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
160 LLNI_field_set_val(fileDescriptor, pointer, (int)outdata);
161 LLNI_field_set_val(fileDescriptor, length, htzfe->uncompressedsize);
162 LLNI_field_set_val(fileDescriptor, position, 0);
163 return fileDescriptor;
167 static struct com_sun_cldchi_jvm_FileDescriptor* file_read_resource(char *path)
170 struct stat statBuffer;
172 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor = NULL;
176 fd = open(path, O_RDONLY);
180 if (fstat(fd, &statBuffer) != -1) {
181 len = statBuffer.st_size;
186 /* Map file into the memory */
187 filep = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
189 /* Create a file descriptor object */
190 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
191 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
192 LLNI_field_set_val(fileDescriptor, pointer, (int)filep);
193 LLNI_field_set_val(fileDescriptor, length, len);
194 LLNI_field_set_val(fileDescriptor, position, 0);
196 return fileDescriptor;
206 * Class: com/sun/cldc/io/ResourceInputStream
208 * Signature: (Ljava/lang/String;)Ljava/lang/Object;
210 JNIEXPORT struct java_lang_Object* JNICALL Java_com_sun_cldc_io_ResourceInputStream_open(JNIEnv *env, jclass clazz, java_lang_String *name)
213 list_classpath_entry *lce;
218 com_sun_cldchi_jvm_FileDescriptor* descriptor;
220 /* get the classname as char string (do it here for the warning at
221 the end of the function) */
223 uname = javastring_toutf((java_handle_t *)name, false);
224 filenamelen = utf_bytes(uname) + strlen("0");
225 filename = MNEW(char, filenamelen);
226 utf_copy(filename, uname);
228 /* walk through all classpath entries */
230 for (lce = list_first(list_classpath_entries); lce != NULL;
231 lce = list_next(list_classpath_entries, lce)) {
233 #if defined(ENABLE_ZLIB)
234 if (lce->type == CLASSPATH_ARCHIVE) {
236 /* enter a monitor on zip/jar archives */
237 LOCK_MONITOR_ENTER(lce);
239 /* try to get the file in current archive */
240 descriptor = zip_read_resource(lce, uname);
242 /* leave the monitor */
243 LOCK_MONITOR_EXIT(lce);
245 if (descriptor != NULL) { /* file exists */
252 path = MNEW(char, lce->pathlen + filenamelen);
253 strcpy(path, lce->path);
254 strcat(path, filename);
256 descriptor = file_read_resource(path);
258 MFREE(path, char, lce->pathlen + filenamelen);
260 if (descriptor != NULL) { /* file exists */
264 #if defined(ENABLE_ZLIB)
270 MFREE(filename, char, filenamelen);
272 return (java_lang_Object*) descriptor;
278 * Class: com_sun_cldc_io_ResourceInputStream
279 * Method: bytesRemain
280 * Signature: (Ljava/lang/Object;)I
282 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_bytesRemain(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
284 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
288 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
289 LLNI_field_get_val(fileDescriptor, position, position);
290 LLNI_field_get_val(fileDescriptor, length, length);
292 return length - position;
297 * Class: com_sun_cldc_io_ResourceInputStream
299 * Signature: (Ljava/lang/Object;)I
301 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_readByte(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
303 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
309 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
310 LLNI_field_get_val(fileDescriptor, position, position);
311 LLNI_field_get_val(fileDescriptor, length, length);
312 LLNI_field_get_val(fileDescriptor, pointer, filep);
314 if (position < length) {
315 byte = ((u1*)(int)filep)[position];
321 /* Update access position */
322 LLNI_field_set_val(fileDescriptor, position, position);
324 return (byte & 0xFF);
329 * Class: com_sun_cldc_io_ResourceInputStream
331 * Signature: (Ljava/lang/Object;[BII)I
333 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) {
335 com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
343 /* get pointer to the buffer */
344 buf = &(LLNI_array_direct(byteArray, off));
346 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
347 LLNI_field_get_val(fileDescriptor, position, position);
348 LLNI_field_get_val(fileDescriptor, length, fileLength);
349 LLNI_field_get_val(fileDescriptor, pointer, filep);
351 if (position < fileLength) {
352 available = fileLength - position;
353 if (available < len) {
354 readBytes = available;
358 memcpy(buf, ((u1*)(int)filep) + position, readBytes * sizeof(u1));
359 position += readBytes;
364 /* Update access position */
365 LLNI_field_set_val(fileDescriptor, position, position);
371 * Class: com_sun_cldc_io_ResourceInputStream
373 * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
375 JNIEXPORT struct java_lang_Object* JNICALL Java_com_sun_cldc_io_ResourceInputStream_clone(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
378 com_sun_cldchi_jvm_FileDescriptor *srcFileDescriptor;
379 com_sun_cldchi_jvm_FileDescriptor *dstFileDescriptor;
382 int64_t srcFilePointer;
384 srcFileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
385 LLNI_field_get_val(srcFileDescriptor, position, srcPosition);
386 LLNI_field_get_val(srcFileDescriptor, length, srcLength);
387 LLNI_field_get_val(srcFileDescriptor, pointer, srcFilePointer);
389 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
390 dstFileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
391 LLNI_field_set_val(dstFileDescriptor, position, srcPosition);
392 LLNI_field_set_val(dstFileDescriptor, length, srcLength);
393 LLNI_field_set_val(dstFileDescriptor, pointer, srcFilePointer);
395 return (java_lang_Object*) dstFileDescriptor;
401 * These are local overrides for various environment variables in Emacs.
402 * Please do not remove this and leave it at the end of the file, where
403 * Emacs will automagically detect them.
404 * ---------------------------------------------------------------------
407 * indent-tabs-mode: t
411 * vim:noexpandtab:sw=4:ts=4: