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.hpp"
36 #include "native/jni.hpp"
37 #include "native/llni.h"
38 #include "native/native.hpp"
40 #if defined(ENABLE_JNI_HEADERS)
41 # include "native/include/com_sun_cldc_io_ResourceInputStream.h"
44 #include "threads/mutex.hpp"
46 #include "vm/jit/builtin.hpp"
47 #include "vm/exceptions.hpp"
48 #include "vm/javaobjects.hpp"
49 #include "vm/string.hpp"
51 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
55 static java_handle_t* zip_read_resource(list_classpath_entry *lce, utf *name)
57 hashtable_zipfile_entry *htzfe;
66 /* try to find the class in the current archive */
68 htzfe = zip_find(lce, name);
73 /* read stuff from local file header */
75 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
76 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
78 indata = htzfe->data +
83 /* allocate buffer for uncompressed data */
85 outdata = MNEW(u1, htzfe->uncompressedsize);
87 /* how is the file stored? */
89 switch (htzfe->compressionmethod) {
91 /* fill z_stream structure */
94 zs.avail_in = htzfe->compressedsize;
95 zs.next_out = outdata;
96 zs.avail_out = htzfe->uncompressedsize;
102 /* initialize this inflate run */
104 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
105 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
107 /* decompress the file into buffer */
109 err = inflate(&zs, Z_SYNC_FLUSH);
111 if ((err != Z_STREAM_END) && (err != Z_OK))
112 vm_abort("zip_get: inflate failed: %s", strerror(errno));
114 /* finish this inflate run */
116 if (inflateEnd(&zs) != Z_OK)
117 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
121 /* uncompressed file, just copy the data */
122 MCOPY(outdata, indata, u1, htzfe->compressedsize);
126 vm_abort("zip_get: unknown compression method %d",
127 htzfe->compressionmethod);
130 // Create a file descriptor object.
131 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
132 java_handle_t* h = native_new_and_init(ci);
137 com_sun_cldchi_jvm_FileDescriptor fd(h, (int64_t) outdata, 0, htzfe->uncompressedsize);
139 return fd.get_handle();
143 static java_handle_t* file_read_resource(char *path)
146 struct stat statBuffer;
151 fd = open(path, O_RDONLY);
155 if (fstat(fd, &statBuffer) != -1) {
156 len = statBuffer.st_size;
161 /* Map file into the memory */
162 filep = (u1*) mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
164 /* Create a file descriptor object */
165 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
166 java_handle_t* h = native_new_and_init(ci);
171 com_sun_cldchi_jvm_FileDescriptor fd(h, (int64_t) filep, 0, len);
173 return fd.get_handle();
181 // Native functions are exported as C functions.
185 * Class: com/sun/cldc/io/ResourceInputStream
187 * Signature: (Ljava/lang/String;)Ljava/lang/Object;
189 JNIEXPORT jobject JNICALL Java_com_sun_cldc_io_ResourceInputStream_open(JNIEnv *env, jclass clazz, jstring name)
195 java_handle_t* descriptor;
197 // Get current list of classpath entries.
198 SuckClasspath& suckclasspath = VM::get_current()->get_suckclasspath();
200 /* get the classname as char string (do it here for the warning at
201 the end of the function) */
203 uname = javastring_toutf((java_handle_t *)name, false);
204 filenamelen = utf_bytes(uname) + strlen("0");
205 filename = MNEW(char, filenamelen);
206 utf_copy(filename, uname);
208 /* walk through all classpath entries */
210 for (SuckClasspath::iterator it = suckclasspath.begin(); it != suckclasspath.end(); it++) {
211 list_classpath_entry* lce = *it;
213 #if defined(ENABLE_ZLIB)
214 if (lce->type == CLASSPATH_ARCHIVE) {
216 /* enter a monitor on zip/jar archives */
219 /* try to get the file in current archive */
220 descriptor = zip_read_resource(lce, uname);
222 /* leave the monitor */
223 lce->mutex->unlock();
225 if (descriptor != NULL) { /* file exists */
232 path = MNEW(char, lce->pathlen + filenamelen);
233 strcpy(path, lce->path);
234 strcat(path, filename);
236 descriptor = file_read_resource(path);
238 MFREE(path, char, lce->pathlen + filenamelen);
240 if (descriptor != NULL) { /* file exists */
243 #if defined(ENABLE_ZLIB)
248 MFREE(filename, char, filenamelen);
250 return (jobject) descriptor;
255 * Class: com/sun/cldc/io/ResourceInputStream
256 * Method: bytesRemain
257 * Signature: (Ljava/lang/Object;)I
259 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_bytesRemain(JNIEnv *env, jclass clazz, jobject jobj)
261 com_sun_cldchi_jvm_FileDescriptor fd(jobj);
262 int32_t length = fd.get_position();
263 int32_t position = fd.get_length();
265 return length - position;
270 * Class: com/sun/cldc/io/ResourceInputStream
272 * Signature: (Ljava/lang/Object;)I
274 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_readByte(JNIEnv *env, jclass clazz, jobject jobj)
276 com_sun_cldchi_jvm_FileDescriptor fd(jobj);
278 int64_t filep = fd.get_pointer();
279 int32_t position = fd.get_position();
280 int32_t length = fd.get_length();
284 if (position < length) {
285 byte = ((uint8_t*) filep)[position];
292 // Update access position.
293 fd.set_position(position);
295 return (byte & 0xFF);
300 * Class: com/sun/cldc/io/ResourceInputStream
302 * Signature: (Ljava/lang/Object;[BII)I
304 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_readBytes(JNIEnv *env, jclass clazz, jobject jobj, jbyteArray byteArray, jint off, jint len)
306 /* get pointer to the buffer */
308 void* buf = &(LLNI_array_direct((java_handle_bytearray_t*) byteArray, off));
310 com_sun_cldchi_jvm_FileDescriptor fd(jobj);
312 int64_t filep = fd.get_pointer();
313 int32_t position = fd.get_position();
314 int32_t fileLength = fd.get_length();
316 int32_t readBytes = -1;
318 if (position < fileLength) {
319 int32_t available = fileLength - position;
321 if (available < len) {
322 readBytes = available;
327 os::memcpy(buf, ((uint8_t*) filep) + position, readBytes * sizeof(uint8_t));
328 position += readBytes;
334 // Update access position.
335 fd.set_position(position);
342 * Class: com/sun/cldc/io/ResourceInputStream
344 * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
346 JNIEXPORT jobject JNICALL Java_com_sun_cldc_io_ResourceInputStream_clone(JNIEnv *env, jclass clazz, jobject jobj)
348 com_sun_cldchi_jvm_FileDescriptor fd(jobj);
350 classinfo* c = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
351 java_handle_t* h = native_new_and_init(c);
356 com_sun_cldchi_jvm_FileDescriptor clonefd(h, fd);
358 return (jobject) clonefd.get_handle();
364 /* native methods implemented by this file ************************************/
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 },
375 /* _Jv_com_sun_cldc_io_ResourceInputStream_init ********************************
377 Register native functions.
379 *******************************************************************************/
381 void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
383 utf* u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
385 NativeMethods& nm = VM::get_current()->get_nativemethods();
386 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
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 * ---------------------------------------------------------------------
397 * indent-tabs-mode: t
401 * vim:noexpandtab:sw=4:ts=4: