Header Ads

[Adroid NDK] Passing String JINI



1
2
3
4
5
6
7
8
9
JNIEXPORT void JNICALLJava_MyJavaClass_printName(JNIEnv *env, jobject obj,
 jstring name)
{
 const char *str= (*env)->GetStringUTFChars(env,name,0);
 printf(%s”, str);
 //need to release this string when done with it in
 //order to avoid memory leak
 (*env)->ReleaseStringUTFChars(env, name, str);
}
To convert a C-style string to a jstring , you can use the (*env)->NewStringUTF() function to create a new jstring from a C-style string. For example, a C function that needs to return a Java string could contain the following code:
1
2
3
4
JNIEXPORT jstring JNICALLJava_MyJavaClass_getName(JNIEnv *env, jobject obj)
{
 return (*env)->NewStringUTF(env, “My String”);
}
JNI String functions:
JNI Function
Description
GetStringChars
ReleaseStringChars
Obtains or releases a pointer to the contents of a string in Unicode format. May return a copy of the string.
GetStringUTFChars
ReleaseStringUTFChars
Obtains or releases a pointer to the contents of a string in UTF-8 format.
GetStringLength
Returns the number of Unicode characters in the string.
GetStringUTFLength
Returns the number of bytes needed to represent a string in the UTF-8 format.
NewString
Creates a java.lang.String instance that contains the same sequence of characters as the given Unicode C string.
NewStringUTF
Creates a java.lang.String instance that contains the same sequence of characters as the given UTF-8 encoded C string.
GetStringCritical
ReleaseStringCritical
Obtains a pointer to the contents of a string in Unicode format. May return a copy of the string. Native code must not block between a pair of Get/ReleaseStringCritical calls.
GetStringRegion
SetStringRegion
Copies the contents of a string to or from a preallocated C buffer in the Unicode format.
GetStringUTFRegion
SetStringUTFRegion
Copies the content of a string to or from a preallocated C buffer in the UTF-8 format.
JNI String Example:
Let’s write a JNI application that passes a prompt message to a native method, which method prints the prompt message, reads the user input and sends it back to the application.
The Java code will be(NativePrompt.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class NativePrompt {
 private native String getInput(String prompt);  //native method
 static   //static initializer code
 {
  System.loadLibrary("NativePrompt");
 }

 public static void main(String[] args)
 {
  NativePrompt NP = new NativePrompt();
  String sName = NP.getInput("Enter your name: ");
        System.out.println("Hello " + sName);
 }
}
Compile NativePrompt.java (javac NativePrompt.java) and generate hedder file(javah -jni NativePrompt).
The generated with javah header file NativePrompt.h will be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class NativePrompt */

#ifndef _Included_NativePrompt
#define _Included_NativePrompt
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     NativePrompt
 * Method:    getInput
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_NativePrompt_getInput
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif
The C++ implementation file NativePrompt.cpp will be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "NativePrompt.h"
#include "jni.h"
#include "string"
#include "iostream"
#include "vector"

using namespace std;
/*
 * Class:     NativePrompt
 * Method:    getInput
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_NativePrompt_getInput
 (JNIEnv *env, jobject obj, jstring prompt){

 string sEntry;
 const char *str;
 str = env->GetStringUTFChars(prompt, NULL);
 if (str == NULL) {
  return env->NewStringUTF("");
 }
 else{
 cout << str;
  //Frees native string resources
  env->ReleaseStringUTFChars(prompt, str);

  //reads n-consecutive words from the
  //keyboard and store them in string
  getline(cin, sEntry);

        return env->NewStringUTF(sEntry.c_str());
 }
}
     

No comments:

Powered by Blogger.