Created
October 15, 2018 23:00
-
-
Save Rishabhk07/104a16705ec6a7ea9ac9cc84a4605538 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <jni.h> | |
#include <string> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <iostream> | |
#include <string.h> | |
#include <android/log.h> | |
#define BLOCK_SIZE 4096 | |
extern "C" | |
JNIEXPORT jstring | |
JNICALL | |
Java_edu_ufl_cise_os_p2_P2Activity_stringFromJNI( | |
JNIEnv *env, | |
jobject /* this */) { | |
std::string hello = "Hello from C++"; | |
return env->NewStringUTF(hello.c_str()); | |
} | |
/* | |
* This is the new function readFile i added to implement the natuve file reading functionality | |
*/ | |
extern "C" | |
JNIEXPORT jstring JNICALL Java_edu_ufl_cise_os_p2_P2Activity_readFile( | |
JNIEnv *env, | |
jobject, jstring path) { | |
char * _path = (char *) env->GetStringUTFChars(path, NULL); | |
__android_log_print(ANDROID_LOG_VERBOSE, "readFile", "%s", _path); | |
int fd = -1; | |
ssize_t bytes_read = -1; | |
// POSIX commnad to open the file | |
fd = open((const char *) _path, O_RDONLY); | |
if (fd == -1) { | |
__android_log_print(ANDROID_LOG_VERBOSE, "readFile", "error in opening file"); | |
} | |
// seeking file to get the size of file and allocating the buffer of size of file | |
off_t sizeOfFile = lseek(fd, 0 , SEEK_END); | |
lseek(fd, 0,SEEK_SET); | |
char* buff = new char[sizeOfFile]; | |
// adding null character at the end of buffer | |
buff[sizeOfFile] = '\0'; | |
// using POSIX read() command to read files | |
while ((bytes_read = read(fd, buff, BLOCK_SIZE)) > 0); | |
jstring thisRead = env->NewStringUTF(buff); | |
// dealocating the memory | |
delete[] buff; | |
return thisRead; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment