* configure.ac: Added check for sys/utsname.h header.
[cacao.git] / src / native / vm / cldc1.1 / com_sun_cldc_io_ResourceInputStream.cpp
1 /* src/native/vm/cldc1.1/com_sun_cldc_io_ResourceInputStream.cpp
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
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.
12
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.
17
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
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <errno.h>
29 #include <zlib.h>
30
31 #include "mm/memory.hpp"
32
33 #include "native/jni.hpp"
34 #include "native/native.hpp"
35
36 #if defined(ENABLE_JNI_HEADERS)
37 # include "native/include/com_sun_cldc_io_ResourceInputStream.h"
38 #endif
39
40 #include "threads/mutex.hpp"
41
42 #include "vm/array.hpp"
43 #include "vm/exceptions.hpp"
44 #include "vm/javaobjects.hpp"
45 #include "vm/os.hpp"
46 #include "vm/string.hpp"
47 #include "vm/types.h"
48 #include "vm/vm.hpp" /* REMOVE ME: temporarily */
49 #include "vm/zip.hpp"
50
51
52 static java_handle_t* zip_read_resource(list_classpath_entry *lce, utf *name)
53 {
54         hashtable_zipfile_entry *htzfe;
55         lfh                      lfh;
56         u1                      *indata;
57         u1                      *outdata;
58         z_stream                 zs;
59         int                      err;
60         
61         classinfo *ci;
62
63         /* try to find the class in the current archive */
64
65         htzfe = zip_find(lce, name);
66
67         if (htzfe == NULL)
68                 return NULL;
69
70         /* read stuff from local file header */
71
72         lfh.filenamelength   = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
73         lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
74
75         indata = htzfe->data +
76                 LFH_HEADER_SIZE +
77                 lfh.filenamelength +
78                 lfh.extrafieldlength;
79
80         /* allocate buffer for uncompressed data */
81
82         outdata = MNEW(u1, htzfe->uncompressedsize);
83
84         /* how is the file stored? */
85
86         switch (htzfe->compressionmethod) {
87         case Z_DEFLATED:
88                 /* fill z_stream structure */
89
90                 zs.next_in   = indata;
91                 zs.avail_in  = htzfe->compressedsize;
92                 zs.next_out  = outdata;
93                 zs.avail_out = htzfe->uncompressedsize;
94
95                 zs.zalloc = Z_NULL;
96                 zs.zfree  = Z_NULL;
97                 zs.opaque = Z_NULL;
98
99                 /* initialize this inflate run */
100
101                 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
102                         vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
103
104                 /* decompress the file into buffer */
105
106                 err = inflate(&zs, Z_SYNC_FLUSH);
107
108                 if ((err != Z_STREAM_END) && (err != Z_OK))
109                         vm_abort("zip_get: inflate failed: %s", strerror(errno));
110
111                 /* finish this inflate run */
112
113                 if (inflateEnd(&zs) != Z_OK)
114                         vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
115                 break;
116
117         case 0:
118                 /* uncompressed file, just copy the data */
119                 MCOPY(outdata, indata, u1, htzfe->compressedsize);
120                 break;
121
122         default:
123                 vm_abort("zip_get: unknown compression method %d",
124                                  htzfe->compressionmethod);
125         }
126                 
127         // Create a file descriptor object.
128         ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
129         java_handle_t* h = native_new_and_init(ci);
130
131         if (h == NULL)
132                 return NULL;
133
134         com_sun_cldchi_jvm_FileDescriptor fd(h, (int64_t) outdata, 0, htzfe->uncompressedsize);
135
136         return fd.get_handle();
137 }
138
139
140 static java_handle_t* file_read_resource(char *path) 
141 {
142         int len;
143         struct stat statBuffer;
144         u1 *filep;
145         classinfo *ci;
146         int fd;
147         
148         fd = open(path, O_RDONLY);
149         
150         if (fd > 0) {
151                 
152                 if (fstat(fd, &statBuffer) != -1) {
153                         len = statBuffer.st_size;
154                 } else {  
155                         return NULL;
156                 }
157                 
158                 /* Map file into the memory */
159                 filep = (u1*) mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
160                 
161                 /* Create a file descriptor object */
162                 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
163                 java_handle_t* h = native_new_and_init(ci);
164
165                 if (h == NULL)
166                         return NULL;
167
168                 com_sun_cldchi_jvm_FileDescriptor fd(h, (int64_t) filep, 0, len); 
169
170                 return fd.get_handle(); 
171         }
172         else {
173                 return NULL;
174         }
175 }
176
177
178 // Native functions are exported as C functions.
179 extern "C" {
180
181 /*
182  * Class:     com/sun/cldc/io/ResourceInputStream
183  * Method:    open
184  * Signature: (Ljava/lang/String;)Ljava/lang/Object;
185  */
186 JNIEXPORT jobject JNICALL Java_com_sun_cldc_io_ResourceInputStream_open(JNIEnv *env, jclass clazz, jstring name)
187 {
188         char *filename;
189         s4 filenamelen;
190         char *path;
191         utf *uname;
192         java_handle_t* descriptor;
193
194         // Get current list of classpath entries.
195         SuckClasspath& suckclasspath = VM::get_current()->get_suckclasspath();
196
197         /* get the classname as char string (do it here for the warning at
198        the end of the function) */
199
200         uname = javastring_toutf((java_handle_t *)name, false);
201         filenamelen = utf_bytes(uname) + strlen("0");
202         filename = MNEW(char, filenamelen);
203         utf_copy(filename, uname);
204
205         /* walk through all classpath entries */
206
207         for (SuckClasspath::iterator it = suckclasspath.begin(); it != suckclasspath.end(); it++) {
208                 list_classpath_entry* lce = *it;
209
210 #if defined(ENABLE_ZLIB)
211                 if (lce->type == CLASSPATH_ARCHIVE) {
212
213                         /* enter a monitor on zip/jar archives */
214                         lce->mutex->lock();
215
216                         /* try to get the file in current archive */
217                         descriptor = zip_read_resource(lce, uname);
218
219                         /* leave the monitor */
220                         lce->mutex->unlock();
221                         
222                         if (descriptor != NULL) { /* file exists */
223                                 break;
224                         }
225
226                 } else {
227 #endif
228                         
229                         path = MNEW(char, lce->pathlen + filenamelen);
230                         strcpy(path, lce->path);
231                         strcat(path, filename);
232
233                         descriptor = file_read_resource(path);
234                         
235                         MFREE(path, char, lce->pathlen + filenamelen);
236
237                         if (descriptor != NULL) { /* file exists */
238                                 break;
239                         }
240 #if defined(ENABLE_ZLIB)
241                 }
242 #endif  
243         }
244
245         MFREE(filename, char, filenamelen);
246
247         return (jobject) descriptor;
248 }
249
250
251 /*
252  * Class:     com/sun/cldc/io/ResourceInputStream
253  * Method:    bytesRemain
254  * Signature: (Ljava/lang/Object;)I
255  */
256 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_bytesRemain(JNIEnv *env, jclass clazz, jobject jobj)
257 {
258         com_sun_cldchi_jvm_FileDescriptor fd(jobj);
259         int32_t length   = fd.get_position();
260         int32_t position = fd.get_length();
261
262         return length - position;
263 }
264
265
266 /*
267  * Class:     com/sun/cldc/io/ResourceInputStream
268  * Method:    readByte
269  * Signature: (Ljava/lang/Object;)I
270  */
271 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_readByte(JNIEnv *env, jclass clazz, jobject jobj)
272 {
273         com_sun_cldchi_jvm_FileDescriptor fd(jobj);
274
275         int64_t filep    = fd.get_pointer();
276         int32_t position = fd.get_position();
277         int32_t length   = fd.get_length();
278
279         uint8_t byte;
280
281         if (position < length) {
282                 byte = ((uint8_t*) filep)[position];
283                 position++;
284         }
285         else {
286                 return -1; /* EOF */
287         }
288
289         // Update access position.
290         fd.set_position(position);
291         
292         return (byte & 0xFF);
293 }
294
295
296 /*
297  * Class:     com/sun/cldc/io/ResourceInputStream
298  * Method:    readBytes
299  * Signature: (Ljava/lang/Object;[BII)I
300  */
301 JNIEXPORT jint JNICALL Java_com_sun_cldc_io_ResourceInputStream_readBytes(JNIEnv *env, jclass clazz, jobject jobj, jbyteArray byteArray, jint off, jint len)
302 {
303         /* get pointer to the buffer */
304         // XXX Not GC safe.
305         ByteArray ba(byteArray);
306         void* buf = (void*) (((int8_t*) ba.get_raw_data_ptr()) + off);
307
308         com_sun_cldchi_jvm_FileDescriptor fd(jobj);
309
310         int64_t filep      = fd.get_pointer();
311         int32_t position   = fd.get_position();
312         int32_t fileLength = fd.get_length();
313
314         int32_t readBytes = -1;
315
316         if (position < fileLength) {
317                 int32_t available = fileLength - position;
318
319                 if (available < len) {
320                         readBytes = available;
321                 } else {
322                         readBytes = len;
323                 }
324
325                 os::memcpy(buf, ((uint8_t*) filep) + position, readBytes * sizeof(uint8_t));
326                 position += readBytes;
327         }
328         else {
329                 return -1; /* EOF */
330         }
331
332         // Update access position.
333         fd.set_position(position);
334         
335         return readBytes;
336 }
337
338
339 /*
340  * Class:     com/sun/cldc/io/ResourceInputStream
341  * Method:    clone
342  * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
343  */
344 JNIEXPORT jobject JNICALL Java_com_sun_cldc_io_ResourceInputStream_clone(JNIEnv *env, jclass clazz, jobject jobj)
345 {
346         com_sun_cldchi_jvm_FileDescriptor fd(jobj);
347
348         classinfo* c = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
349         java_handle_t* h = native_new_and_init(c);
350
351         if (h == NULL)
352                 return NULL;
353
354         com_sun_cldchi_jvm_FileDescriptor clonefd(h, fd);
355         
356         return (jobject) clonefd.get_handle();
357 }
358
359 } // extern "C"
360
361
362 /* native methods implemented by this file ************************************/
363  
364 static JNINativeMethod methods[] = {
365         { (char*) "open",        (char*) "(Ljava/lang/String;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_open        },
366         { (char*) "bytesRemain", (char*) "(Ljava/lang/Object;)I",                  (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_bytesRemain },
367         { (char*) "readByte",    (char*) "(Ljava/lang/Object;)I",                  (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_readByte    },
368         { (char*) "readBytes",   (char*) "(Ljava/lang/Object;[BII)I",              (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_readBytes   },
369         { (char*) "clone",       (char*) "(Ljava/lang/Object;)Ljava/lang/Object;", (void*) (uintptr_t) &Java_com_sun_cldc_io_ResourceInputStream_clone       },
370 };
371  
372
373 /* _Jv_com_sun_cldc_io_ResourceInputStream_init ********************************
374  
375    Register native functions.
376  
377 *******************************************************************************/
378  
379 void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
380 {
381         utf* u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
382  
383         NativeMethods& nm = VM::get_current()->get_nativemethods();
384         nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
385 }
386
387
388 /*
389  * These are local overrides for various environment variables in Emacs.
390  * Please do not remove this and leave it at the end of the file, where
391  * Emacs will automagically detect them.
392  * ---------------------------------------------------------------------
393  * Local variables:
394  * mode: c++
395  * indent-tabs-mode: t
396  * c-basic-offset: 4
397  * tab-width: 4
398  * End:
399  * vim:noexpandtab:sw=4:ts=4:
400  */