Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into adrien
[simgrid.git] / src / bindings / java / jmsg_file.cpp
1 /* Java bindings of the file API.                                           */
2
3 /* Copyright (c) 2012-2020. The SimGrid Team. 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
8 #include "jmsg_file.h"
9 #include "jxbt_utilities.hpp"
10 #include <xbt/asserts.h>
11
12 void jfile_bind(JNIEnv *env, jobject jfile, msg_file_t fd) {
13   env->SetLongField(jfile, jfile_field_bind, reinterpret_cast<std::intptr_t>(fd));
14 }
15
16 msg_file_t jfile_get_native(JNIEnv *env, jobject jfile) {
17   return reinterpret_cast<msg_file_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 != nullptr), "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   sg_file_t file   = sg_file_open(path, nullptr);
29   jfile_bind(env, jfile, file);
30
31   env->ReleaseStringUTFChars(static_cast<jstring>(jpath), path);
32 }
33
34 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_File_read(JNIEnv *env, jobject jfile, jlong jsize) {
35   sg_file_t file = jfile_get_native(env, jfile);
36   return static_cast<jlong>(sg_file_read(file, static_cast<sg_size_t>(jsize)));
37 }
38
39 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_File_write(JNIEnv *env, jobject jfile, jlong jsize) {
40   sg_file_t file = jfile_get_native(env, jfile);
41   return static_cast<jlong>(sg_file_write(file, static_cast<sg_size_t>(jsize)));
42 }
43
44 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_seek(JNIEnv *env, jobject jfile, jlong offset, jlong origin) {
45   sg_file_t file = jfile_get_native(env, jfile);
46   sg_file_seek(file, static_cast<sg_offset_t>(offset), static_cast<int>(origin));
47 }
48
49 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_close(JNIEnv *env, jobject jfile) {
50   const_sg_file_t file = jfile_get_native(env, jfile);
51
52   sg_file_close(file);
53   jfile_bind(env, jfile, nullptr);
54 }