1 /* Java bindings of the file API. */
3 /* Copyright (c) 2012-2019. The SimGrid Team. All rights reserved. */
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. */
9 #include "jxbt_utilities.hpp"
11 void jfile_bind(JNIEnv *env, jobject jfile, msg_file_t fd) {
12 env->SetLongField(jfile, jfile_field_bind, reinterpret_cast<std::intptr_t>(fd));
15 msg_file_t jfile_get_native(JNIEnv *env, jobject jfile) {
16 return reinterpret_cast<msg_file_t>(env->GetLongField(jfile, jfile_field_bind));
19 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_nativeInit(JNIEnv *env, jclass cls) {
20 jclass class_File = env->FindClass("org/simgrid/msg/File");
21 jfile_field_bind = jxbt_get_jfield(env , class_File, "bind", "J");
22 xbt_assert((jfile_field_bind != nullptr), "Can't find 'bind' field in File class.");
25 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_open(JNIEnv *env, jobject jfile, jobject jpath) {
26 const char *path = env->GetStringUTFChars((jstring) jpath, 0);
27 msg_file_t file = MSG_file_open(path, nullptr);
28 jfile_bind(env, jfile, file);
30 env->ReleaseStringUTFChars(static_cast<jstring>(jpath), path);
33 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_File_read(JNIEnv *env, jobject jfile, jlong jsize) {
34 msg_file_t file = jfile_get_native(env, jfile);
35 return static_cast<jlong>(MSG_file_read(file, static_cast<sg_size_t>(jsize)));
38 JNIEXPORT jlong JNICALL Java_org_simgrid_msg_File_write(JNIEnv *env, jobject jfile, jlong jsize) {
39 msg_file_t file = jfile_get_native(env, jfile);
40 return static_cast<jlong>(MSG_file_write(file, static_cast<sg_size_t>(jsize)));
43 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_seek(JNIEnv *env, jobject jfile, jlong offset, jlong origin) {
44 msg_file_t file = jfile_get_native(env, jfile);
45 MSG_file_seek(file, static_cast<sg_offset_t>(offset), static_cast<int>(origin));
48 JNIEXPORT void JNICALL Java_org_simgrid_msg_File_close(JNIEnv *env, jobject jfile) {
49 msg_file_t file = jfile_get_native(env, jfile);
52 jfile_bind(env, jfile, nullptr);