Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c6a1db970c4d09ebca3ce97223033c9cbb0979db
[simgrid.git] / src / bindings / java / jmsg_file.c
1 /* Functions related to the java file API.                            */
2 /* Copyright (c) 2012-2013. The SimGrid Team.
3  * All rights reserved.                                                     */
4
5 /* This program is free software; you can redistribute it and/or modify it
6   * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "jmsg_file.h"
8 #include "jxbt_utilities.h"
9
10 void jfile_bind(JNIEnv *env, jobject jfile, msg_file_t fd) {
11   (*env)->SetLongField(env, jfile, jfile_field_bind, (intptr_t)fd);
12 }
13
14 msg_file_t jfile_get_native(JNIEnv *env, jobject jfile) {
15   msg_file_t file =
16     (msg_file_t)(intptr_t)(*env)->GetLongField(env, jfile, jfile_field_bind);
17   return file;
18 }
19
20 JNIEXPORT void JNICALL
21 Java_org_simgrid_msg_File_nativeInit(JNIEnv *env, jclass cls) {
22   jclass class_File = (*env)->FindClass(env, "org/simgrid/msg/File");
23   jfile_field_bind = jxbt_get_jfield(env , class_File, "bind", "J");
24   xbt_assert((jfile_field_bind != NULL), "Can't find \"bind\" field in File class.");
25 }
26 JNIEXPORT void JNICALL
27 Java_org_simgrid_msg_File_open(JNIEnv *env, jobject jfile, jobject jstorage, jobject jpath) {
28   const char *storage = (*env)->GetStringUTFChars(env, jstorage, 0);
29   const char *path = (*env)->GetStringUTFChars(env, jpath, 0);
30   msg_file_t file;
31
32   file = MSG_file_open(storage, path, NULL);
33   jfile_bind(env, jfile, file);
34
35   (*env)->ReleaseStringUTFChars(env, jstorage, storage);
36   (*env)->ReleaseStringUTFChars(env, jpath, path);
37 }
38 JNIEXPORT jlong JNICALL
39 Java_org_simgrid_msg_File_read(JNIEnv *env, jobject jfile, jlong jsize) {
40   msg_file_t file = jfile_get_native(env, jfile);
41   size_t n;
42   n = MSG_file_read(file, (size_t)jsize);
43   return (jlong)n;
44 }
45
46 JNIEXPORT jlong JNICALL
47 Java_org_simgrid_msg_File_write(JNIEnv *env, jobject jfile, jlong jsize) {
48   msg_file_t file = jfile_get_native(env, jfile);
49   size_t n;
50   n = MSG_file_write(file, (size_t)jsize);
51   return (jlong)n;
52 }
53 JNIEXPORT void JNICALL
54 Java_org_simgrid_msg_File_close(JNIEnv *env, jobject jfile) {
55   msg_file_t file = jfile_get_native(env, jfile);
56
57   MSG_file_close(file);
58   jfile_bind(env, jfile, NULL);
59 }
60