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/native.hpp"
39 #if defined(ENABLE_JNI_HEADERS)
40 # include "native/include/com_sun_cldc_io_ResourceInputStream.h"
43 #include "threads/mutex.hpp"
45 #include "vm/jit/builtin.hpp"
46 #include "vm/exceptions.hpp"
47 #include "vm/javaobjects.hpp"
48 #include "vm/string.hpp"
50 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
54 static java_handle_t* zip_read_resource(list_classpath_entry *lce, utf *name)
56 hashtable_zipfile_entry *htzfe;
65 /* try to find the class in the current archive */
67 htzfe = zip_find(lce, name);
72 /* read stuff from local file header */
74 lfh.filenamelength = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
75 lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
77 indata = htzfe->data +
82 /* allocate buffer for uncompressed data */
84 outdata = MNEW(u1, htzfe->uncompressedsize);
86 /* how is the file stored? */
88 switch (htzfe->compressionmethod) {
90 /* fill z_stream structure */
93 zs.avail_in = htzfe->compressedsize;
94 zs.next_out = outdata;
95 zs.avail_out = htzfe->uncompressedsize;
101 /* initialize this inflate run */
103 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
104 vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
106 /* decompress the file into buffer */
108 err = inflate(&zs, Z_SYNC_FLUSH);
110 if ((err != Z_STREAM_END) && (err != Z_OK))
111 vm_abort("zip_get: inflate failed: %s", strerror(errno));
113 /* finish this inflate run */
115 if (inflateEnd(&zs) != Z_OK)
116 vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
120 /* uncompressed file, just copy the data */
121 MCOPY(outdata, indata, u1, htzfe->compressedsize);
125 vm_abort("zip_get: unknown compression method %d",
126 htzfe->compressionmethod);
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);
136 com_sun_cldchi_jvm_FileDescriptor fd(h, (int64_t) outdata, 0, htzfe->uncompressedsize);
138 return fd.get_handle();
142 static java_handle_t* file_read_resource(char *path)
145 struct stat statBuffer;
150 fd = open(path, O_RDONLY);
154 if (fstat(fd, &statBuffer) != -1) {
155 len = statBuffer.st_size;
160 /* Map file into the memory */
161 filep = (u1*) mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
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);
170 com_sun_cldchi_jvm_FileDescriptor fd(h, (int64_t) filep, 0, len);
172 return fd.get_handle();
180 // Native functions are exported as C functions.
184 * Class: com/sun/cldc/io/ResourceInputStream
186 * Signature: (Ljava/lang/String;)Ljava/lang/Object;
188 JNIEXPORT jobject JNICALL Java_com_sun_cldc_io_ResourceInputStream_open(JNIEnv *env, jclass clazz, jstring name)
194 java_handle_t* descriptor;
196 /* get the classname as char string (do it here for the warning at
197 the end of the function) */
199 uname = javastring_toutf((java_handle_t *)name, false);
200 filenamelen = utf_bytes(uname) + strlen("0");
201 filename = MNEW(char, filenamelen);
202 utf_copy(filename, uname);
204 /* walk through all classpath entries */
206 for (List<list_classpath_entry*>::iterator it = list_classpath_entries->begin(); it != list_classpath_entries->end(); it++) {
207 list_classpath_entry* lce = *it;
209 #if defined(ENABLE_ZLIB)
210 if (lce->type == CLASSPATH_ARCHIVE) {
212 /* enter a monitor on zip/jar archives */
215 /* try to get the file in current archive */
216 descriptor = zip_read_resource(lce, uname);
218 /* leave the monitor */
219 lce->mutex->unlock();
221 if (descriptor != NULL) { /* file exists */
228 path = MNEW(char, lce->pathlen + filenamelen);
229 strcpy(path, lce->path);
230 strcat(path, filename);
232 descriptor = file_read_resource(path);
234 MFREE(path, char, lce->pathlen + filenamelen);
236 if (descriptor != NULL) { /* file exists */
239 #if defined(ENABLE_ZLIB)
244 MFREE(filename, char, filenamelen);
246 return (jobject) descriptor;
251 * Class: com/sun/cldc/io/ResourceInputStream
252 * Method: bytesRemain
253 * Signature: (Ljava/lang/Object;)I
255 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_bytesRemain(JNIEnv *env, jclass clazz, jobject jobj)
257 com_sun_cldchi_jvm_FileDescriptor fd(jobj);
258 int32_t length = fd.get_position();
259 int32_t position = fd.get_length();
261 return length - position;
266 * Class: com/sun/cldc/io/ResourceInputStream
268 * Signature: (Ljava/lang/Object;)I
270 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_readByte(JNIEnv *env, jclass clazz, jobject jobj)
272 com_sun_cldchi_jvm_FileDescriptor fd(jobj);
274 int64_t filep = fd.get_pointer();
275 int32_t position = fd.get_position();
276 int32_t length = fd.get_length();
280 if (position < length) {
281 byte = ((uint8_t*) filep)[position];
288 // Update access position.
289 fd.set_position(position);
291 return (byte & 0xFF);
296 * Class: com/sun/cldc/io/ResourceInputStream
298 * Signature: (Ljava/lang/Object;[BII)I
300 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_readBytes(JNIEnv *env, jclass clazz, jobject jobj, jbyteArray byteArray, jint off, jint len)
302 /* get pointer to the buffer */
304 ByteArray ba(byteArray);
305 void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
307 com_sun_cldchi_jvm_FileDescriptor fd(jobj);
309 int64_t filep = fd.get_pointer();
310 int32_t position = fd.get_position();
311 int32_t fileLength = fd.get_length();
313 int32_t readBytes = -1;
315 if (position < fileLength) {
316 int32_t available = fileLength - position;
318 if (available < len) {
319 readBytes = available;
324 os::memcpy(buf, ((uint8_t*) filep) + position, readBytes * sizeof(uint8_t));
325 position += readBytes;
331 // Update access position.
332 fd.set_position(position);
339 * Class: com/sun/cldc/io/ResourceInputStream
341 * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
343 JNIEXPORT jobject JNICALL Java_com_sun_cldc_io_ResourceInputStream_clone(JNIEnv *env, jclass clazz, jobject jobj)
345 com_sun_cldchi_jvm_FileDescriptor fd(jobj);
347 classinfo* c = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
348 java_handle_t* h = native_new_and_init(c);
353 com_sun_cldchi_jvm_FileDescriptor clonefd(h, fd);
355 return (jobject) clonefd.get_handle();
361 /* native methods implemented by this file ************************************/
363 static JNINativeMethod methods[] = {
364 { (char*) "open", (char*) "(Ljava/lang/String;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_open },
365 { (char*) "bytesRemain", (char*) "(Ljava/lang/Object;)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_bytesRemain },
366 { (char*) "readByte", (char*) "(Ljava/lang/Object;)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_readByte },
367 { (char*) "readBytes", (char*) "(Ljava/lang/Object;[BII)I", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_readBytes },
368 { (char*) "clone", (char*) "(Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_clone },
372 /* _Jv_com_sun_cldc_io_ResourceInputStream_init ********************************
374 Register native functions.
376 *******************************************************************************/
378 void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
380 utf* u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
382 NativeMethods& nm = VM::get_current()->get_nativemethods();
383 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
388 * These are local overrides for various environment variables in Emacs.
389 * Please do not remove this and leave it at the end of the file, where
390 * Emacs will automagically detect them.
391 * ---------------------------------------------------------------------
394 * indent-tabs-mode: t
398 * vim:noexpandtab:sw=4:ts=4: