Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / bindings / java / jmsg_file.cpp
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(jfile, jfile_field_bind, (intptr_t)fd);
14 }
15
16 msg_file_t jfile_get_native(JNIEnv *env, jobject jfile) {
17   return (msg_file_t)(intptr_t)env->GetLongField(jfile, jfile_field_bind);
18 }
19
20 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_nativeInit(JNIEnv *env, jclass cls) {
21   jclass class_File = env->FindClass("org/simgrid/msg/File");
22   jfile_field_bind = jxbt_get_jfield(env , class_File, "bind", "J");
23   xbt_assert((jfile_field_bind != NULL), "Can't find \"bind\" field in File class.");
24 }
25
26 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_open(JNIEnv *env, jobject jfile, jobject jpath) {
27   const char *path = env->GetStringUTFChars((jstring) jpath, 0);
28   msg_file_t file;
29
30   file = MSG_file_open(path, NULL);
31   jfile_bind(env, jfile, file);
32
33   env->ReleaseStringUTFChars((jstring) jpath, path);
34 }
35
36 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_File_read(JNIEnv *env, jobject jfile, jlong jsize) {
37   msg_file_t file = jfile_get_native(env, jfile);
38   return (jlong)MSG_file_read(file, (sg_size_t)jsize);
39 }
40
41 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_File_write(JNIEnv *env, jobject jfile, jlong jsize) {
42   msg_file_t file = jfile_get_native(env, jfile);
43   return (jlong)MSG_file_write(file, (sg_size_t)jsize);
44 }
45
46 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_close(JNIEnv *env, jobject jfile) {
47   msg_file_t file = jfile_get_native(env, jfile);
48
49   MSG_file_close(file);
50   jfile_bind(env, jfile, NULL);
51 }