Logo AND Algorithmique Numérique Distribuée

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