Debian Codegraph @

src/android/android_input_stream.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Android input stream.
12 *
13 * By Thomas Fjellstrom.
14 */
15
16 #include "allegro5/allegro.h"
17 #include "allegro5/internal/aintern_android.h"
18
19 #include <jni.h>
20
21 ALLEGRO_DEBUG_CHANNEL("android")
22
23 /* XXX ALLEGRO_FILE pointers currently passed as ints */
24 ALLEGRO_STATIC_ASSERT(android, sizeof(jlong) >= sizeof(ALLEGRO_FILE *));
25
26 JNI_FUNC(int, AllegroInputStream, nativeRead, (JNIEnv *env, jobject obj,
27 jlong handle, jbyteArray array, int offset, int length))
28 {
29 ALLEGRO_FILE *fp = (ALLEGRO_FILE *)(intptr_t)handle;
30 int ret = -1;
31 jbyte *array_ptr = NULL;
32 ASSERT(fp != NULL);
33
34 (void)obj;
35 ALLEGRO_DEBUG("nativeRead begin: handle:%lli fp:%p offset:%i length:%i",
36 handle, fp, offset, length);
37
38 int array_len = _jni_call(env, int, GetArrayLength, array);
39 ALLEGRO_DEBUG("array length: %i", array_len);
40
41 array_ptr = _jni_call(env, jbyte *, GetByteArrayElements, array, NULL);
42 ASSERT(array_ptr != NULL);
43
44 ALLEGRO_DEBUG("al_fread: p:%p, o:%i, l:%i", array_ptr, offset, length);
45 ret = al_fread(fp, array_ptr + offset, length);
46
47 if (ret == 0 && al_feof(fp)) {
48 /* InputStream.read() semantics. */
49 ret = -1;
50 }
51
52 _jni_callv(env, ReleaseByteArrayElements, array, array_ptr, 0);
53
54 ALLEGRO_DEBUG("nativeRead end");
55 return ret;
56 }
57
58 JNI_FUNC(void, AllegroInputStream, nativeClose, (JNIEnv *env, jobject obj,
59 jlong handle))
60 {
61 ALLEGRO_FILE *fp = (ALLEGRO_FILE *)(intptr_t)handle;
62 (void)env;
63 (void)obj;
64 al_fclose(fp);
65 }
66
67 /* vim: set sts=3 sw=3 et: */